import { Box, Group, Stack, TextInput } from '@mantine/core'; import { DateTimePicker } from '@mantine/dates'; import { useForm } from '@mantine/form'; import { closeModal, ContextModalProps } from '@mantine/modals'; import { useTranslation } from 'react-i18next'; import { useShareItem } from '../mutations/share-item-mutation'; import { Button, Switch, toast } from '/@/renderer/components'; import { useCurrentServer } from '/@/renderer/store'; export const ShareItemContextModal = ({ id, innerProps, }: ContextModalProps<{ itemIds: string[]; resourceType: string; }>) => { const { t } = useTranslation(); const { itemIds, resourceType } = innerProps; const server = useCurrentServer(); const shareItemMutation = useShareItem({}); // Uses the same default as Navidrome: 1 year const defaultDate = new Date(); defaultDate.setFullYear(defaultDate.getFullYear() + 1); const form = useForm({ initialValues: { allowDownloading: false, description: '', expires: defaultDate, }, validate: { expires: (value) => value > new Date() ? null : t('form.shareItem.expireInvalid', { postProcess: 'sentenceCase', }), }, }); const handleSubmit = form.onSubmit(async (values) => { shareItemMutation.mutate( { body: { description: values.description, downloadable: values.allowDownloading, expires: values.expires.getTime(), resourceIds: itemIds.join(), resourceType, }, serverId: server?.id, }, { onError: () => { toast.error({ message: t('form.shareItem.createFailed', { postProcess: 'sentenceCase', }), }); }, onSuccess: (_data) => { if (!server) throw new Error('Server not found'); if (!_data?.id) throw new Error('Failed to share item'); const shareUrl = `${server.url}/share/${_data.id}`; navigator.clipboard.writeText(shareUrl); toast.success({ autoClose: 5000, id: 'share-item-toast', message: t('form.shareItem.success', { postProcess: 'sentenceCase', }), onClick: (a) => { if (!(a.target instanceof HTMLElement)) return; // Make sure we weren't clicking close (otherwise clicking close /also/ opens the url) if (a.target.nodeName !== 'svg') { window.open(shareUrl); toast.hide('share-item-toast'); } }, }); }, }, ); closeModal(id); return null; }); return (
); };