From 536043a26421c32a35bf66244057000456ec3c9a Mon Sep 17 00:00:00 2001 From: malmen237 Date: Mon, 2 Dec 2024 09:27:03 +0100 Subject: [PATCH] feat: added local-storage-hook, minor lint-fix --- .../landing-page/join-production.tsx | 19 +++++++++---- .../landing-page/use-access-local-storage.ts | 28 +++++++++++++++++++ .../production-line/production-line.tsx | 3 +- 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/components/landing-page/use-access-local-storage.ts diff --git a/src/components/landing-page/join-production.tsx b/src/components/landing-page/join-production.tsx index 3c3642c9..ea3836fb 100644 --- a/src/components/landing-page/join-production.tsx +++ b/src/components/landing-page/join-production.tsx @@ -19,6 +19,7 @@ import { darkText, errorColour } from "../../css-helpers/defaults.ts"; import { TJoinProductionOptions } from "../production-line/types.ts"; import { uniqBy } from "../../helpers.ts"; import { FormInputWithLoader } from "./form-input-with-loader.tsx"; +import { useLocalStorage } from "./use-access-local-storage.ts"; type FormValues = TJoinProductionOptions; @@ -47,6 +48,12 @@ export const JoinProduction = ({ addAdditionalCallId, }: TProps) => { const [joinProductionId, setJoinProductionId] = useState(null); + const [localStorageType, setLocalStorageType] = useState(null); + const [localStorageData, setLocalStorageData] = useState(null); + const cachedUsername = useLocalStorage({ + localStorageType, + localStorageData, + }); const { formState: { errors, isValid }, register, @@ -58,7 +65,7 @@ export const JoinProduction = ({ productionId: preSelected?.preSelectedProductionId || addAdditionalCallId || "", lineId: preSelected?.preSelectedLineId || undefined, - username: window.localStorage?.getItem("username") || "", + username: cachedUsername || "", }, resetOptions: { keepDirtyValues: true, // user-interacted input will be retained @@ -74,6 +81,10 @@ export const JoinProduction = ({ loading, } = useFetchProduction(joinProductionId); + useEffect(() => { + setLocalStorageType("username"); + }, []); + // Update selected line id when a new production is fetched useEffect(() => { // Don't run this hook if we have pre-selected values @@ -96,8 +107,6 @@ export const JoinProduction = ({ // Use local cache useEffect(() => { - const cachedUsername = window.localStorage?.getItem("username"); - if (cachedUsername) { setValue("username", cachedUsername); } @@ -105,7 +114,7 @@ export const JoinProduction = ({ if (addAdditionalCallId) { setValue("productionId", addAdditionalCallId); } - }, [addAdditionalCallId, setValue]); + }, [addAdditionalCallId, cachedUsername, setValue]); // If user selects a production from the productionlist useEffect(() => { @@ -124,7 +133,7 @@ export const JoinProduction = ({ const onSubmit: SubmitHandler = (payload) => { if (payload.username) { - window.localStorage?.setItem("username", payload.username); + setLocalStorageData(payload.username); } dispatch({ diff --git a/src/components/landing-page/use-access-local-storage.ts b/src/components/landing-page/use-access-local-storage.ts new file mode 100644 index 00000000..5a8d65fa --- /dev/null +++ b/src/components/landing-page/use-access-local-storage.ts @@ -0,0 +1,28 @@ +import { useEffect, useState } from "react"; + +type TLocalStorage = { + localStorageType: string | null; + localStorageData: string | null; +}; + +export const useLocalStorage = ({ + localStorageType, + localStorageData, +}: TLocalStorage) => { + const [cachedData, setCachedData] = useState(null); + + useEffect(() => { + if (localStorageType) { + setCachedData(window.localStorage.getItem(localStorageType)); + } + }, [localStorageType]); + + useEffect(() => { + if (localStorageType && localStorageData) { + window.localStorage.setItem(localStorageType, localStorageData); + setCachedData(localStorageData); + } + }, [localStorageData, localStorageType]); + + return cachedData; +}; diff --git a/src/components/production-line/production-line.tsx b/src/components/production-line/production-line.tsx index 6ccc0ca2..5d7a1b75 100644 --- a/src/components/production-line/production-line.tsx +++ b/src/components/production-line/production-line.tsx @@ -146,7 +146,8 @@ export const ProductionLine = ({ speakerHotkey: "n", pressToTalkHotkey: "t", }); - const { joinProductionOptions, dominantSpeaker, audioLevelAboveThreshold } = callState; + const { joinProductionOptions, dominantSpeaker, audioLevelAboveThreshold } = + callState; const inputAudioStream = useAudioInput({ inputId: joinProductionOptions?.audioinput ?? null,