|
| 1 | +import { Box } from '@mui/material'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import ToolContent from '@components/ToolContent'; |
| 4 | +import { ToolComponentProps } from '@tools/defineTool'; |
| 5 | +import { GetGroupsType } from '@components/options/ToolOptions'; |
| 6 | +import { CardExampleType } from '@components/examples/ToolExamples'; |
| 7 | +import { loopVideo } from './service'; |
| 8 | +import { InitialValuesType } from './types'; |
| 9 | +import ToolVideoInput from '@components/input/ToolVideoInput'; |
| 10 | +import ToolFileResult from '@components/result/ToolFileResult'; |
| 11 | +import TextFieldWithDesc from '@components/options/TextFieldWithDesc'; |
| 12 | +import { updateNumberField } from '@utils/string'; |
| 13 | +import * as Yup from 'yup'; |
| 14 | + |
| 15 | +const initialValues: InitialValuesType = { |
| 16 | + loops: 2 |
| 17 | +}; |
| 18 | + |
| 19 | +const validationSchema = Yup.object({ |
| 20 | + loops: Yup.number().min(1, 'Number of loops must be greater than 1') |
| 21 | +}); |
| 22 | + |
| 23 | +export default function Loop({ title, longDescription }: ToolComponentProps) { |
| 24 | + const [input, setInput] = useState<File | null>(null); |
| 25 | + const [result, setResult] = useState<File | null>(null); |
| 26 | + const [loading, setLoading] = useState(false); |
| 27 | + |
| 28 | + const compute = async (values: InitialValuesType, input: File | null) => { |
| 29 | + if (!input) return; |
| 30 | + try { |
| 31 | + setLoading(true); |
| 32 | + const resultFile = await loopVideo(input, values); |
| 33 | + await setResult(resultFile); |
| 34 | + } catch (error) { |
| 35 | + console.error(error); |
| 36 | + } finally { |
| 37 | + setLoading(false); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + const getGroups: GetGroupsType<InitialValuesType> | null = ({ |
| 42 | + values, |
| 43 | + updateField |
| 44 | + }) => [ |
| 45 | + { |
| 46 | + title: 'Loops', |
| 47 | + component: ( |
| 48 | + <Box> |
| 49 | + <TextFieldWithDesc |
| 50 | + onOwnChange={(value) => |
| 51 | + updateNumberField(value, 'loops', updateField) |
| 52 | + } |
| 53 | + value={values.loops} |
| 54 | + label={'Number of Loops'} |
| 55 | + /> |
| 56 | + </Box> |
| 57 | + ) |
| 58 | + } |
| 59 | + ]; |
| 60 | + return ( |
| 61 | + <ToolContent |
| 62 | + title={title} |
| 63 | + input={input} |
| 64 | + inputComponent={ |
| 65 | + <ToolVideoInput |
| 66 | + value={input} |
| 67 | + onChange={setInput} |
| 68 | + accept={['video/*']} |
| 69 | + /> |
| 70 | + } |
| 71 | + resultComponent={ |
| 72 | + loading ? ( |
| 73 | + <ToolFileResult |
| 74 | + value={null} |
| 75 | + title={'Looping Video'} |
| 76 | + loading={true} |
| 77 | + extension={''} |
| 78 | + /> |
| 79 | + ) : ( |
| 80 | + <ToolFileResult |
| 81 | + value={result} |
| 82 | + title={'Looped Video'} |
| 83 | + extension={'mp4'} |
| 84 | + /> |
| 85 | + ) |
| 86 | + } |
| 87 | + initialValues={initialValues} |
| 88 | + validationSchema={validationSchema} |
| 89 | + getGroups={getGroups} |
| 90 | + setInput={setInput} |
| 91 | + compute={compute} |
| 92 | + toolInfo={{ title: `What is a ${title}?`, description: longDescription }} |
| 93 | + /> |
| 94 | + ); |
| 95 | +} |
0 commit comments