|
| 1 | +import { useCallback, useState } from 'react' |
| 2 | + |
| 3 | +import { CreatePlaylistSource } from '@audius/common/models' |
| 4 | +import { |
| 5 | + cacheCollectionsActions, |
| 6 | + useCreatePlaylistModal |
| 7 | +} from '@audius/common/store' |
| 8 | +import { getErrorMessage } from '@audius/common/utils' |
| 9 | +import { |
| 10 | + Button, |
| 11 | + Flex, |
| 12 | + IconPlaylists, |
| 13 | + Modal, |
| 14 | + ModalContent, |
| 15 | + ModalFooter, |
| 16 | + ModalHeader, |
| 17 | + ModalTitle, |
| 18 | + TextArea, |
| 19 | + TextInput |
| 20 | +} from '@audius/harmony' |
| 21 | +import { useDispatch } from 'react-redux' |
| 22 | + |
| 23 | +import UploadArtwork from 'components/upload/UploadArtwork' |
| 24 | +import { resizeImage } from 'utils/imageProcessingUtil' |
| 25 | + |
| 26 | +const { createPlaylist, createAlbum } = cacheCollectionsActions |
| 27 | + |
| 28 | +const messages = { |
| 29 | + createPlaylistTitle: 'Create New Playlist', |
| 30 | + createAlbumTitle: 'Create New Album', |
| 31 | + nameLabel: 'Playlist Name', |
| 32 | + nameLabelAlbum: 'Album Name', |
| 33 | + namePlaceholder: 'Give your playlist a name', |
| 34 | + namePlaceholderAlbum: 'Give your album a name', |
| 35 | + descriptionLabel: 'Description', |
| 36 | + descriptionPlaceholder: 'Describe what makes this special (optional)', |
| 37 | + cancel: 'Cancel', |
| 38 | + create: 'Create', |
| 39 | + defaultName: 'New Playlist', |
| 40 | + defaultAlbumName: 'New Album' |
| 41 | +} |
| 42 | + |
| 43 | +type ArtworkValue = { |
| 44 | + url: string |
| 45 | + file: File |
| 46 | + source?: string |
| 47 | +} | null |
| 48 | + |
| 49 | +export const CreatePlaylistModal = () => { |
| 50 | + const dispatch = useDispatch() |
| 51 | + const { isOpen, onClose, onClosed, data } = useCreatePlaylistModal() |
| 52 | + const { isAlbum = false, initTrackId } = data |
| 53 | + |
| 54 | + const [name, setName] = useState('') |
| 55 | + const [description, setDescription] = useState('') |
| 56 | + const [artwork, setArtwork] = useState<ArtworkValue>(null) |
| 57 | + const [imageProcessingError, setImageProcessingError] = useState(false) |
| 58 | + const [isSubmitting, setIsSubmitting] = useState(false) |
| 59 | + |
| 60 | + const reset = useCallback(() => { |
| 61 | + setName('') |
| 62 | + setDescription('') |
| 63 | + setArtwork(null) |
| 64 | + setImageProcessingError(false) |
| 65 | + setIsSubmitting(false) |
| 66 | + }, []) |
| 67 | + |
| 68 | + const handleClose = useCallback(() => { |
| 69 | + onClose() |
| 70 | + }, [onClose]) |
| 71 | + |
| 72 | + const handleClosed = useCallback(() => { |
| 73 | + reset() |
| 74 | + onClosed() |
| 75 | + }, [onClosed, reset]) |
| 76 | + |
| 77 | + const handleDropArtwork = useCallback( |
| 78 | + async (selectedFiles: File[], source: string) => { |
| 79 | + try { |
| 80 | + let file = selectedFiles[0] |
| 81 | + file = await resizeImage(file) |
| 82 | + // @ts-ignore writing to read-only property; matches ArtworkField pattern |
| 83 | + file.name = selectedFiles[0].name |
| 84 | + const url = URL.createObjectURL(file) |
| 85 | + setArtwork({ url, file, source }) |
| 86 | + setImageProcessingError(false) |
| 87 | + } catch (err) { |
| 88 | + // eslint-disable-next-line no-console |
| 89 | + console.error(getErrorMessage(err)) |
| 90 | + setImageProcessingError(true) |
| 91 | + } |
| 92 | + }, |
| 93 | + [] |
| 94 | + ) |
| 95 | + |
| 96 | + const handleRemoveArtwork = useCallback(() => { |
| 97 | + setArtwork(null) |
| 98 | + }, []) |
| 99 | + |
| 100 | + const handleSubmit = useCallback(() => { |
| 101 | + setIsSubmitting(true) |
| 102 | + const trimmedName = name.trim() |
| 103 | + const trimmedDescription = description.trim() |
| 104 | + const playlistName = |
| 105 | + trimmedName || |
| 106 | + (isAlbum ? messages.defaultAlbumName : messages.defaultName) |
| 107 | + const action = isAlbum ? createAlbum : createPlaylist |
| 108 | + dispatch( |
| 109 | + action( |
| 110 | + { |
| 111 | + playlist_name: playlistName, |
| 112 | + description: trimmedDescription || undefined, |
| 113 | + // Cast: optimisticallySavePlaylist accepts the form-shape artwork. |
| 114 | + ...(artwork ? { artwork: artwork as any } : {}) |
| 115 | + }, |
| 116 | + CreatePlaylistSource.NAV, |
| 117 | + initTrackId, |
| 118 | + 'route' |
| 119 | + ) |
| 120 | + ) |
| 121 | + onClose() |
| 122 | + }, [name, description, artwork, isAlbum, initTrackId, dispatch, onClose]) |
| 123 | + |
| 124 | + const title = isAlbum |
| 125 | + ? messages.createAlbumTitle |
| 126 | + : messages.createPlaylistTitle |
| 127 | + const nameLabel = isAlbum ? messages.nameLabelAlbum : messages.nameLabel |
| 128 | + const namePlaceholder = isAlbum |
| 129 | + ? messages.namePlaceholderAlbum |
| 130 | + : messages.namePlaceholder |
| 131 | + |
| 132 | + return ( |
| 133 | + <Modal |
| 134 | + isOpen={isOpen} |
| 135 | + onClose={handleClose} |
| 136 | + onClosed={handleClosed} |
| 137 | + size='small' |
| 138 | + > |
| 139 | + <ModalHeader onClose={handleClose}> |
| 140 | + <ModalTitle title={title} icon={<IconPlaylists />} /> |
| 141 | + </ModalHeader> |
| 142 | + <ModalContent> |
| 143 | + <Flex direction='column' gap='l'> |
| 144 | + <Flex justifyContent='center'> |
| 145 | + <UploadArtwork |
| 146 | + artworkUrl={artwork?.url} |
| 147 | + onDropArtwork={handleDropArtwork} |
| 148 | + onRemoveArtwork={artwork ? handleRemoveArtwork : undefined} |
| 149 | + imageProcessingError={imageProcessingError} |
| 150 | + size='small' |
| 151 | + isUpload |
| 152 | + /> |
| 153 | + </Flex> |
| 154 | + <TextInput |
| 155 | + label={nameLabel} |
| 156 | + placeholder={namePlaceholder} |
| 157 | + value={name} |
| 158 | + onChange={(e) => setName(e.target.value)} |
| 159 | + maxLength={64} |
| 160 | + /> |
| 161 | + <TextArea |
| 162 | + aria-label={messages.descriptionLabel} |
| 163 | + placeholder={messages.descriptionPlaceholder} |
| 164 | + value={description} |
| 165 | + onChange={(e) => setDescription(e.target.value)} |
| 166 | + maxLength={1000} |
| 167 | + showMaxLength |
| 168 | + grows |
| 169 | + /> |
| 170 | + </Flex> |
| 171 | + </ModalContent> |
| 172 | + <ModalFooter> |
| 173 | + <Flex gap='xl' flex={1}> |
| 174 | + <Button variant='secondary' fullWidth onClick={handleClose}> |
| 175 | + {messages.cancel} |
| 176 | + </Button> |
| 177 | + <Button |
| 178 | + variant='primary' |
| 179 | + fullWidth |
| 180 | + isLoading={isSubmitting} |
| 181 | + disabled={isSubmitting} |
| 182 | + onClick={handleSubmit} |
| 183 | + > |
| 184 | + {messages.create} |
| 185 | + </Button> |
| 186 | + </Flex> |
| 187 | + </ModalFooter> |
| 188 | + </Modal> |
| 189 | + ) |
| 190 | +} |
| 191 | + |
| 192 | +export default CreatePlaylistModal |
0 commit comments