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,13 +25,14 @@ interface TransformGridItemProp {
export const TransformGridItem: FC<TransformGridItemProp> = ({ 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<WrappedTransformResult>({
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<TransformGridItemProp> = ({ 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<TransformGridItemProp> = ({ 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<TransformGridItemProp> = ({ transform }) => {
return
}
if (previewDisabled) {
setClosed(alreadyClosed)
triggerTransform()
setPreviewDisabled(false)
}
triggerTransform()
}, [value])
const onCheckboxOptionChanged =
@ -132,12 +136,8 @@ export const TransformGridItem: FC<TransformGridItemProp> = ({ 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<TransformGridItemProp> = ({ transform }) => {
</Button>
</div>
<Tooltip id={`${transform.name}-tooltip`} place="bottom">
<Tooltip className={style.error} id={`${transform.name}-tooltip`} place="bottom">
{previewDisabled && result.error && result.value}
</Tooltip>

View File

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