File size: 3,939 Bytes
f41aded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23e3fb3
 
 
f41aded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd4b277
 
f41aded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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<string, unknown> = {},
  ) {
    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<T = unknown>(path: string, { method, body }: Options = {}): Promise<T> {
  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' });