Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

changes.tsx

199 lines6.1 KB

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 { timeAgo } from '#/lib/time.ts'
17​
18interface ChangesSearch {
19 cursor?: string
20 provider?: string
22​
23interface ChangeRow {
24 id: string
25 type: string
26 providerId: string
27 subjectId: string
28 summary: string
29 createdAt: number
31​
32interface ChangesData {
33 count: number
34 changes: Array<ChangeRow>
35 nextCursor: string | null
37​
38const getChanges = createServerFn({ method: 'GET' })
39 .inputValidator((query: ChangesSearch) => query)
40 .handler(async ({ data }): Promise<ChangesData> => {
41 const { env } = await import('cloudflare:workers')
42 const { getDb } = await import('#/db/index.ts')
43 const { listChanges } = await import('#/server/changes-api.ts')
44 const outcome = await listChanges(getDb(env), { ...data, limit: 50 })
45 if (!outcome.ok) return { count: 0, changes: [], nextCursor: null }
46 return {
47 count: outcome.result.count,
48 nextCursor: outcome.result.nextCursor,
49 changes: outcome.result.changes.map((c) => ({
50 id: c.id,
51 type: c.type,
52 providerId: c.providerId,
53 subjectId: c.subjectId,
54 summary: c.summary,
55 createdAt: c.createdAt,
56 })),
57 }
58 })
59​
60export const Route = createFileRoute('/changes')({
61 validateSearch: (search: Record<string, unknown>): ChangesSearch => ({
62 cursor:
63 typeof search.cursor === 'string' && search.cursor !== ''
64 ? search.cursor
65 : undefined,
66 provider:
67 typeof search.provider === 'string' && search.provider !== ''
68 ? search.provider
69 : undefined,
70 }),
71 loaderDeps: ({ search }) => search,
72 loader: ({ deps }) => getChanges({ data: deps }),
73 component: Changes,
74})
75​
76function Changes() {
77 const data = Route.useLoaderData()
78 const search = Route.useSearch()
79 const [view, setView] = useState<ResourceView>('readable')
80​
81 const params = new URLSearchParams()
82 if (search.provider) params.set('provider', search.provider)
83 if (search.cursor) params.set('cursor', search.cursor)
84 const qs = params.toString() === '' ? '' : `?${params.toString()}`
85 const apiPath = `/v1/changes${qs}`
86 const jsonBody = JSON.stringify(
87 { count: data.count, changes: data.changes, nextCursor: data.nextCursor },
88 null,
89 2,
90 )
91​
92 const olderParams = new URLSearchParams()
93 if (search.provider) olderParams.set('provider', search.provider)
94 if (data.nextCursor) olderParams.set('cursor', data.nextCursor)
95​
96 return (
97 <div className="min-h-screen text-ink">
98 <SiteNav active="changes" />
99 <main className="mx-auto max-w-[1080px] px-6 pb-16">
100 <ReqLine
101 path={
102 <>
103 /v1/changes
104 {qs === '' ? null : <span className="text-ink-faint">{qs}</span>}
105 </>
106 }
107 copyUrl={`https://modelschemas.com${apiPath}`}
108 right={<ViewToggle view={view} onChange={setView} />}
109 />
110 <MetaStrip
111 items={[
112 ['count', String(data.count)],
113 ['order', 'newest first'],
114 ['page size', '50'],
115 ]}
116 />
117​
118 {view === 'json' ? (
119 <JsonPane
120 title="application/json · 200 OK"
121 json={jsonBody}
122 note={
123 <>
124 identical body to{' '}
125 <code>curl https://modelschemas.com{apiPath}</code>
126 </>
127 }
128 />
129 ) : (
130 <>
131 <div className="figure overflow-x-auto">
132 <table className="dtable">
133 <thead>
134 <tr>
135 <th>when</th>
136 <th>type</th>
137 <th>summary</th>
138 <th className="max-sm:hidden">provider</th>
139 </tr>
140 </thead>
141 <tbody>
142 {data.changes.length === 0 ? (
143 <tr>
144 <td colSpan={4} className="text-ink-faint">
145 no changes yet — the next cron poll will populate this
146 feed.
147 </td>
148 </tr>
149 ) : (
150 data.changes.map((change) => (
151 <tr key={change.id}>
152 <td className="font-mono text-xs whitespace-nowrap text-ink-faint">
153 {timeAgo(change.createdAt)}
154 </td>
155 <td
156 className={`font-mono text-xs whitespace-nowrap ${CHANGE_STYLES[change.type] ?? 'text-ink-soft'}`}
157 >
158 {change.type}
159 </td>
160 <td className="max-w-[38em] truncate">
161 <ChangeSummary change={change} />
162 </td>
163 <td className="font-mono text-xs text-ink-faint max-sm:hidden">
164 <a
165 className="press-link"
166 href={`/changes?provider=${change.providerId}`}
167 >
168 {change.providerId}
169 </a>
170 </td>
171 </tr>
172 ))
173 )}
174 </tbody>
175 </table>
176 </div>
177 <div className="mt-2.5 flex flex-wrap gap-4 font-mono text-xs">
178 {data.nextCursor === null ? null : (
179 <a
180 className="press-link"
181 href={`/changes?${olderParams.toString()}`}
182 >
183 older →
184 </a>
185 )}
186 <span className="ml-auto text-ink-faint">
187 webhook push:{' '}
188 <a className="press-link" href="/docs">
189 POST /v1/subscriptions
190 </a>
191 </span>
192 </div>
193 </>
194 )}
195 </main>
196 <SiteFooter />
197 </div>
198 )