better iterface
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { api } from '../api/client';
|
||||
import type { FileDto, ListFilesParams } from '../api/types';
|
||||
|
||||
export function useFiles(params?: ListFilesParams) {
|
||||
return useQuery({
|
||||
queryKey: ['files', params],
|
||||
queryFn: () => api.listFiles(params),
|
||||
});
|
||||
}
|
||||
|
||||
export function useFile(id: string) {
|
||||
return useQuery({
|
||||
queryKey: ['files', id],
|
||||
queryFn: () => api.getFile(id),
|
||||
enabled: id.length > 0,
|
||||
});
|
||||
}
|
||||
|
||||
export function useFileTags(id: string) {
|
||||
return useQuery({
|
||||
queryKey: ['files', id, 'tags'],
|
||||
queryFn: async (): Promise<string[]> => {
|
||||
const { data } = await api.getFile(id);
|
||||
return data.tags ?? [];
|
||||
},
|
||||
enabled: id.length > 0,
|
||||
});
|
||||
}
|
||||
|
||||
export type { FileDto };
|
||||
@@ -0,0 +1,14 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { api } from '../api/client';
|
||||
import type { FileDto } from '../api/types';
|
||||
|
||||
export function useSearch(q: string, page?: number, pageSize?: number) {
|
||||
const trimmed = q.trim();
|
||||
return useQuery({
|
||||
queryKey: ['search', trimmed, page, pageSize],
|
||||
queryFn: () => api.searchFiles(trimmed, { page, pageSize }),
|
||||
enabled: trimmed.length > 0,
|
||||
});
|
||||
}
|
||||
|
||||
export type { FileDto };
|
||||
@@ -0,0 +1,40 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { api } from '../api/client';
|
||||
import type { FileDto, OcrJob } from '../api/types';
|
||||
|
||||
export function useUpload() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (input: {
|
||||
file: { uri: string; name: string; mimeType: string };
|
||||
folderId?: string | null;
|
||||
}) => api.uploadFile(input.file, input.folderId),
|
||||
onSuccess: (result) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['files'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['search'] });
|
||||
return result;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useOcrJob(jobId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['ocr', jobId],
|
||||
queryFn: () => api.getOcrJob(jobId!),
|
||||
enabled: Boolean(jobId),
|
||||
refetchInterval: (query) =>
|
||||
query.state.data && query.state.data.data.status === 'done' ? false : 3000,
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateOcrJob() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (fileId: string) => api.createOcrJob(fileId),
|
||||
onSuccess: (result) => {
|
||||
queryClient.setQueryData(['ocr', result.data.id], result.data);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export type { FileDto, OcrJob };
|
||||
Reference in New Issue
Block a user