From 2f400f5e48be215cfd7676df536d3760dd75efa6 Mon Sep 17 00:00:00 2001 From: Minhyeok Park Date: Wed, 13 Nov 2024 15:12:48 +0900 Subject: [PATCH] refactor: use normal variable instead of component state for better maintaince --- src/TransformGridItem/index.tsx | 42 ++++++++++++------------- src/TransformGridItem/style.module.scss | 4 +++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/TransformGridItem/index.tsx b/src/TransformGridItem/index.tsx index 1f1ddf2..12d2e3e 100644 --- a/src/TransformGridItem/index.tsx +++ b/src/TransformGridItem/index.tsx @@ -25,13 +25,14 @@ interface TransformGridItemProp { export const TransformGridItem: FC = ({ transform }) => { const [value, setValue] = useRecoilState(EditorValueState) const [options, setOptions] = useState(transform.options) - const [previewDisabled, setPreviewDisabled] = useState(false) - const [alreadyClosed, setAlreadyClosed] = useState(false) - const [closed, setClosed] = useLocalStorage(`transform_closed__${transform.name}`, false) + const [closedToggle, setClosedToggle] = useLocalStorage(`transform_closed__${transform.name}`, false) const [result, setResult] = useState({ error: false, value: '' }) + + const previewDisabled = value.length > 30000 + const closed = previewDisabled || closedToggle const triggerTransform = async () => await transform @@ -40,11 +41,8 @@ export const TransformGridItem: FC = ({ transform }) => { setResult(result) return result }) - - useEffect(() => { - setAlreadyClosed(closed) - }, []) + // trigger transform when value, option or closed state changed useEffect(() => { if (previewDisabled) return @@ -52,10 +50,20 @@ export const TransformGridItem: FC = ({ transform }) => { triggerTransform() }, [value, options, closed]) + // reset error state when option changed useEffect(() => { - if (value.length > 30000) { - setClosed(true) - setPreviewDisabled(true) + if (!previewDisabled) + return + + setResult({ + error: false, + value: '' + }) + }, [options]) + + // forcely disable preview when value is longer than 30,000 chars + useEffect(() => { + if (previewDisabled) { setResult({ error: false, value: '' @@ -63,11 +71,7 @@ export const TransformGridItem: FC = ({ transform }) => { return } - if (previewDisabled) { - setClosed(alreadyClosed) - triggerTransform() - setPreviewDisabled(false) - } + triggerTransform() }, [value]) const onCheckboxOptionChanged = @@ -132,12 +136,8 @@ export const TransformGridItem: FC = ({ transform }) => { const onLabelClicked = async () => { if (previewDisabled) return - - if (closed) - triggerTransform() - setClosed(!closed) - setAlreadyClosed(!closed) + setClosedToggle(!closed) } return ( @@ -152,7 +152,7 @@ export const TransformGridItem: FC = ({ transform }) => { - + {previewDisabled && result.error && result.value} diff --git a/src/TransformGridItem/style.module.scss b/src/TransformGridItem/style.module.scss index da1b9c2..7c1320f 100644 --- a/src/TransformGridItem/style.module.scss +++ b/src/TransformGridItem/style.module.scss @@ -13,6 +13,10 @@ background-color: #323232; border-radius: 4px; + .error { + color: #ffcaca; + } + .name { font-size: inherit; padding: 14px 6px;