refactor: use normal variable instead of component state for better maintaince
All checks were successful
/ deploy_site (push) Successful in 24s

This commit is contained in:
Minhyeok Park 2024-11-13 15:12:48 +09:00
parent d0cf9e65b9
commit 2f400f5e48
Signed by: pmh_only
SSH Key Fingerprint: SHA256:g/OyGvi2pcd8ub9mqge/ohmDP0fZX/xOPWPIcM+9XpI
2 changed files with 25 additions and 21 deletions

View File

@ -25,14 +25,15 @@ interface TransformGridItemProp {
export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => { export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => {
const [value, setValue] = useRecoilState(EditorValueState) const [value, setValue] = useRecoilState(EditorValueState)
const [options, setOptions] = useState(transform.options) const [options, setOptions] = useState(transform.options)
const [previewDisabled, setPreviewDisabled] = useState(false) const [closedToggle, setClosedToggle] = useLocalStorage(`transform_closed__${transform.name}`, false)
const [alreadyClosed, setAlreadyClosed] = useState(false)
const [closed, setClosed] = useLocalStorage(`transform_closed__${transform.name}`, false)
const [result, setResult] = useState<WrappedTransformResult>({ const [result, setResult] = useState<WrappedTransformResult>({
error: false, error: false,
value: '' value: ''
}) })
const previewDisabled = value.length > 30000
const closed = previewDisabled || closedToggle
const triggerTransform = async () => const triggerTransform = async () =>
await transform await transform
.fn(value, options) .fn(value, options)
@ -41,10 +42,7 @@ export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => {
return result return result
}) })
useEffect(() => { // trigger transform when value, option or closed state changed
setAlreadyClosed(closed)
}, [])
useEffect(() => { useEffect(() => {
if (previewDisabled) if (previewDisabled)
return return
@ -52,10 +50,20 @@ export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => {
triggerTransform() triggerTransform()
}, [value, options, closed]) }, [value, options, closed])
// reset error state when option changed
useEffect(() => { useEffect(() => {
if (value.length > 30000) { if (!previewDisabled)
setClosed(true) return
setPreviewDisabled(true)
setResult({
error: false,
value: ''
})
}, [options])
// forcely disable preview when value is longer than 30,000 chars
useEffect(() => {
if (previewDisabled) {
setResult({ setResult({
error: false, error: false,
value: '' value: ''
@ -63,11 +71,7 @@ export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => {
return return
} }
if (previewDisabled) {
setClosed(alreadyClosed)
triggerTransform() triggerTransform()
setPreviewDisabled(false)
}
}, [value]) }, [value])
const onCheckboxOptionChanged = const onCheckboxOptionChanged =
@ -133,11 +137,7 @@ export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => {
if (previewDisabled) if (previewDisabled)
return return
if (closed) setClosedToggle(!closed)
triggerTransform()
setClosed(!closed)
setAlreadyClosed(!closed)
} }
return ( return (
@ -152,7 +152,7 @@ export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => {
</Button> </Button>
</div> </div>
<Tooltip id={`${transform.name}-tooltip`} place="bottom"> <Tooltip className={style.error} id={`${transform.name}-tooltip`} place="bottom">
{previewDisabled && result.error && result.value} {previewDisabled && result.error && result.value}
</Tooltip> </Tooltip>

View File

@ -13,6 +13,10 @@
background-color: #323232; background-color: #323232;
border-radius: 4px; border-radius: 4px;
.error {
color: #ffcaca;
}
.name { .name {
font-size: inherit; font-size: inherit;
padding: 14px 6px; padding: 14px 6px;