-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhooks.tsx
218 lines (198 loc) · 5.25 KB
/
hooks.tsx
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
import {
queryOptions as queryOptionsV5,
useQuery as useQueryV5,
} from '@tanstack/react-queryV5'
import { useRef } from 'react'
import { useParams, useRouteMatch } from 'react-router'
import { z } from 'zod'
import {
RepoNotFoundErrorSchema,
RepoOwnerNotActivatedErrorSchema,
} from 'services/repo'
import Api from 'shared/api'
import { Provider, rejectNetworkError } from 'shared/api/helpers'
import A from 'ui/A'
import { eventTracker } from './events'
import { EventContext } from './types'
// Hook to keep the global EventTracker's context up-to-date.
export function useEventContext() {
const { provider, owner, repo } = useParams<{
provider: Provider
owner?: string
repo?: string
}>()
const context = useRef<EventContext>({})
const { path } = useRouteMatch()
const { data: ownerData } = useQueryV5(
OwnerContextQueryOpts({ provider, owner })
)
const { data: repoData } = useQueryV5(
RepoContextQueryOpts({ provider, owner, repo })
)
if (
path !== context.current.path ||
ownerData?.ownerid !== context.current.ownerid ||
repoData?.repoid !== context.current.repoid
) {
// only update if this is a new owner or repo
const newContext: EventContext = {
path,
ownerid: ownerData?.ownerid || undefined,
repoid: repoData?.repoid || undefined,
repoIsPrivate:
typeof repoData?.private === 'boolean' ? repoData?.private : undefined,
}
eventTracker().setContext(newContext)
context.current = newContext
}
}
const OwnerContextSchema = z.object({
owner: z
.object({
ownerid: z.number().nullable(),
})
.nullable(),
})
const ownerContextQuery = `
query OwnerContext($owner: String!) {
owner(username: $owner) {
ownerid
}
}
`
interface OwnerContextQueryOptsArgs {
provider: Provider
owner?: string
}
export const OwnerContextQueryOpts = ({
provider,
owner,
}: OwnerContextQueryOptsArgs) =>
queryOptionsV5({
queryKey: ['OwnerContext', provider, owner],
queryFn: ({ signal }) =>
Api.graphql({
provider,
query: ownerContextQuery,
signal,
variables: {
owner,
},
}).then((res) => {
const parsedRes = OwnerContextSchema.safeParse(res.data)
if (!parsedRes.success) {
return rejectNetworkError({
status: 404,
data: {},
dev: 'OwnerContextQueryOpts - 404 Failed to parse data',
error: parsedRes.error,
})
}
return parsedRes.data.owner
}),
enabled: !!owner,
// Fetch this data only once per session
staleTime: Infinity,
gcTime: Infinity,
})
const RepositorySchema = z.object({
__typename: z.literal('Repository'),
repoid: z.number(),
private: z.boolean().nullable(),
})
const RepoContextSchema = z.object({
owner: z
.object({
repository: z
.discriminatedUnion('__typename', [
RepositorySchema,
RepoNotFoundErrorSchema,
RepoOwnerNotActivatedErrorSchema,
])
.nullable(),
})
.nullable(),
})
const repoContextQuery = `
query RepoContext($owner: String!, $repo: String!) {
owner(username: $owner) {
repository(name: $repo) {
__typename
... on Repository {
repoid
private
}
... on NotFoundError {
message
}
... on OwnerNotActivatedError {
message
}
}
}
}
`
interface RepoContextQueryOptsArgs {
provider: Provider
owner?: string
repo?: string
}
export const RepoContextQueryOpts = ({
provider,
owner,
repo,
}: RepoContextQueryOptsArgs) =>
queryOptionsV5({
queryKey: ['RepoContext', provider, owner, repo],
queryFn: ({ signal }) =>
Api.graphql({
provider,
query: repoContextQuery,
signal,
variables: {
owner,
repo,
},
}).then((res) => {
const parsedRes = RepoContextSchema.safeParse(res.data)
if (!parsedRes.success) {
return rejectNetworkError({
status: 404,
data: {},
dev: 'RepoContextQueryOpts - 404 Failed to parse data',
error: parsedRes.error,
})
}
if (parsedRes.data?.owner?.repository?.__typename === 'NotFoundError') {
return rejectNetworkError({
status: 404,
data: {},
dev: 'RepoContextQueryOpts - 404 NotFoundError',
})
}
if (
parsedRes.data?.owner?.repository?.__typename ===
'OwnerNotActivatedError'
) {
return rejectNetworkError({
status: 403,
data: {
detail: (
<p>
Activation is required to view this repo, please{' '}
{/* @ts-expect-error - A hasn't been typed yet */}
<A to={{ pageName: 'membersTab' }}>click here </A> to activate
your account.
</p>
),
},
dev: 'RepoContextQueryOpts - 403 OwnerNotActivatedError',
})
}
return parsedRes.data.owner?.repository
}),
enabled: !!owner && !!repo,
// Fetch this data only once per session
staleTime: Infinity,
gcTime: Infinity,
})