Multi-tenant factory inspection system (SpiFox, Enkid, Alpet): - FastAPI backend with JWT auth, PostgreSQL (asyncpg) - Next.js 16 frontend with App Router, SWR data fetching - Machines CRUD with equipment parts management - Part lifecycle tracking (hours/count/date) with counters - Partial unique index for soft-delete support - 24 pytest tests passing, E2E verified Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
78 lines
1.8 KiB
TypeScript
78 lines
1.8 KiB
TypeScript
import useSWR from 'swr';
|
|
import { fetcher, getTenantUrl } from './api';
|
|
import type { Tenant, Machine, MachineDetail, EquipmentPart } from './types';
|
|
|
|
export function useTenants() {
|
|
const { data, error, isLoading, mutate } = useSWR<{ tenants: Tenant[] }>(
|
|
'/api/tenants',
|
|
fetcher,
|
|
);
|
|
|
|
return {
|
|
tenants: data?.tenants || [],
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useTenantData<T>(path: string, tenantId?: string) {
|
|
const url = tenantId ? getTenantUrl(path, tenantId) : null;
|
|
const { data, error, isLoading, mutate } = useSWR<T>(url, fetcher);
|
|
|
|
return {
|
|
data: data ?? null,
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useMachines(tenantId?: string) {
|
|
const url = tenantId ? `/api/${tenantId}/machines` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<{ machines: Machine[] }>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 30000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
machines: data?.machines || [],
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useMachine(tenantId?: string, machineId?: string) {
|
|
const url = tenantId && machineId ? `/api/${tenantId}/machines/${machineId}` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<MachineDetail>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 30000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
machine: data ?? null,
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|
|
|
|
export function useEquipmentParts(tenantId?: string, machineId?: string) {
|
|
const url = tenantId && machineId ? `/api/${tenantId}/machines/${machineId}/parts` : null;
|
|
const { data, error, isLoading, mutate } = useSWR<{ parts: EquipmentPart[] }>(
|
|
url,
|
|
fetcher,
|
|
{ refreshInterval: 30000, dedupingInterval: 2000 },
|
|
);
|
|
|
|
return {
|
|
parts: data?.parts || [],
|
|
error,
|
|
isLoading,
|
|
mutate,
|
|
};
|
|
}
|