- Replace Proxy-based useTranslation hook with thin react-i18next wrapper
- Convert all t.section.key property access to t('section.key') function calls across 84 files
- Migrate TranslationKeys type parameters to TFunction from i18next
- Update test setup mock and test assertions for new pattern
- Preserve setLanguage with language change events for loading overlay
Closes #579
112 lines
No EOL
3.3 KiB
TypeScript
112 lines
No EOL
3.3 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { notebooksApi } from '@/lib/api/notebooks'
|
|
import { QUERY_KEYS } from '@/lib/api/query-client'
|
|
import { useToast } from '@/lib/hooks/use-toast'
|
|
import { useTranslation } from '@/lib/hooks/use-translation'
|
|
import { getApiErrorKey } from '@/lib/utils/error-handler'
|
|
import { CreateNotebookRequest, UpdateNotebookRequest } from '@/lib/types/api'
|
|
|
|
export function useNotebooks(archived?: boolean) {
|
|
return useQuery({
|
|
queryKey: [...QUERY_KEYS.notebooks, { archived }],
|
|
queryFn: () => notebooksApi.list({ archived, order_by: 'updated desc' }),
|
|
})
|
|
}
|
|
|
|
export function useNotebook(id: string) {
|
|
return useQuery({
|
|
queryKey: QUERY_KEYS.notebook(id),
|
|
queryFn: () => notebooksApi.get(id),
|
|
enabled: !!id,
|
|
})
|
|
}
|
|
|
|
export function useCreateNotebook() {
|
|
const queryClient = useQueryClient()
|
|
const { toast } = useToast()
|
|
const { t } = useTranslation()
|
|
|
|
return useMutation({
|
|
mutationFn: (data: CreateNotebookRequest) => notebooksApi.create(data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.notebooks })
|
|
toast({
|
|
title: t('common.success'),
|
|
description: t('notebooks.createSuccess'),
|
|
})
|
|
},
|
|
onError: (error: unknown) => {
|
|
toast({
|
|
title: t('common.error'),
|
|
description: t(getApiErrorKey(error, t('common.error'))),
|
|
variant: 'destructive',
|
|
})
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useUpdateNotebook() {
|
|
const queryClient = useQueryClient()
|
|
const { toast } = useToast()
|
|
const { t } = useTranslation()
|
|
|
|
return useMutation({
|
|
mutationFn: ({ id, data }: { id: string; data: UpdateNotebookRequest }) =>
|
|
notebooksApi.update(id, data),
|
|
onSuccess: (_, { id }) => {
|
|
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.notebooks })
|
|
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.notebook(id) })
|
|
toast({
|
|
title: t('common.success'),
|
|
description: t('notebooks.updateSuccess'),
|
|
})
|
|
},
|
|
onError: (error: unknown) => {
|
|
toast({
|
|
title: t('common.error'),
|
|
description: t(getApiErrorKey(error, t('common.error'))),
|
|
variant: 'destructive',
|
|
})
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useNotebookDeletePreview(id: string, enabled: boolean = false) {
|
|
return useQuery({
|
|
queryKey: [...QUERY_KEYS.notebook(id), 'delete-preview'],
|
|
queryFn: () => notebooksApi.deletePreview(id),
|
|
enabled: !!id && enabled,
|
|
})
|
|
}
|
|
|
|
export function useDeleteNotebook() {
|
|
const queryClient = useQueryClient()
|
|
const { toast } = useToast()
|
|
const { t } = useTranslation()
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
id,
|
|
deleteExclusiveSources = false,
|
|
}: {
|
|
id: string
|
|
deleteExclusiveSources?: boolean
|
|
}) => notebooksApi.delete(id, deleteExclusiveSources),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.notebooks })
|
|
// Also invalidate sources since some may have been deleted
|
|
queryClient.invalidateQueries({ queryKey: ['sources'] })
|
|
toast({
|
|
title: t('common.success'),
|
|
description: t('notebooks.deleteSuccess'),
|
|
})
|
|
},
|
|
onError: (error: unknown) => {
|
|
toast({
|
|
title: t('common.error'),
|
|
description: t(getApiErrorKey(error, t('common.error'))),
|
|
variant: 'destructive',
|
|
})
|
|
},
|
|
})
|
|
} |