|
| 1 | +import { useAppStore } from 'app/store/nanostores/store'; |
| 2 | +import type { Dimensions } from 'features/controlLayers/store/types'; |
| 3 | +import { selectUiSlice, textAreaSizesStateChanged } from 'features/ui/store/uiSlice'; |
| 4 | +import { debounce } from 'lodash-es'; |
| 5 | +import { type RefObject, useCallback, useEffect, useMemo } from 'react'; |
| 6 | + |
| 7 | +type Options = { |
| 8 | + trackWidth: boolean; |
| 9 | + trackHeight: boolean; |
| 10 | + initialWidth?: number; |
| 11 | + initialHeight?: number; |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * Persists the width and/or height of a text area to redux. |
| 16 | + * @param id The unique id of this textarea, used as key to storage |
| 17 | + * @param ref A ref to the textarea element |
| 18 | + * @param options.trackWidth Whether to track width |
| 19 | + * @param options.trackHeight Whether to track width |
| 20 | + * @param options.initialWidth An optional initial width in pixels |
| 21 | + * @param options.initialHeight An optional initial height in pixels |
| 22 | + */ |
| 23 | +export const usePersistedTextAreaSize = (id: string, ref: RefObject<HTMLTextAreaElement>, options: Options) => { |
| 24 | + const { dispatch, getState } = useAppStore(); |
| 25 | + |
| 26 | + const onResize = useCallback( |
| 27 | + (size: Partial<Dimensions>) => { |
| 28 | + dispatch(textAreaSizesStateChanged({ id, size })); |
| 29 | + }, |
| 30 | + [dispatch, id] |
| 31 | + ); |
| 32 | + |
| 33 | + const debouncedOnResize = useMemo(() => debounce(onResize, 300), [onResize]); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + const el = ref.current; |
| 37 | + if (!el) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Nothing to do here if we are not tracking anything. |
| 42 | + if (!options.trackHeight && !options.trackWidth) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // Before registering the observer, grab the stored size from state - we may need to restore the size. |
| 47 | + const storedSize = selectUiSlice(getState()).textAreaSizes[id]; |
| 48 | + |
| 49 | + // Prefer to restore the stored size, falling back to initial size if it exists |
| 50 | + if (storedSize?.width !== undefined) { |
| 51 | + el.style.width = `${storedSize.width}px`; |
| 52 | + } else if (options.initialWidth !== undefined) { |
| 53 | + el.style.width = `${options.initialWidth}px`; |
| 54 | + } |
| 55 | + |
| 56 | + if (storedSize?.height !== undefined) { |
| 57 | + el.style.height = `${storedSize.height}px`; |
| 58 | + } else if (options.initialHeight !== undefined) { |
| 59 | + el.style.height = `${options.initialHeight}px`; |
| 60 | + } |
| 61 | + |
| 62 | + let currentHeight = el.offsetHeight; |
| 63 | + let currentWidth = el.offsetWidth; |
| 64 | + |
| 65 | + const resizeObserver = new ResizeObserver(() => { |
| 66 | + // We only want to push the changes if a tracked dimension changes |
| 67 | + let didChange = false; |
| 68 | + const newSize: Partial<Dimensions> = {}; |
| 69 | + |
| 70 | + if (options.trackHeight) { |
| 71 | + if (el.offsetHeight !== currentHeight) { |
| 72 | + didChange = true; |
| 73 | + currentHeight = el.offsetHeight; |
| 74 | + } |
| 75 | + newSize.height = currentHeight; |
| 76 | + } |
| 77 | + |
| 78 | + if (options.trackWidth) { |
| 79 | + if (el.offsetWidth !== currentWidth) { |
| 80 | + didChange = true; |
| 81 | + currentWidth = el.offsetWidth; |
| 82 | + } |
| 83 | + newSize.width = currentWidth; |
| 84 | + } |
| 85 | + |
| 86 | + if (didChange) { |
| 87 | + debouncedOnResize(newSize); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + resizeObserver.observe(el); |
| 92 | + |
| 93 | + return () => { |
| 94 | + debouncedOnResize.cancel(); |
| 95 | + resizeObserver.disconnect(); |
| 96 | + }; |
| 97 | + }, [ |
| 98 | + debouncedOnResize, |
| 99 | + dispatch, |
| 100 | + getState, |
| 101 | + id, |
| 102 | + options.initialHeight, |
| 103 | + options.initialWidth, |
| 104 | + options.trackHeight, |
| 105 | + options.trackWidth, |
| 106 | + ref, |
| 107 | + ]); |
| 108 | +}; |
0 commit comments