| 43 | }; |
| 44 | |
| 45 | export function useDragResize({ |
| 46 | defaultSizeRelation = DEFAULT_FLEX, |
| 47 | direction, |
| 48 | initiallyHidden, |
| 49 | onHiddenElementChange, |
| 50 | sizeThresholdFirst = 100, |
| 51 | sizeThresholdSecond = 100, |
| 52 | storageKey, |
| 53 | }: UseDragResizeArgs) { |
| 54 | const storage = useStorageContext(); |
| 55 | |
| 56 | const store = debounce(500, (value: string) => { |
| 57 | if (storageKey) { |
| 58 | storage?.set(storageKey, value); |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | const [hiddenElement, setHiddenElement] = useState<ResizableElement | null>( |
| 63 | () => { |
| 64 | const storedValue = storageKey && storage?.get(storageKey); |
| 65 | if (storedValue === HIDE_FIRST || initiallyHidden === 'first') { |
| 66 | return 'first'; |
| 67 | } |
| 68 | if (storedValue === HIDE_SECOND || initiallyHidden === 'second') { |
| 69 | return 'second'; |
| 70 | } |
| 71 | return null; |
| 72 | }, |
| 73 | ); |
| 74 | |
| 75 | const setHiddenElementWithCallback = // eslint-disable-line react-hooks/exhaustive-deps -- false positive, function is optimized by react-compiler, no need to wrap with useCallback |
| 76 | (element: ResizableElement | null) => { |
| 77 | if (element !== hiddenElement) { |
| 78 | setHiddenElement(element); |
| 79 | onHiddenElementChange?.(element); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | const firstRef = useRef<HTMLDivElement>(null); |
| 84 | const dragBarRef = useRef<HTMLDivElement>(null); |
| 85 | const secondRef = useRef<HTMLDivElement>(null); |
| 86 | |
| 87 | const defaultFlexRef = useRef(`${defaultSizeRelation}`); |
| 88 | |
| 89 | /** |
| 90 | * Set initial flex values |
| 91 | */ |
| 92 | useLayoutEffect(() => { |
| 93 | const storedValue = |
| 94 | (storageKey && storage?.get(storageKey)) || defaultFlexRef.current; |
| 95 | |
| 96 | if (firstRef.current) { |
| 97 | firstRef.current.style.display = 'flex'; |
| 98 | firstRef.current.style.flex = |
| 99 | storedValue === HIDE_FIRST || storedValue === HIDE_SECOND |
| 100 | ? defaultFlexRef.current |
| 101 | : storedValue; |
| 102 | } |