webapp/src/components/Editor/AudioUploader/AudioUploader.tsx

72 lines
2.2 KiB
TypeScript
Raw Normal View History

import { clsx } from 'clsx'
import { Show } from 'solid-js'
2024-07-06 06:51:25 +00:00
import { isServer } from 'solid-js/web'
import { DropArea } from '~/components/_shared/DropArea'
import { useLocalize } from '~/context/localize'
2024-07-13 11:42:53 +00:00
import { composeMediaItems } from '~/lib/composeMediaItems'
2024-06-24 17:50:27 +00:00
import { MediaItem } from '~/types/mediaitem'
import { AudioPlayer } from '../../Article/AudioPlayer'
import styles from './AudioUploader.module.scss'
2024-07-06 06:51:25 +00:00
if (!isServer && window) window.Buffer = Buffer
2024-07-07 13:48:53 +00:00
// console.debug('buffer patch passed')
type Props = {
class?: string
audio: MediaItem[]
baseFields?: {
artist?: string
date?: string
genre?: string
}
onAudioChange: (index: number, value: MediaItem) => void
onAudioAdd: (value: MediaItem[]) => void
onAudioSorted: (value: MediaItem[]) => void
}
export const AudioUploader = (props: Props) => {
const { t } = useLocalize()
2024-06-24 17:50:27 +00:00
const handleMediaItemFieldChange = (
index: number,
field: keyof MediaItem | string | symbol | number,
2024-06-26 08:22:05 +00:00
value: string
2024-06-24 17:50:27 +00:00
) => {
props.onAudioChange(index, { ...props.audio[index], [field]: value })
}
const handleChangeIndex = (direction: 'up' | 'down', index: number) => {
const media = [...props.audio]
2024-06-02 10:37:54 +00:00
if (media?.length > 0) {
if (direction === 'up' && index > 0) {
const copy = media.splice(index, 1)[0]
media.splice(index - 1, 0, copy)
} else if (direction === 'down' && index < media.length - 1) {
const copy = media.splice(index, 1)[0]
media.splice(index + 1, 0, copy)
}
}
props.onAudioSorted(media)
}
return (
<div class={clsx(styles.AudioUploader, props.class)}>
<Show when={props.audio.length > 0}>
<AudioPlayer
editorMode={true}
media={props.audio}
onMediaItemFieldChange={handleMediaItemFieldChange}
onChangeMediaIndex={handleChangeIndex}
/>
</Show>
<DropArea
isMultiply={true}
placeholder={t('Add audio')}
description={t('You can download multiple tracks at once in .mp3, .wav or .flac formats')}
fileType={'audio'}
onUpload={(value) => props.onAudioAdd(composeMediaItems(value, props.baseFields))}
/>
</div>
)
}