import { fileBitsToString } from '@/helpers'; import useFileManagerSwr from '@/plugins/useFileManagerSwr'; import React from 'react'; import Modal, { RequiredModalProps } from '@/components/elements/Modal'; import { Form, Formik, FormikHelpers } from 'formik'; import Field from '@/components/elements/Field'; import { chmodFiles } from '@/api/server/files'; import { ServerContext } from '@/state/server'; import tw from 'twin.macro'; import Button from '@/components/elements/Button'; import useFlash from '@/plugins/useFlash'; interface FormikValues { mode: string; } interface File { file: string; mode: string; } type OwnProps = RequiredModalProps & { files: File[] }; const ChmodFileModal = ({ files, ...props }: OwnProps) => { const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const { mutate } = useFileManagerSwr(); const { clearFlashes, clearAndAddHttpError } = useFlash(); const directory = ServerContext.useStoreState(state => state.files.directory); const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles); const submit = async ({ mode }: FormikValues, { setSubmitting }: FormikHelpers) => { clearFlashes('files'); await mutate(data => data!.map(f => f.name === files[0].file ? { ...f, mode: fileBitsToString(mode, !f.isFile), modeBits: mode } : f), false); const data = files.map(f => ({ file: f.file, mode: mode })); chmodFiles(uuid, directory, data) .then(async (): Promise => files.length > 0 ? await mutate() : Promise.resolve()) .then(() => setSelectedFiles([])) .catch(async (error) => { await mutate(); setSubmitting(false); clearAndAddHttpError({ key: 'files', error }); }) .then(() => props.onDismissed()); }; return ( 1 ? '' : (files[0].mode || '') }}> {({ isSubmitting }) => (
)}
); }; export default ChmodFileModal;