import { useQuery } from '@tanstack/react-query' /** 올릴 곳 (관리자 서버가 비밀값을 가진 곳만 나온다): 로컬 백엔드, 킹서버 */ export type Target = { id: 'local' | 'king'; label: string; site: string; api: string } export function useTargets() { return useQuery({ queryKey: ['targets'], queryFn: async () => (await (await fetch('/api/targets')).json()) as Target[], staleTime: Infinity, }) } export type CardSummary = { card_id: number; status: 1 | 2 | 3; post_id?: number; ig_media_id?: string } export async function postCard(target: Target['id'], body: unknown): Promise { const r = await fetch(`/api/post?target=${target}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) const res = await r.json() if (!r.ok || res.result?.success === false) throw new Error(res.error ?? res.msg ?? res.result?.desc ?? `HTTP ${r.status}`) return res } export async function cardAction(target: Target['id'], id: number, what: 'publish' | 'reject' | 'instagram'): Promise { const r = await fetch(`/api/cards/${id}/${what}?target=${target}`, { method: 'POST' }) const res = await r.json() if (!r.ok || res.result?.success === false) throw new Error(res.error ?? res.msg ?? res.result?.desc ?? `HTTP ${r.status}`) return res }