import { useQueryClient } from '@tanstack/react-query'; import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { PROJECT_DATA } from '../../../../common/api/constants'; import { getDb, patchData } from '../../../../common/api/db'; import { maybeAxiosError } from '../../../../common/api/utils'; import Button from '../../../../common/components/buttons/Button'; import Info from '../../../../common/components/info/Info'; import Modal from '../../../../common/components/modal/Modal'; import Switch from '../../../../common/components/switch/Switch'; import { cx } from '../../../../common/utils/styleUtils'; import * as Panel from '../../panel-utils/PanelUtils'; import { makeProjectPatch } from './project.utils'; import style from './ProjectPanel.module.scss'; const formId = 'project-merge-form'; interface ProjectMergeFromProps { onClose: () => void; fileName: string; } type ProjectMergeFormValues = { project: boolean; rundowns: boolean; viewSettings: boolean; urlPresets: boolean; automation: boolean; }; export default function ProjectMergeForm({ onClose, fileName }: ProjectMergeFromProps) { const [error, setError] = useState(null); const queryClient = useQueryClient(); const { handleSubmit, watch, setValue, formState: { isSubmitting, isValid, isDirty }, } = useForm({ defaultValues: { project: false, rundowns: false, viewSettings: false, urlPresets: false, automation: false, }, resetOptions: { keepDirtyValues: true, }, }); const handleSubmitCreate = async (values: ProjectMergeFormValues) => { const allFalse = Object.values(values).every((value) => !value); if (allFalse) { setError('At least one option must be selected'); return; } try { setError(null); // make patch object const { data } = await getDb(fileName); if (!data.settings.version.startsWith('4.')) { setError('The project you are attempting to merge is from an older version and it need to be migrated first'); return; } const patch = await makeProjectPatch(data, values); // request patch await patchData(patch); await queryClient.invalidateQueries({ queryKey: PROJECT_DATA }); onClose(); } catch (error) { setError(maybeAxiosError(error)); } }; return ( {error && {error}} } bodyElements={
Select data from {`"${fileName}"`} to merge into the current project. This process is irreversible and can result in data loss.
You may want to create a duplicate backup beforehand.
} /> ); }