Open History to see the last commit
1import { useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { createServerFn } from '@tanstack/react-start'
4
5import {
6 CHANGE_STYLES,
7 ChangeSummary,
8 JsonPane,
9 MetaStrip,
10 ReqLine,
11 SiteFooter,
12 SiteNav,
13 ViewToggle,
14} from '#/components/site.tsx'
15import type { ResourceView } from '#/components/site.tsx'
16import { changeDiffs } from '#/lib/change-diff.ts'
17import type { ChangeDiff } from '#/lib/change-diff.ts'
18import { timeAgo } from '#/lib/time.ts'
19import type { ChangeType } from '#/db/schema.ts'
20
21interface ChangesSearch {
22 cursor?: string
23 provider?: string
24 type?: ChangeType
25}
26
27/** Filter links above the table; `undefined` is every type. */
28const TYPE_FILTERS: Array<[string, ChangeType | undefined]> = [
29 ['all', undefined],
30 ['new models', 'model.added'],
31 ['removed models', 'model.removed'],
32 ['updated models', 'model.updated'],
33 ['row shape', 'catalog.shape_changed'],
34 ['deprecated models', 'model.deprecated'],
35]
36
37interface ChangeRow {
38 id: string
39 type: string
40 providerId: string
41 subjectId: string
42 summary: string
43 createdAt: number
44 /** What a `model.updated` changed; the raw payload stays on the API. */
45 diffs: Array<ChangeDiff>
46}
47
48/** Diffs shown per row before the rest collapse into a count. */
49const SHOWN_DIFFS = 4
50
51interface ChangesData {
52 count: number
53 changes: Array<ChangeRow>
54 nextCursor: string | null
55 /** Epoch seconds the server read the feed; `timeAgo` measures from it. */
56 renderedAt: number
57}
58
59const getChanges = createServerFn({ method: 'GET' })
60 .validator((query: ChangesSearch) => query)
61 .handler(async ({ data }): Promise<ChangesData> => {
62 const { env } = await import('cloudflare:workers')
63 const { getDb } = await import('#/db/index.ts')
64 const { listChanges } = await import('#/server/changes-api.ts')
65 // Same primary as GET /v1/changes: the cursor has to see the latest write.
66 const outcome = await listChanges(getDb(env), { ...data, limit: 50 })
67 const renderedAt = Math.floor(Date.now() / 1000)
68 if (!outcome.ok)
69 return { count: 0, changes: [], nextCursor: null, renderedAt }
70 return {
71 renderedAt,
72 count: outcome.result.count,
73 nextCursor: outcome.result.nextCursor,
74 changes: outcome.result.changes.map((c) => ({
75 id: c.id,
76 type: c.type,
77 providerId: c.providerId,
78 subjectId: c.subjectId,
79 summary: c.summary,
80 createdAt: c.createdAt,
81 diffs: changeDiffs(c.payload),
82 })),
83 }
84 })
85
86export const Route = createFileRoute('/changes')({
87 validateSearch: (search: Record<string, unknown>): ChangesSearch => ({
88 cursor:
89 typeof search.cursor === 'string' && search.cursor !== ''
90 ? search.cursor
91 : undefined,
92 provider:
93 typeof search.provider === 'string' && search.provider !== ''
94 ? search.provider
95 : undefined,
96 type: TYPE_FILTERS.find(([, type]) => type === search.type)?.[1],
97 }),
98 loaderDeps: ({ search }) => search,
99 loader: ({ deps }) => getChanges({ data: deps }),
100 component: Changes,
101})
102
103function Changes() {
104 const data = Route.useLoaderData()
105 const search = Route.useSearch()
106 const [view, setView] = useState<ResourceView>('readable')
107
108 const params = new URLSearchParams()
109 if (search.provider) params.set('provider', search.provider)
110 if (search.type) params.set('type', search.type)
111 if (search.cursor) params.set('cursor', search.cursor)
112 const qs = params.toString() === '' ? '' : `?${params.toString()}`
113 const apiPath = `/v1/changes${qs}`
114 const jsonBody = JSON.stringify(
115 {
116 count: data.count,
117 changes: data.changes.map(({ diffs: _diffs, ...change }) => change),
118 nextCursor: data.nextCursor,
119 },
120 null,
121 2,
122 )
123
124 const olderParams = new URLSearchParams()
125 if (search.provider) olderParams.set('provider', search.provider)
126 if (search.type) olderParams.set('type', search.type)
127 if (data.nextCursor) olderParams.set('cursor', data.nextCursor)
128
129 /** A filter link keeps the provider and starts again at the newest page. */
130 const typeHref = (type: ChangeType | undefined) => {
131 const filter = new URLSearchParams()
132 if (search.provider) filter.set('provider', search.provider)
133 if (type) filter.set('type', type)
134 const query = filter.toString()
135 return query === '' ? '/changes' : `/changes?${query}`
136 }
137
138 return (
139 <div className="min-h-screen text-ink">
140 <SiteNav active="changes" />
141 <main className="mx-auto max-w-[1080px] px-6 pb-16">
142 <ReqLine
143 path={
144 <>
145 /v1/changes
146 {qs === '' ? null : <span className="text-ink-faint">{qs}</span>}
147 </>
148 }
149 copyUrl={`https://modelschemas.com${apiPath}`}
150 right={<ViewToggle view={view} onChange={setView} />}
151 />
152 <MetaStrip
153 items={[
154 ['count', String(data.count)],
155 ['order', 'newest first'],
156 ['page size', '50'],
157 ]}
158 />
159
160 <div className="mb-2.5 flex flex-wrap gap-4 font-mono text-xs">
161 {TYPE_FILTERS.map(([label, type]) =>
162 type === search.type ? (
163 <span key={label} className="text-ink">
164 {label}
165 </span>
166 ) : (
167 <a key={label} className="press-link" href={typeHref(type)}>
168 {label}
169 </a>
170 ),
171 )}
172 </div>
173
174 {view === 'json' ? (
175 <JsonPane
176 title="application/json · 200 OK"
177 json={jsonBody}
178 note={
179 <>
180 identical body to{' '}
181 <code>curl https://modelschemas.com{apiPath}</code>
182 </>
183 }
184 />
185 ) : (
186 <>
187 <div className="figure overflow-x-auto">
188 <table className="dtable">
189 <thead>
190 <tr>
191 <th>when</th>
192 <th>type</th>
193 <th>summary</th>
194 <th className="max-sm:hidden">provider</th>
195 </tr>
196 </thead>
197 <tbody>
198 {data.changes.length === 0 ? (
199 <tr>
200 <td colSpan={4} className="text-ink-faint">
201 {search.type || search.provider
202 ? 'no changes match this filter.'
203 : 'no changes yet — the next cron poll will populate this feed.'}
204 </td>
205 </tr>
206 ) : (
207 data.changes.map((change) => (
208 <tr key={change.id}>
209 <td className="font-mono text-xs whitespace-nowrap text-ink-faint">
210 {timeAgo(change.createdAt, data.renderedAt)}
211 </td>
212 <td
213 className={`font-mono text-xs whitespace-nowrap ${CHANGE_STYLES[change.type] ?? 'text-ink-soft'}`}
214 >
215 {change.type}
216 </td>
217 <td className="max-w-[38em]">
218 <div className="truncate">
219 <ChangeSummary change={change} />
220 </div>
221 {change.diffs.slice(0, SHOWN_DIFFS).map((diff) => (
222 <div
223 key={diff.path}
224 className="truncate font-mono text-xs text-ink-faint"
225 >
226 {diff.path}{' '}
227 <span className="text-tok-red">
228 {diff.before}
229 </span>
230 {' → '}
231 <span className="text-tok-green">
232 {diff.after}
233 </span>
234 </div>
235 ))}
236 {change.diffs.length > SHOWN_DIFFS ? (
237 <div className="font-mono text-xs text-ink-faint">
238 +{change.diffs.length - SHOWN_DIFFS} more
239 </div>
240 ) : null}
241 </td>
242 <td className="font-mono text-xs text-ink-faint max-sm:hidden">
243 <a
244 className="press-link"
245 href={`/changes?provider=${change.providerId}`}
246 >
247 {change.providerId}
248 </a>
249 </td>
250 </tr>
251 ))
252 )}
253 </tbody>
254 </table>
255 </div>
256 <div className="mt-2.5 flex flex-wrap gap-4 font-mono text-xs">
257 {data.nextCursor === null ? null : (
258 <a
259 className="press-link"
260 href={`/changes?${olderParams.toString()}`}
261 >
262 older →
263 </a>
264 )}
265 <span className="ml-auto text-ink-faint">
266 webhook push:{' '}
267 <a className="press-link" href="/docs">
268 POST /v1/subscriptions
269 </a>
270 </span>
271 </div>
272 </>
273 )}
274 </main>
275 <SiteFooter />
276 </div>
277 )
278}