-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathqueries.ts
More file actions
447 lines (424 loc) · 14.2 KB
/
Copy pathqueries.ts
File metadata and controls
447 lines (424 loc) · 14.2 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* Shared query objects for React Query.
*
* These functions return query options compatible with both useQuery() and prefetchQuery().
* They accept a UniversalClient instance, enabling use in both SSR and client contexts.
*/
import type {
HMDomainInfo,
HMCapability,
HMContactRecord,
HMDocumentInfo,
HMInteractionSummaryOutput,
HMQueryBlockInput,
HMQueryBlockPayload,
HMListChangesOutput,
HMListCitationsOutput,
HMListCommentVersionsOutput,
HMListCommentsOutput,
HMListDiscussionsOutput,
HMListDocumentCollaboratorsOutput,
HMMetadataPayload,
HMRawCapability,
HMResource,
HMRole,
UnpackedHypermediaId,
} from '@seed-hypermedia/client/hm-types'
import {HMQueryBlockPayloadSchema, HMQueryResultSchema, HMResourceSchema} from '@seed-hypermedia/client/hm-types'
import type {UniversalClient} from '../universal-client'
import {hmIdPathToEntityQueryPath} from '../utils'
import {hmId} from '../utils/entity-id-url'
import {entityQueryPathToHmIdPath} from '../utils/path-api'
import {queryKeys} from './query-keys'
function rawRoleToHMRole(role?: string): HMRole {
if (role === 'WRITER') return 'writer'
if (role === 'AGENT') return 'agent'
return 'none'
}
function parseTimestamp(ts?: string): {seconds: number; nanos: number} {
if (!ts) return {seconds: 0, nanos: 0}
const ms = new Date(ts).getTime()
return {seconds: Math.floor(ms / 1000), nanos: (ms % 1000) * 1_000_000}
}
function isAbortError(error: unknown): boolean {
return typeof DOMException !== 'undefined' && error instanceof DOMException && error.name === 'AbortError'
}
function rawCapToHMCapability(raw: HMRawCapability): HMCapability | null {
if (!raw.delegate || !raw.account) return null
return {
id: raw.id || '',
accountUid: raw.delegate,
role: rawRoleToHMRole(raw.role),
grantId: hmId(raw.account, {
path: entityQueryPathToHmIdPath(raw.path),
}),
label: raw.label,
createTime: parseTimestamp(raw.createTime),
}
}
/**
* Query options for fetching a resource (document, comment, etc.)
*/
export function queryResource(client: UniversalClient, id: UnpackedHypermediaId | null | undefined) {
const version = id?.version || undefined
const latest = id?.latest || false
return {
queryKey: [queryKeys.ENTITY, id?.id, version, latest] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMResource | null> => {
if (!id) return null
try {
let res = await client.request('Resource', id, {signal})
let republishSourceId: UnpackedHypermediaId | null = null
// Follow redirects automatically so consumers never see {type: 'redirect'}.
// Republish refs are special: they should render the target content while
// preserving the source route/ID, so the omnibar and navigation stay on
// the republished path instead of being treated like a move redirect.
let maxRedirects = 5
while (res?.type === 'redirect' && maxRedirects-- > 0) {
const nextTarget = {
...res.redirectTarget,
hostname: res.redirectTarget.hostname || res.id.hostname || id.hostname,
}
if (res.republish && !republishSourceId) {
republishSourceId = res.id
}
res = await client.request('Resource', nextTarget, {signal})
}
if (res?.type === 'redirect') {
return {type: 'error', id, message: 'Too many redirects while resolving resource'}
}
const parsed = HMResourceSchema.parse(res)
if (republishSourceId && (parsed.type === 'document' || parsed.type === 'comment') && !id.hostname) {
return {
...parsed,
id: republishSourceId,
}
}
return parsed
} catch (e) {
if (isAbortError(e)) throw e
const message = e instanceof Error ? e.message : 'Unknown error'
return {type: 'error', id, message}
}
},
enabled: !!id,
}
}
/**
* Query options for fetching account metadata.
* Returns HMMetadataPayload or null (handles not-found internally).
*/
export function queryAccount(client: UniversalClient, uid: string | null | undefined) {
return {
queryKey: [queryKeys.ACCOUNT, uid] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMMetadataPayload | null> => {
if (!uid) return null
const result = await client.request('Account', uid, {signal})
if (result.type === 'account-not-found') return null
return result
},
enabled: !!uid,
}
}
/**
* Query options for fetching tracked domain information.
* Returns null when the domain is unknown or the lookup fails.
*/
export function queryDomain(client: UniversalClient, domain: string | null | undefined, forceCheck = false) {
return {
queryKey: [queryKeys.DOMAIN, domain, forceCheck] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMDomainInfo | null> => {
if (!domain) return null
try {
return await client.request('GetDomain', forceCheck ? {domain, forceCheck: true} : {domain}, {signal})
} catch (error) {
if (isAbortError(error)) throw error
return null
}
},
enabled: !!domain,
}
}
/**
* Query options for fetching directory listing.
*/
export function queryDirectory(
client: UniversalClient,
id: UnpackedHypermediaId | null | undefined,
mode: 'Children' | 'AllDescendants' = 'Children',
) {
return {
queryKey: [queryKeys.DOC_LIST_DIRECTORY, id?.id, mode] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMDocumentInfo[]> => {
if (!id) return []
const result = await client.request(
'Query',
{
includes: [
{
space: id.uid,
mode,
path: hmIdPathToEntityQueryPath(id.path),
},
],
},
{signal},
)
if (!result) return []
return HMQueryResultSchema.parse(result).results
},
enabled: !!id,
}
}
/**
* Query options for fetching all metadata needed to render a query block.
*/
export function queryQueryBlock(client: UniversalClient, input: HMQueryBlockInput | null | undefined) {
return {
queryKey: [queryKeys.QUERY_BLOCK, input?.query ?? null] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMQueryBlockPayload | null> => {
if (!input) return null
const result = await client.request('QueryBlock', input, {signal})
if (!result) return null
return HMQueryBlockPayloadSchema.parse(result)
},
enabled: !!input,
}
}
/**
* Query options for fetching comments on a target.
*/
export function queryComments(client: UniversalClient, targetId: UnpackedHypermediaId | null | undefined) {
return {
queryKey: [queryKeys.COMMENTS, targetId?.id] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMListCommentsOutput> => {
if (!targetId) throw new Error('ID required')
return await client.request(
'ListComments',
{
targetId,
},
{signal},
)
},
enabled: !!targetId,
}
}
/**
* Query options for fetching the main comments list used by document comment views.
*/
export function queryDocumentComments(client: UniversalClient, targetId: UnpackedHypermediaId) {
return {
queryKey: [queryKeys.DOCUMENT_COMMENTS, targetId] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMListCommentsOutput> => {
try {
return await client.request('ListComments', {targetId}, {signal})
} catch (error) {
console.error('Error fetching comments:', error)
throw error
}
},
retry: 1,
staleTime: 30_000,
}
}
/**
* Query options for fetching grouped discussions on a document or focused comment.
*/
export function queryDocumentDiscussions(client: UniversalClient, targetId: UnpackedHypermediaId, commentId?: string) {
return {
queryKey: [queryKeys.DOCUMENT_DISCUSSION, targetId, commentId] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMListDiscussionsOutput> => {
try {
return await client.request('ListDiscussions', {targetId, commentId}, {signal})
} catch (error) {
console.error('Error fetching discussions:', error)
throw error
}
},
retry: 1,
staleTime: 30_000,
}
}
/**
* Query options for fetching comments that reference a specific document block.
*/
export function queryBlockDiscussions(client: UniversalClient, targetId: UnpackedHypermediaId) {
return {
queryKey: [queryKeys.BLOCK_DISCUSSIONS, targetId] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMListCommentsOutput> => {
try {
return await client.request('ListCommentsByReference', {targetId}, {signal})
} catch (error) {
console.error('Error fetching block discussions:', error)
throw error
}
},
retry: 1,
staleTime: 30_000,
}
}
/**
* Query options for fetching all versions of a comment.
*/
export function queryCommentVersions(client: UniversalClient, commentId: string | null | undefined) {
return {
queryKey: [queryKeys.COMMENT_VERSIONS, commentId] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMListCommentVersionsOutput> => {
return await client.request('ListCommentVersions', {id: commentId!}, {signal})
},
enabled: !!commentId,
useErrorBoundary: false,
staleTime: 60_000,
}
}
/**
* Query options for fetching a comment's reply count.
*/
export function queryCommentReplyCount(client: UniversalClient, id: string) {
return {
queryKey: [queryKeys.COMMENT_REPLY_COUNT, id] as const,
queryFn: ({signal}: {signal?: AbortSignal} = {}) =>
client.request(
'GetCommentReplyCount',
{
id,
},
{signal},
),
retry: 1,
staleTime: 60_000,
refetchOnWindowFocus: false,
}
}
/**
* Query options for fetching citations to a target.
*/
export function queryCitations(client: UniversalClient, targetId: UnpackedHypermediaId | null | undefined) {
return {
queryKey: [queryKeys.CITATIONS, targetId?.id] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMListCitationsOutput> => {
if (!targetId) throw new Error('ID required')
return await client.request(
'ListCitations',
{
targetId,
},
{signal},
)
},
enabled: !!targetId,
}
}
/**
* Query options for fetching changes to a target.
*/
export function queryChanges(client: UniversalClient, targetId: UnpackedHypermediaId | null | undefined) {
return {
queryKey: [queryKeys.CHANGES, targetId?.id] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMListChangesOutput> => {
if (!targetId) throw new Error('ID required')
return await client.request(
'ListChanges',
{
targetId,
},
{signal},
)
},
enabled: !!targetId,
}
}
/**
* Query options for fetching capabilities on a target.
* Returns deduplicated HMCapability[] including a synthetic owner entry.
*/
export function queryCapabilities(client: UniversalClient, targetId: UnpackedHypermediaId | null | undefined) {
return {
queryKey: [queryKeys.CAPABILITIES, targetId?.uid, ...(targetId?.path || [])] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMCapability[]> => {
if (!targetId) throw new Error('ID required')
const result = await client.request('ListCapabilities', {targetId}, {signal})
const visitedCaps = new Set<string>()
const caps: HMCapability[] = []
for (const raw of result.capabilities) {
const key = `${raw.delegate}-${raw.role}`
if (visitedCaps.has(key)) continue
visitedCaps.add(key)
const cap = rawCapToHMCapability(raw)
if (cap) caps.push(cap)
}
caps.push({
id: '_owner',
accountUid: targetId.uid,
role: 'owner',
grantId: hmId(targetId.uid),
label: 'Owner',
createTime: {seconds: 0, nanos: 0},
})
return caps
},
enabled: !!targetId,
}
}
/**
* Query options for fetching collaborators with account metadata for a target.
*/
export function queryDocumentCollaborators(client: UniversalClient, targetId: UnpackedHypermediaId | null | undefined) {
return {
queryKey: [queryKeys.DOCUMENT_COLLABORATORS, targetId?.uid, ...(targetId?.path || [])] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMListDocumentCollaboratorsOutput | null> => {
if (!targetId) return null
return await client.request('ListDocumentCollaborators', {targetId}, {signal})
},
enabled: !!targetId,
}
}
/**
* Query options for fetching interaction summary for a document.
*/
export function queryInteractionSummary(client: UniversalClient, id: UnpackedHypermediaId | null | undefined) {
return {
queryKey: [queryKeys.DOCUMENT_INTERACTION_SUMMARY, id?.id] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMInteractionSummaryOutput> => {
if (!id) {
return {
citations: 0,
comments: 0,
changes: 0,
children: 0,
authorUids: [],
blocks: {},
}
}
return await client.request('InteractionSummary', {id}, {signal})
},
enabled: !!id,
}
}
/**
* Query options for fetching contacts where the given account is the subject.
*/
export function queryContactsOfSubject(client: UniversalClient, uid: string | undefined) {
return {
queryKey: [queryKeys.CONTACTS_SUBJECT, uid] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMContactRecord[]> => {
if (!uid) return []
return client.request('SubjectContacts', uid, {signal})
},
enabled: !!uid,
}
}
/**
* Query options for fetching contacts owned by the given account.
*/
export function queryContactsOfAccount(client: UniversalClient, uid: string | null | undefined) {
return {
queryKey: [queryKeys.CONTACTS_ACCOUNT, uid] as const,
queryFn: async ({signal}: {signal?: AbortSignal} = {}): Promise<HMContactRecord[]> => {
if (!uid) return []
return client.request('AccountContacts', uid, {signal})
},
enabled: !!uid,
}
}