@@ -2,7 +2,7 @@ import { useState, useMemo, useEffect, useCallback, useRef } from 'react';
22import { useAuth , useUser } from '@clerk/clerk-react' ;
33import { api , type ApiNote , type ApiFolder } from '@/lib/api/api.ts' ;
44import { fileService } from '@/services/fileService' ;
5- import { clearEncryptionKeys } from '@/lib/encryption' ;
5+ import { clearEncryptionKeys , hasMasterPassword , isMasterPasswordUnlocked } from '@/lib/encryption' ;
66import type { Note , Folder , ViewMode } from '@/types/note' ;
77import { getDescendantIds } from '@/utils/folderTree' ;
88
@@ -39,7 +39,15 @@ export function useNotes() {
3939 try {
4040 setError ( null ) ;
4141 api . setCurrentUser ( clerkUser ! . id ) ;
42- await api . getCurrentUser ( ) ;
42+
43+ // Only call API if user has a master password set up and unlocked
44+ const hasPassword = hasMasterPassword ( clerkUser ! . id ) ;
45+ const isUnlocked = isMasterPasswordUnlocked ( clerkUser ! . id ) ;
46+
47+ if ( hasPassword && isUnlocked ) {
48+ await api . getCurrentUser ( ) ;
49+ }
50+
4351 setEncryptionReady ( true ) ;
4452 } catch ( error ) {
4553 console . error ( 'Failed to initialize user:' , error ) ;
@@ -110,7 +118,13 @@ export function useNotes() {
110118
111119 useEffect ( ( ) => {
112120 if ( encryptionReady && clerkUser ) {
113- void loadData ( ) ;
121+ const hasPassword = hasMasterPassword ( clerkUser . id ) ;
122+ const isUnlocked = isMasterPasswordUnlocked ( clerkUser . id ) ;
123+
124+ // Only load data if user has master password set up and unlocked
125+ if ( hasPassword && isUnlocked ) {
126+ void loadData ( ) ;
127+ }
114128 }
115129 } , [ encryptionReady , clerkUser , loadData ] ) ;
116130
@@ -541,5 +555,11 @@ export function useNotes() {
541555 setSearchQuery,
542556 setFolders,
543557 refetch : loadData ,
558+ reinitialize : async ( ) => {
559+ if ( clerkUser ) {
560+ await initializeUser ( ) ;
561+ await loadData ( ) ;
562+ }
563+ } ,
544564 } ;
545565}
0 commit comments