Files
tolaria/src/hooks/useEntryActions.ts
lucaronin 5bbd1469e2 fix: resolve TypeScript build errors in refactored code and test mocks
- Remove unused cn import from NoteList.tsx
- Fix SidebarSelection type narrowing in NoteList entity view
- Fix SortableSection onToggle type in Sidebar Omit type
- Fix TypeRow isA prop to accept null
- Align useEntryActions handleUpdateFrontmatter value type
- Add missing trashed/trashedAt/order to test mock VaultEntry objects

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 11:00:07 +01:00

65 lines
2.9 KiB
TypeScript

import { useCallback } from 'react'
import type { VaultEntry } from '../types'
interface EntryActionsConfig {
entries: VaultEntry[]
updateEntry: (path: string, updates: Partial<VaultEntry>) => void
handleUpdateFrontmatter: (path: string, key: string, value: string | number | boolean | string[]) => Promise<void>
handleDeleteProperty: (path: string, key: string) => Promise<void>
setToastMessage: (msg: string | null) => void
}
function findTypeEntry(entries: VaultEntry[], typeName: string): VaultEntry | undefined {
return entries.find((e) => e.isA === 'Type' && e.title === typeName)
}
export function useEntryActions({
entries, updateEntry, handleUpdateFrontmatter, handleDeleteProperty, setToastMessage,
}: EntryActionsConfig) {
const handleTrashNote = useCallback(async (path: string) => {
const now = new Date().toISOString().slice(0, 10)
await handleUpdateFrontmatter(path, 'trashed', true)
await handleUpdateFrontmatter(path, 'trashed_at', now)
updateEntry(path, { trashed: true, trashedAt: Date.now() / 1000 })
setToastMessage('Note moved to trash')
}, [handleUpdateFrontmatter, updateEntry, setToastMessage])
const handleRestoreNote = useCallback(async (path: string) => {
await handleUpdateFrontmatter(path, 'trashed', false)
await handleDeleteProperty(path, 'trashed_at')
updateEntry(path, { trashed: false, trashedAt: null })
setToastMessage('Note restored from trash')
}, [handleUpdateFrontmatter, handleDeleteProperty, updateEntry, setToastMessage])
const handleArchiveNote = useCallback(async (path: string) => {
await handleUpdateFrontmatter(path, 'archived', true)
updateEntry(path, { archived: true })
setToastMessage('Note archived')
}, [handleUpdateFrontmatter, updateEntry, setToastMessage])
const handleUnarchiveNote = useCallback(async (path: string) => {
await handleUpdateFrontmatter(path, 'archived', false)
updateEntry(path, { archived: false })
setToastMessage('Note unarchived')
}, [handleUpdateFrontmatter, updateEntry, setToastMessage])
const handleCustomizeType = useCallback((typeName: string, icon: string, color: string) => {
const typeEntry = findTypeEntry(entries, typeName)
if (!typeEntry) return
handleUpdateFrontmatter(typeEntry.path, 'icon', icon)
handleUpdateFrontmatter(typeEntry.path, 'color', color)
updateEntry(typeEntry.path, { icon, color })
}, [entries, handleUpdateFrontmatter, updateEntry])
const handleReorderSections = useCallback((orderedTypes: { typeName: string; order: number }[]) => {
for (const { typeName, order } of orderedTypes) {
const typeEntry = findTypeEntry(entries, typeName)
if (!typeEntry) continue
handleUpdateFrontmatter(typeEntry.path, 'order', order)
updateEntry(typeEntry.path, { order })
}
}, [entries, handleUpdateFrontmatter, updateEntry])
return { handleTrashNote, handleRestoreNote, handleArchiveNote, handleUnarchiveNote, handleCustomizeType, handleReorderSections }
}