From a0d176c9525c7aad7166f2ee7f4bb5ec48b24185 Mon Sep 17 00:00:00 2001 From: Dakshesh Jain <65905942+dakshesh14@users.noreply.github.com> Date: Fri, 17 Feb 2023 18:52:16 +0530 Subject: [PATCH] refractor: use local storage hook (#293) * feat: resend login code on signing page after 30 seconds * refractor: use local storage hook * refractor: properly using new local storage hook on modules sidebar --- .../core/sidebar/sidebar-progress-stats.tsx | 6 ++- apps/app/hooks/use-local-storage.tsx | 47 ++++++++++++++----- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/apps/app/components/core/sidebar/sidebar-progress-stats.tsx b/apps/app/components/core/sidebar/sidebar-progress-stats.tsx index 8fb073f54..01417b625 100644 --- a/apps/app/components/core/sidebar/sidebar-progress-stats.tsx +++ b/apps/app/components/core/sidebar/sidebar-progress-stats.tsx @@ -42,7 +42,7 @@ export const SidebarProgressStats: React.FC = ({ groupedIssues, issues }) const router = useRouter(); const { workspaceSlug, projectId } = router.query; - const [tab, setTab] = useLocalStorage("tab", "Assignees"); + const { storedValue: tab, setValue: setTab } = useLocalStorage("tab", "Assignees"); const { data: issueLabels } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId as string) : null, @@ -58,7 +58,7 @@ export const SidebarProgressStats: React.FC = ({ groupedIssues, issues }) : null ); - const currentValue = (tab: string) => { + const currentValue = (tab: string | null) => { switch (tab) { case "Assignees": return 0; @@ -66,6 +66,8 @@ export const SidebarProgressStats: React.FC = ({ groupedIssues, issues }) return 1; case "States": return 2; + default: + return 0; } }; return ( diff --git a/apps/app/hooks/use-local-storage.tsx b/apps/app/hooks/use-local-storage.tsx index 8b863899c..8e2877a65 100644 --- a/apps/app/hooks/use-local-storage.tsx +++ b/apps/app/hooks/use-local-storage.tsx @@ -1,22 +1,43 @@ -import React, { useEffect, useState } from "react"; +import { useState, useEffect, useCallback } from "react"; -const getSavedValue = (key: any, value: any) => { - const savedValue = localStorage.getItem(key); - if (savedValue) { - return savedValue; - } - return value; +const getValueFromLocalStorage = (key: string, defaultValue: any) => { + const value = window.localStorage.getItem(key); + return value ? JSON.parse(value) : defaultValue; }; -const useLocalStorage = (key: any, value: any) => { - const [updatedvalue, seUpdatedvalue] = useState(() => getSavedValue(key, value)); +const useLocalStorage = (key: string, initialValue: T) => { + const [storedValue, setStoredValue] = useState(() => + getValueFromLocalStorage(key, initialValue) + ); + + const setValue = useCallback( + (value: T) => { + window.localStorage.setItem(key, JSON.stringify(value)); + setStoredValue(value); + window.dispatchEvent(new Event(`local-storage:${key}`)); + }, + [key] + ); + + const clearValue = useCallback(() => { + window.localStorage.removeItem(key); + setStoredValue(null); + window.dispatchEvent(new Event(`local-storage:${key}`)); + }, [key]); + + const reHydrate = useCallback(() => { + const data = getValueFromLocalStorage(key, initialValue); + setStoredValue(data); + }, [key, initialValue]); useEffect(() => { - localStorage.setItem(key, updatedvalue); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [updatedvalue]); + window.addEventListener(`local-storage:${key}`, reHydrate); + return () => { + window.removeEventListener(`local-storage:${key}`, reHydrate); + }; + }, [key, reHydrate]); - return [updatedvalue, seUpdatedvalue]; + return { storedValue, setValue, clearValue } as const; }; export default useLocalStorage;