Hey is a decentralized and permissionless social media app built with Lens Protocol 🌿

Refactor local storage clearing helper (#5807)

authored by yoginth.com and committed by

GitHub a6f7bf50 dd8c9f01

+44 -8
+2 -1
apps/web/src/components/Shared/SiteError.tsx
··· 1 1 import { Button, H3 } from "@/components/Shared/UI"; 2 + import clearLocalStorage from "@/helpers/clearLocalStorage"; 2 3 3 4 interface SiteErrorProps { 4 5 message?: string; ··· 6 7 7 8 const SiteError = ({ message }: SiteErrorProps) => { 8 9 const clearLocalData = () => { 9 - localStorage.clear(); 10 + clearLocalStorage(); 10 11 setTimeout(() => location.reload(), 200); 11 12 }; 12 13
+27
apps/web/src/helpers/clearLocalStorage.test.ts
··· 1 + import { beforeEach, describe, expect, it, vi } from "vitest"; 2 + import clearLocalStorage from "./clearLocalStorage"; 3 + import { Localstorage } from "@hey/data/storage"; 4 + 5 + describe("clearLocalStorage", () => { 6 + beforeEach(() => { 7 + (global as any).localStorage = { 8 + removeItem: vi.fn() 9 + }; 10 + }); 11 + 12 + it("removes all keys except search store", () => { 13 + clearLocalStorage(); 14 + 15 + const stores = Object.values(Localstorage).filter( 16 + (s) => s !== Localstorage.SearchStore 17 + ); 18 + 19 + for (const store of stores) { 20 + expect((global as any).localStorage.removeItem).toHaveBeenCalledWith(store); 21 + } 22 + 23 + expect((global as any).localStorage.removeItem).toHaveBeenCalledTimes( 24 + stores.length 25 + ); 26 + }); 27 + });
+13
apps/web/src/helpers/clearLocalStorage.ts
··· 1 + import { Localstorage } from "@hey/data/storage"; 2 + 3 + const clearLocalStorage = (): void => { 4 + const storesToClear = Object.values(Localstorage).filter( 5 + (store) => store !== Localstorage.SearchStore 6 + ); 7 + 8 + for (const store of storesToClear) { 9 + localStorage.removeItem(store); 10 + } 11 + }; 12 + 13 + export default clearLocalStorage;
+2 -7
apps/web/src/store/persisted/useAuthStore.ts
··· 1 1 import { createPersistedTrackedStore } from "@/store/createTrackedStore"; 2 2 import { Localstorage } from "@hey/data/storage"; 3 + import clearLocalStorage from "@/helpers/clearLocalStorage"; 3 4 4 5 interface Tokens { 5 6 accessToken: null | string; ··· 28 29 signIn: ({ accessToken, refreshToken }) => 29 30 set({ accessToken, refreshToken }), 30 31 signOut: async () => { 31 - // Clear Localstorage 32 - const allLocalstorageStores = Object.values(Localstorage).filter( 33 - (value) => value !== Localstorage.SearchStore 34 - ); 35 - for (const store of allLocalstorageStores) { 36 - localStorage.removeItem(store); 37 - } 32 + clearLocalStorage(); 38 33 } 39 34 }), 40 35 { name: Localstorage.AuthStore }