import {getAccessToken} from '@/api'; export type SocialPost = { post_id: string; provider: number; body: string; link_url: string; status: string; approval_expires_at?: string; approval_channel?: string; permalink?: string; posted_at?: string; last_error?: string; account_handle?: string; account_bound: boolean; }; export type SocialState = { posts: SocialPost[]; connection_enabled: boolean; posting_enabled: boolean; account: {handle: string; profile_url: string; status: string} | null; }; const base = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:9800'; const messages: Record = { SOCIAL_PUBLISHED_FIXED_URL_REQUIRED: '홈페이지 주소를 확정하고 발행한 뒤 이용할 수 있습니다.', NO_GROUNDED_FACTS: '확인된 정보가 아직 없습니다. 가게 정보를 먼저 확인해 주세요.', SOCIAL_POSTING_DISABLED_COPY_AVAILABLE: '자동 게재를 준비 중입니다. 글을 복사해 직접 올릴 수 있습니다.', ACCOUNT_CONNECTION_REQUIRED: 'Threads 계정을 먼저 연결해 주세요.', APPROVAL_NOTIFICATION_FAILED_SCREEN_AVAILABLE: '알림톡을 보내지 못했습니다. 아래 카드에서 승인할 수 있습니다.', PUBLISH_URL_CHANGED: '홈페이지 주소가 변경되었습니다. 기존 승인으로 게재할 수 없습니다.', APPROVAL_NOT_FOUND: '유효하지 않거나 교체된 승인 링크입니다.', PLACE_NOT_FOUND: '가게를 찾을 수 없습니다.', }; export async function socialApi(path: string, data?: unknown, publicAccess = false): Promise { const token = publicAccess ? null : getAccessToken(); const response = await fetch(`${base}/v1/social${path}`, { method: data === undefined ? 'GET' : 'POST', credentials: 'include', referrerPolicy: 'no-referrer', cache: 'no-store', headers: {'Content-Type': 'application/json', ...(token ? {Authorization: `Bearer ${token}`} : {})}, body: data === undefined ? undefined : JSON.stringify(data), }); const result = await response.json(); if (!response.ok) throw new Error(messages[result.detail] ?? '요청을 처리하지 못했습니다. 잠시 후 다시 확인해 주세요.'); return result as T; } export const statusLabels: Record = { DRAFTING: '글 작성 중', DRAFT: '초안', PENDING_APPROVAL: '승인 대기', APPROVED: '승인 완료', POSTING: '게재 중', POSTED: '게재 완료', DECLINED: '게재하지 않음', EXPIRED: '승인 만료', FAILED: '실패', UNKNOWN: '게재 결과 확인 필요', }; export function postStatus(post: SocialPost) { return post.status === 'PENDING_APPROVAL' && post.approval_expires_at && Date.parse(post.approval_expires_at) <= Date.now() ? 'EXPIRED' : post.status; }