Refactor imports and add auth clearAll

- Remove unused SetStateAction import in SideBar - Add clearAll to
authStore to wipe state, localStorage, and reset notes - Expose
useAuthStore on window for debugging - Update notesStore.updateFolder to
return FolderTreeNode and fix recursion by using the correct child
variable
This commit is contained in:
james fitzsimons 2025-12-13 13:04:13 +00:00
parent b596c9f34d
commit 0a23332a09
3 changed files with 28 additions and 5 deletions

View file

@ -1,4 +1,4 @@
import React, { useState, useRef, useEffect, SetStateAction } from "react"; import React, { useState, useRef, useEffect } from "react";
// @ts-ignore // @ts-ignore
import FolderIcon from "@/assets/fontawesome/svg/folder.svg?react"; import FolderIcon from "@/assets/fontawesome/svg/folder.svg?react";

View file

@ -1,11 +1,13 @@
import { create } from "zustand"; import { create } from "zustand";
import { persist } from "zustand/middleware"; import { persist } from "zustand/middleware";
import { useNoteStore } from "./notesStore";
import { import {
deriveKey, deriveKey,
generateMasterKey, generateMasterKey,
unwrapMasterKey, unwrapMasterKey,
wrapMasterKey, wrapMasterKey,
} from "../api/encryption"; } from "../api/encryption";
import { FolderTree } from "@/pages/Home/components/sidebar/subcomponents/FolderTree";
interface User { interface User {
id: number; id: number;
@ -30,6 +32,7 @@ interface AuthState {
logout: () => Promise<void>; logout: () => Promise<void>;
checkAuth: () => Promise<void>; checkAuth: () => Promise<void>;
initEncryptionKey: (password: string, salt: string) => Promise<void>; initEncryptionKey: (password: string, salt: string) => Promise<void>;
clearAll: () => void;
} }
const API_URL = "http://localhost:8000/api"; const API_URL = "http://localhost:8000/api";
@ -115,9 +118,10 @@ export const useAuthStore = create<AuthState>()(
set({ set({
user: null, user: null,
encryptionKey: null, // Wipe from memory encryptionKey: null,
isAuthenticated: false, isAuthenticated: false,
}); });
get().clearAll();
}, },
checkAuth: async () => { checkAuth: async () => {
@ -137,6 +141,22 @@ export const useAuthStore = create<AuthState>()(
get().logout(); get().logout();
} }
}, },
clearAll: () => {
set({
user: null,
encryptionKey: null,
isAuthenticated: false,
rememberMe: false,
});
localStorage.clear();
useNoteStore.setState({
folderTree: null,
selectedFolder: null,
selectedNote: null,
});
},
}), }),
{ {
name: "auth-storage", name: "auth-storage",
@ -149,3 +169,6 @@ export const useAuthStore = create<AuthState>()(
}, },
), ),
); );
if (typeof window !== "undefined") {
(window as any).useAuthStore = useAuthStore;
}

View file

@ -36,15 +36,15 @@ const updateFolder = (
id: number, id: number,
folder: FolderTreeNode, folder: FolderTreeNode,
newFolder: FolderUpdate, newFolder: FolderUpdate,
) => { ): FolderTreeNode => {
if (folder.id === id) { if (folder.id === id) {
return { ...folder, ...newFolder }; return { ...folder, ...newFolder };
} }
if (folder.children) { if (folder.children) {
return { return {
...folder, ...folder,
children: folder.children.map((folder) => children: folder.children.map((child) =>
updateFolder(id, folder, newFolder), updateFolder(id, child, newFolder),
), ),
}; };
} }