Files
pn-new-crm/src/hooks/use-vocabulary.ts

27 lines
905 B
TypeScript
Raw Normal View History

'use client';
import { useQuery } from '@tanstack/react-query';
import { apiFetch } from '@/lib/api/client';
import { getVocabularyDef, type VocabularyKey } from '@/lib/vocabularies';
interface VocabulariesResponse {
data: Record<string, readonly string[]>;
}
/**
* Fetches the effective per-port vocabulary list for a key. Falls back
* to the shipped defaults when the request hasn't resolved or the key
* is missing from the response. Cached for 5 minutes vocabularies
* change rarely and the admin Vocabularies page invalidates by save.
*/
export function useVocabulary(key: VocabularyKey): readonly string[] {
const def = getVocabularyDef(key);
const { data } = useQuery<VocabulariesResponse>({
queryKey: ['vocabularies'],
queryFn: () => apiFetch<VocabulariesResponse>('/api/v1/vocabularies'),
staleTime: 5 * 60 * 1000,
});
return data?.data[key] ?? def.defaults;
}