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
import FolderIcon from "@/assets/fontawesome/svg/folder.svg?react";

View file

@ -1,11 +1,13 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { useNoteStore } from "./notesStore";
import {
deriveKey,
generateMasterKey,
unwrapMasterKey,
wrapMasterKey,
} from "../api/encryption";
import { FolderTree } from "@/pages/Home/components/sidebar/subcomponents/FolderTree";
interface User {
id: number;
@ -30,6 +32,7 @@ interface AuthState {
logout: () => Promise<void>;
checkAuth: () => Promise<void>;
initEncryptionKey: (password: string, salt: string) => Promise<void>;
clearAll: () => void;
}
const API_URL = "http://localhost:8000/api";
@ -115,9 +118,10 @@ export const useAuthStore = create<AuthState>()(
set({
user: null,
encryptionKey: null, // Wipe from memory
encryptionKey: null,
isAuthenticated: false,
});
get().clearAll();
},
checkAuth: async () => {
@ -137,6 +141,22 @@ export const useAuthStore = create<AuthState>()(
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",
@ -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,
folder: FolderTreeNode,
newFolder: FolderUpdate,
) => {
): FolderTreeNode => {
if (folder.id === id) {
return { ...folder, ...newFolder };
}
if (folder.children) {
return {
...folder,
children: folder.children.map((folder) =>
updateFolder(id, folder, newFolder),
children: folder.children.map((child) =>
updateFolder(id, child, newFolder),
),
};
}