refactor: use normal variable instead of component state for better maintaince
All checks were successful
/ deploy_site (push) Successful in 24s
All checks were successful
/ deploy_site (push) Successful in 24s
This commit is contained in:
parent
d0cf9e65b9
commit
2f400f5e48
@ -25,13 +25,14 @@ 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
|
||||||
@ -40,11 +41,8 @@ export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => {
|
|||||||
setResult(result)
|
setResult(result)
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setAlreadyClosed(closed)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
|
// trigger transform when value, option or closed state changed
|
||||||
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) {
|
triggerTransform()
|
||||||
setClosed(alreadyClosed)
|
|
||||||
triggerTransform()
|
|
||||||
setPreviewDisabled(false)
|
|
||||||
}
|
|
||||||
}, [value])
|
}, [value])
|
||||||
|
|
||||||
const onCheckboxOptionChanged =
|
const onCheckboxOptionChanged =
|
||||||
@ -132,12 +136,8 @@ export const TransformGridItem: FC<TransformGridItemProp> = ({ transform }) => {
|
|||||||
const onLabelClicked = async () => {
|
const onLabelClicked = async () => {
|
||||||
if (previewDisabled)
|
if (previewDisabled)
|
||||||
return
|
return
|
||||||
|
|
||||||
if (closed)
|
|
||||||
triggerTransform()
|
|
||||||
|
|
||||||
setClosed(!closed)
|
setClosedToggle(!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>
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user