diff --git a/src/components/local-storage-input.tsx b/src/components/local-storage-input.tsx deleted file mode 100644 index 578a0614..00000000 --- a/src/components/local-storage-input.tsx +++ /dev/null @@ -1,90 +0,0 @@ -import React, { useCallback, useEffect, useState } from 'react' -import { InputDescription } from './input-description' -import { InputLabel } from './input-label' - -export interface LocalStorageInputProps extends React.DetailedHTMLProps, HTMLDivElement> { - localStorageKey: string - label: string - placeholder?: string - defaultValue: string - validationFn?(value: string): Error | null - resetKey: number - description?: string - preSaveFormat?(value: string): string - postLoadFormat?(value: string): string -} - -const defaultValidationFunction = (value: string): Error | null => { - try { - value.split('\n') - return null - } catch (err) { - return err as Error - } -} - -const getFromLocalStorage = (postLoadFormat?: (arg0: string) => string) => (key: string, fallback: string) => { - let localStorageValue = localStorage.getItem(key) - if (localStorageValue != null && postLoadFormat != null) { - localStorageValue = postLoadFormat(localStorageValue) - } - return localStorageValue ?? fallback -} - -/** - * A Local storage input (text area) component that saves the input to local storage. - */ -export default ({ resetKey, localStorageKey, label, placeholder, validationFn, defaultValue, description, preSaveFormat, postLoadFormat, ...props }: LocalStorageInputProps): JSX.Element => { - const localStorageLoadFn = getFromLocalStorage(postLoadFormat) - const [value, setValue] = useState(localStorageLoadFn(localStorageKey, defaultValue)) - const [error, setError] = useState(null) - - useEffect(() => { - setValue(localStorageLoadFn(localStorageKey, defaultValue)) - }, [resetKey]) - - if (validationFn == null) { - validationFn = defaultValidationFunction - } - - const updateValue = useCallback((e: React.ChangeEvent) => { - setValue(e.target.value) - }, []) - - const saveValue = useCallback((e: React.ChangeEvent) => { - const newValue = e.target.value - try { - const err = validationFn?.(newValue) - if (err != null) { - throw err - } - setValue(newValue) - localStorage.setItem(localStorageKey, preSaveFormat?.(newValue) ?? newValue) - setError(null) - } catch (err) { - setError(err as Error) - } - }, [validationFn, localStorageKey, preSaveFormat]) - - props = { - ...props, - className: `${props.className ?? ''} flex-column items-start` - } - - return ( -
- - -