export type Role = 'owner' | 'editor' | 'viewer'; export interface User { id: number; email: string; name: string; } export interface ProjectSummary { id: string; title: string; updatedAt: string; ownerName: string; role: Role; } export interface FileInfo { path: string; isBinary: boolean; rev: number; size: number; } export interface Member extends User { role: 'editor' | 'viewer'; } export interface ProjectDetail { project: { id: string; title: string; mainFile: string; role: Role; owner: User }; files: FileInfo[]; members: Member[]; } export interface Problem { level: 'error' | 'warning'; file: string | null; line: number | null; message: string; } export interface CompileResult { ok: boolean; problems: Problem[]; log: string; texLog: string; durationMs: number; hasPdf: boolean; } export interface Version { id: number; label: string; isAuto: boolean; createdAt: string; userName: string | null; } export interface Comment { id: number; parentId: number | null; filePath: string | null; line: number | null; quote: string | null; body: string; resolved: boolean; createdAt: string; userId: number; userName: string; } export interface Citation { id: string; key: string; title: string; authors: string[]; year: number | null; venue: string | null; url: string; bibtex: string; } export interface Template { id: string; name: string; description: string; /** 'genel' ya da 'kurum' (kurumların resmî tez/proje şablonları) */ group: string; institution: string | null; } export interface AiProvider { id: string; label: string; model: string; } export class ApiError extends Error { constructor( public status: number, message: string, public data: Record = {}, ) { super(message); } } interface Options { method?: string; /** JSON olarak gönderilecek gövde ya da dosya yüklemeleri için FormData. */ body?: unknown; } export async function api(path: string, { method, body }: Options = {}): Promise { const isForm = body instanceof FormData; let res: Response; try { res = await fetch(`/api${path}`, { method: method ?? (body === undefined ? 'GET' : 'POST'), // X-Thoth: başka sitelerin oturum çereziyle istek göndermesini (CSRF) engelleyen başlık. headers: body !== undefined && !isForm ? { 'Content-Type': 'application/json', 'X-Thoth': '1' } : { 'X-Thoth': '1' }, body: body === undefined ? undefined : isForm ? body : JSON.stringify(body), }); } catch { throw new ApiError(0, 'Sunucuya ulaşılamıyor. Bağlantınızı kontrol edin.'); } const data = (res.headers.get('content-type') ?? '').includes('json') ? await res.json() : {}; if (!res.ok) throw new ApiError(res.status, data.error ?? `İstek başarısız oldu (${res.status}).`, data); return data as T; } export const q = (path: string) => encodeURIComponent(path); /** SQLite'ın UTC "YYYY-AA-GG SS:DD:SS" biçimindeki zamanını Date'e çevirir. */ export const parseDate = (s: string) => new Date(`${s.replace(' ', 'T')}Z`); const rtf = new Intl.RelativeTimeFormat('tr', { numeric: 'auto' }); export function timeAgo(s: string): string { const seconds = (parseDate(s).getTime() - Date.now()) / 1000; const steps: [number, Intl.RelativeTimeFormatUnit][] = [ [60, 'second'], [3600, 'minute'], [86400, 'hour'], [2592000, 'day'], [31536000, 'month'], ]; if (Math.abs(seconds) < 45) return 'az önce'; let previous = 1; for (const [limit, unit] of steps) { if (Math.abs(seconds) < limit) return rtf.format(Math.round(seconds / previous), unit); previous = limit; } return rtf.format(Math.round(seconds / 31536000), 'year'); } export const formatDate = (s: string) => parseDate(s).toLocaleString('tr-TR', { day: 'numeric', month: 'long', hour: '2-digit', minute: '2-digit' });