perf: eliminate N+1 getNoteAllUsers calls and merge loadSettings+loadNotes to fix double-render cascade

- Skip getNoteAllUsers server action for for own notes (majority of notes), Only fetch collaborators for shared notes.
- Merge loadSettings and loadNotes into single useEffect to prevent double loadNotes trigger when showRecentNotes state changes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Sepehr Ramezani
2026-04-01 22:27:29 +02:00
parent 377f0c739f
commit 18ed116e0d
2 changed files with 28 additions and 18 deletions

View File

@@ -152,23 +152,20 @@ export default function HomePage() {
// Enable reminder notifications
useReminderCheck(notes)
// Load user settings to check if recent notes should be shown
// Load settings + notes in a single effect to avoid cascade re-renders
useEffect(() => {
const loadSettings = async () => {
const load = async () => {
// Load settings first
let showRecent = true
try {
const settings = await getAISettings()
// Default to true if setting is undefined or null
setShowRecentNotes(settings?.showRecentNotes !== false)
showRecent = settings?.showRecentNotes !== false
} catch (error) {
// Default to true on error
setShowRecentNotes(true)
}
}
loadSettings()
}, [refreshKey])
setShowRecentNotes(showRecent)
useEffect(() => {
const loadNotes = async () => {
// Then load notes
setIsLoading(true)
const search = searchParams.get('search')?.trim() || null
const semanticMode = searchParams.get('semantic') === 'true'
@@ -214,7 +211,7 @@ export default function HomePage() {
setPinnedNotes(pinnedFilter)
// Derive recent notes from already-fetched allNotes (no extra server call)
if (showRecentNotes) {
if (showRecent) {
const sevenDaysAgo = new Date()
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7)
sevenDaysAgo.setHours(0, 0, 0, 0)
@@ -240,9 +237,9 @@ export default function HomePage() {
setIsLoading(false)
}
loadNotes()
load()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchParams, refreshKey, showRecentNotes]) // Intentionally omit 'labels' and 'semantic' to prevent reload when adding tags or from router.push
}, [searchParams, refreshKey]) // Intentionally omit 'labels' to prevent reload when adding tags
// Get notebooks context to display header
const { notebooks } = useNotebooks()
const currentNotebook = notebooks.find((n: any) => n.id === searchParams.get('notebook'))