51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
/**
|
|
* Placeholder - will be extracted from frontend/src/hooks/
|
|
*/
|
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
interface WikidataEntity {
|
|
id: string;
|
|
label: string;
|
|
description?: string;
|
|
claims: Record<string, unknown>;
|
|
}
|
|
|
|
interface WikidataOptions {
|
|
language?: string;
|
|
enabled?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Hook for fetching Wikidata entity data
|
|
*/
|
|
export function useWikidata(entityId: string, options: WikidataOptions = {}) {
|
|
const { language = 'en', enabled = true } = options;
|
|
|
|
return useQuery({
|
|
queryKey: ['wikidata', entityId, language],
|
|
queryFn: async (): Promise<WikidataEntity | null> => {
|
|
if (!entityId) return null;
|
|
|
|
const response = await fetch(
|
|
`https://www.wikidata.org/wiki/Special:EntityData/${entityId}.json`
|
|
);
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Wikidata fetch failed: ${response.statusText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
const entity = data.entities[entityId];
|
|
|
|
return {
|
|
id: entity.id,
|
|
label: entity.labels?.[language]?.value || entity.id,
|
|
description: entity.descriptions?.[language]?.value,
|
|
claims: entity.claims || {},
|
|
};
|
|
},
|
|
enabled: enabled && !!entityId,
|
|
staleTime: 1000 * 60 * 60 * 24, // Cache for 24 hours
|
|
});
|
|
}
|