2026-05-11 12:26:57 +02:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
|
|
|
|
|
import { apiFetch } from '@/lib/api/client';
|
|
|
|
|
|
|
|
|
|
export interface AggregatedFile {
|
|
|
|
|
id: string;
|
|
|
|
|
filename: string;
|
|
|
|
|
mimeType: string | null;
|
|
|
|
|
createdAt: string;
|
|
|
|
|
folderId: string | null;
|
|
|
|
|
clientId: string | null;
|
|
|
|
|
companyId: string | null;
|
|
|
|
|
yachtId: string | null;
|
2026-05-11 12:44:48 +02:00
|
|
|
signedFromDocumentId: string | null;
|
2026-05-11 12:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AggregatedWorkflow {
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
|
|
|
|
status: string;
|
|
|
|
|
documentType: string;
|
|
|
|
|
updatedAt: string;
|
|
|
|
|
createdAt: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AggregatedGroup<T> {
|
|
|
|
|
label: string;
|
|
|
|
|
source: 'direct' | 'client' | 'company' | 'yacht';
|
|
|
|
|
files?: T[];
|
|
|
|
|
workflows?: T[];
|
|
|
|
|
total: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useAggregatedFiles(
|
|
|
|
|
entityType: 'client' | 'company' | 'yacht' | undefined,
|
|
|
|
|
entityId: string | undefined,
|
|
|
|
|
) {
|
|
|
|
|
return useQuery<AggregatedGroup<AggregatedFile>[]>({
|
|
|
|
|
queryKey: ['files', 'aggregated', entityType, entityId],
|
|
|
|
|
queryFn: () =>
|
|
|
|
|
apiFetch<{ data: { groups: AggregatedGroup<AggregatedFile>[] } }>(
|
|
|
|
|
`/api/v1/files?entityType=${entityType}&entityId=${entityId}`,
|
|
|
|
|
).then((r) => r.data.groups),
|
|
|
|
|
enabled: Boolean(entityType && entityId),
|
|
|
|
|
staleTime: 10_000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useAggregatedWorkflows(
|
|
|
|
|
entityType: 'client' | 'company' | 'yacht' | undefined,
|
|
|
|
|
entityId: string | undefined,
|
|
|
|
|
) {
|
|
|
|
|
return useQuery<AggregatedGroup<AggregatedWorkflow>[]>({
|
|
|
|
|
queryKey: ['documents', 'aggregated', entityType, entityId],
|
|
|
|
|
queryFn: () =>
|
|
|
|
|
apiFetch<{ data: { groups: AggregatedGroup<AggregatedWorkflow>[] } }>(
|
|
|
|
|
`/api/v1/documents?entityType=${entityType}&entityId=${entityId}`,
|
|
|
|
|
).then((r) => r.data.groups),
|
|
|
|
|
enabled: Boolean(entityType && entityId),
|
|
|
|
|
staleTime: 10_000,
|
|
|
|
|
});
|
|
|
|
|
}
|