refactor: note-editor-context uses invalidateQueries on save/copy
Replace triggerRefresh() with targeted invalidateQueries() for: - handleSave: invalidate note + notes list for notebookId - handleSaveInPlace: invalidate note + notes list for notebookId - handleMakeCopy: invalidate notes list for current notebook Keeps triggerRefresh() for backward compat until fully migrated. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { createContext, useContext, useState, useEffect, useRef, useMemo, useCallback, ReactNode } from 'react'
|
import { createContext, useContext, useState, useEffect, useRef, useMemo, useCallback, ReactNode } from 'react'
|
||||||
|
import { useQueryClient } from '@tanstack/react-query'
|
||||||
import { Note, CheckItem, NOTE_COLORS, NoteColor, LinkMetadata, NoteType, NoteSize } from '@/lib/types'
|
import { Note, CheckItem, NOTE_COLORS, NoteColor, LinkMetadata, NoteType, NoteSize } from '@/lib/types'
|
||||||
import { updateNote, createNote, cleanupOrphanedImages, leaveSharedNote, deleteNote } from '@/app/actions/notes'
|
import { updateNote, createNote, cleanupOrphanedImages, leaveSharedNote, deleteNote } from '@/app/actions/notes'
|
||||||
import { fetchLinkMetadata } from '@/app/actions/scrape'
|
import { fetchLinkMetadata } from '@/app/actions/scrape'
|
||||||
@@ -13,6 +14,7 @@ import { useLanguage } from '@/lib/i18n'
|
|||||||
import { useSession } from 'next-auth/react'
|
import { useSession } from 'next-auth/react'
|
||||||
import { getAISettings } from '@/app/actions/ai-settings'
|
import { getAISettings } from '@/app/actions/ai-settings'
|
||||||
import { extractImagesFromHTML } from '@/lib/utils'
|
import { extractImagesFromHTML } from '@/lib/utils'
|
||||||
|
import { queryKeys } from '@/lib/query-keys'
|
||||||
import type { TitleSuggestion } from '@/hooks/use-title-suggestions'
|
import type { TitleSuggestion } from '@/hooks/use-title-suggestions'
|
||||||
import type { TagSuggestion } from '@/lib/ai/types'
|
import type { TagSuggestion } from '@/lib/ai/types'
|
||||||
import type { NoteEditorState, NoteEditorActions, NoteEditorContextValue } from './types'
|
import type { NoteEditorState, NoteEditorActions, NoteEditorContextValue } from './types'
|
||||||
@@ -29,6 +31,7 @@ interface NoteEditorProviderProps {
|
|||||||
export function NoteEditorProvider({ note, readOnly = false, fullPage = false, children }: NoteEditorProviderProps) {
|
export function NoteEditorProvider({ note, readOnly = false, fullPage = false, children }: NoteEditorProviderProps) {
|
||||||
const { data: session } = useSession()
|
const { data: session } = useSession()
|
||||||
const { t } = useLanguage()
|
const { t } = useLanguage()
|
||||||
|
const queryClient = useQueryClient()
|
||||||
const { labels: globalLabels, addLabel, refreshLabels, setNotebookId: setContextNotebookId, notebooks } = useNotebooks()
|
const { labels: globalLabels, addLabel, refreshLabels, setNotebookId: setContextNotebookId, notebooks } = useNotebooks()
|
||||||
const { triggerRefresh } = useNoteRefresh()
|
const { triggerRefresh } = useNoteRefresh()
|
||||||
|
|
||||||
@@ -539,6 +542,9 @@ export function NoteEditorProvider({ note, readOnly = false, fullPage = false, c
|
|||||||
}
|
}
|
||||||
|
|
||||||
await refreshLabels()
|
await refreshLabels()
|
||||||
|
// Invalidate note and notes list cache
|
||||||
|
queryClient.invalidateQueries({ queryKey: queryKeys.note(note.id) })
|
||||||
|
queryClient.invalidateQueries({ queryKey: queryKeys.notes(note.notebookId) })
|
||||||
triggerRefresh()
|
triggerRefresh()
|
||||||
|
|
||||||
// Note: onClose is handled by the composition component
|
// Note: onClose is handled by the composition component
|
||||||
@@ -615,6 +621,8 @@ export function NoteEditorProvider({ note, readOnly = false, fullPage = false, c
|
|||||||
size: size,
|
size: size,
|
||||||
})
|
})
|
||||||
toast.success(t('notes.copySuccess'))
|
toast.success(t('notes.copySuccess'))
|
||||||
|
// Invalidate notes list cache for current notebook
|
||||||
|
queryClient.invalidateQueries({ queryKey: queryKeys.notes(note.notebookId) })
|
||||||
triggerRefresh()
|
triggerRefresh()
|
||||||
// Note: onClose is handled by the composition component
|
// Note: onClose is handled by the composition component
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -644,6 +652,9 @@ export function NoteEditorProvider({ note, readOnly = false, fullPage = false, c
|
|||||||
cleanupOrphanedImages(removedImageUrls, note.id).catch(() => {})
|
cleanupOrphanedImages(removedImageUrls, note.id).catch(() => {})
|
||||||
}
|
}
|
||||||
await refreshLabels()
|
await refreshLabels()
|
||||||
|
// Invalidate note and notes list cache
|
||||||
|
queryClient.invalidateQueries({ queryKey: queryKeys.note(note.id) })
|
||||||
|
queryClient.invalidateQueries({ queryKey: queryKeys.notes(note.notebookId) })
|
||||||
triggerRefresh()
|
triggerRefresh()
|
||||||
setIsDirty(false)
|
setIsDirty(false)
|
||||||
toast.success('Note sauvegardée !')
|
toast.success('Note sauvegardée !')
|
||||||
|
|||||||
Reference in New Issue
Block a user