-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathApp.tsx
344 lines (324 loc) · 11.2 KB
/
App.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
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
import { useEffect, useState } from "react";
import type { ReactNode } from "react";
import type { TypedDocumentNode } from "@apollo/client";
import { useReactiveVar, gql, useQuery } from "@apollo/client";
import { useMachine } from "@xstate/react";
import { ErrorBoundary } from "react-error-boundary";
import { currentScreen, Screens } from "./components/Layouts/Navigation";
import { Queries } from "./components/Queries/Queries";
import { Mutations } from "./components/Mutations/Mutations";
import { Explorer } from "./components/Explorer/Explorer";
import { Cache } from "./components/Cache/Cache";
import type {
AppQuery,
AppQueryVariables,
ClientQuery,
ClientQueryVariables,
} from "./types/gql";
import { Tabs } from "./components/Tabs";
import { Button } from "./components/Button";
import IconSettings from "@apollo/icons/default/IconSettings.svg";
import IconGitHubSolid from "@apollo/icons/small/IconGitHubSolid.svg";
import { SettingsModal } from "./components/Layouts/SettingsModal";
import Logo from "@apollo/icons/logos/LogoSymbol.svg";
import { BannerAlert } from "./components/BannerAlert";
import { devtoolsMachine } from "./machines/devtoolsMachine";
import { ClientNotFoundModal } from "./components/ClientNotFoundModal";
import { ButtonGroup } from "./components/ButtonGroup";
import {
GitHubIssueLink,
LABELS,
SECTIONS,
} from "./components/GitHubIssueLink";
import { Tooltip } from "./components/Tooltip";
import { Badge } from "./components/Badge";
import { GitHubReleaseHoverCard } from "./components/GitHubReleaseHoverCard";
import { isSnapshotRelease, parseSnapshotRelease } from "./utilities/github";
import { Select } from "./components/Select";
import { Divider } from "./components/Divider";
import { useActorEvent } from "./hooks/useActorEvent";
import { removeClient } from ".";
import { PageError } from "./components/PageError";
import { SidebarLayout } from "./components/Layouts/SidebarLayout";
const APP_QUERY: TypedDocumentNode<AppQuery, AppQueryVariables> = gql`
query AppQuery {
clients {
id
name
}
}
`;
const CLIENT_QUERY: TypedDocumentNode<ClientQuery, ClientQueryVariables> = gql`
query ClientQuery($id: ID!) {
client(id: $id) {
id
version
queries {
total
}
mutations {
total
}
}
}
`;
const ISSUE_BODY = `
${SECTIONS.defaultDescription}
${SECTIONS.reproduction}
${SECTIONS.apolloClientVersion}
${SECTIONS.devtoolsVersion}
`;
const stableEmptyClients: Required<AppQuery["clients"]> = [];
const noop = () => {};
export const App = () => {
const [snapshot, send] = useMachine(
devtoolsMachine.provide({
actions: {
resetStore: async () => {
await apolloClient.clearStore().catch(noop);
refetch().catch(noop);
},
},
})
);
const {
data,
client: apolloClient,
refetch,
} = useQuery(APP_QUERY, { errorPolicy: "all" });
useActorEvent("registerClient", () => {
send({ type: "connect" });
// Unfortunately after we clear the store above, the query ends up "stuck"
// holding onto the old list of clients even if we manually write a cache
// update to properly resolve the list. Instead we refetch the list again to
// force the query to reload to ensure its in sync with the latest values.
refetch();
});
useActorEvent("clientTerminated", (message) => {
// Disconnect if we are terminating the last client. We assume that 1 client
// means we are terminating the selected client
if (clients.length === 1) {
send({ type: "disconnect" });
}
removeClient(message.clientId);
});
useActorEvent("pageNavigated", () => {
send({ type: "disconnect" });
});
const [settingsOpen, setSettingsOpen] = useState(false);
const [selectedClientId, setSelectedClientId] = useState<string | undefined>(
data?.clients[0]?.id
);
const selected = useReactiveVar<Screens>(currentScreen);
const [embeddedExplorerIFrame, setEmbeddedExplorerIFrame] =
useState<HTMLIFrameElement | null>(null);
const {
data: clientData,
startPolling,
stopPolling,
} = useQuery(CLIENT_QUERY, {
variables: { id: selectedClientId as string },
skip: !selectedClientId,
pollInterval: 500,
});
const client = clientData?.client;
const clients = data?.clients ?? stableEmptyClients;
const clientIds = clients.map((c) => c.id);
useActorEvent("panelHidden", () => stopPolling());
useActorEvent("panelShown", () => startPolling(500));
if (
(selectedClientId && !clientIds.includes(selectedClientId)) ||
(!selectedClientId && clientIds.length > 0)
) {
setSelectedClientId(clientIds[0]);
}
useEffect(() => {
if (clients.length) {
send({ type: "connect" });
}
}, [send, clients.length]);
return (
<>
<SettingsModal open={settingsOpen} onOpen={setSettingsOpen} />
<ClientNotFoundModal
open={snapshot.context.modalOpen}
onClose={() => send({ type: "closeModal" })}
onRetry={() => send({ type: "retry" })}
/>
<BannerAlert />
<Tabs
value={selected}
onChange={(screen) => currentScreen(screen)}
className="flex flex-col h-screen bg-primary dark:bg-primary-dark"
>
<div className="flex items-center border-b border-b-primary dark:border-b-primary-dark gap-4 px-4">
<a
href="https://go.apollo.dev/c/docs"
target="_blank"
title="Apollo Client developer documentation"
className="block"
rel="noreferrer"
>
<Logo
role="img"
width="24"
height="24"
fill="currentColor"
className="text-icon-primary dark:text-icon-primary-dark"
/>
</a>
<Divider orientation="vertical" />
<Tabs.List className="-mb-px">
<Tabs.Trigger value={Screens.Queries}>
Queries ({client?.queries.total ?? 0})
</Tabs.Trigger>
<Tabs.Trigger value={Screens.Mutations}>
Mutations ({client?.mutations.total ?? 0})
</Tabs.Trigger>
<Tabs.Trigger value={Screens.Cache}>Cache</Tabs.Trigger>
<Tabs.Trigger value={Screens.Explorer}>Explorer</Tabs.Trigger>
</Tabs.List>
<div className="ml-auto flex-1 justify-end flex items-center gap-2 h-full">
{client?.version && (
<GitHubReleaseHoverCard version={client.version}>
<a
className="no-underline"
href={
isSnapshotRelease(client.version)
? `https://github.com/apollographql/apollo-client/pull/${parseSnapshotRelease(client.version).prNumber}`
: `https://github.com/apollographql/apollo-client/releases/tag/v${client.version}`
}
target="_blank"
rel="noreferrer"
>
<Badge variant="info" className="cursor-pointer">
Apollo Client <span className="lowercase">v</span>
{client.version}
</Badge>
</a>
</GitHubReleaseHoverCard>
)}
{clients.length > 1 && (
<Select
size="sm"
className="w-60 ml-2"
value={selectedClientId}
onValueChange={setSelectedClientId}
>
{clients.map((client) => (
<Select.Option key={client.id} value={client.id}>
{client.name ?? `Apollo client ${client.id}`}
</Select.Option>
))}
</Select>
)}
<Divider orientation="vertical" className="mx-2" />
<ButtonGroup>
<Tooltip content="Report an issue">
<Button
aria-label="Report an issue"
variant="hidden"
size="sm"
icon={<IconGitHubSolid />}
asChild
>
<GitHubIssueLink labels={[LABELS.bug]} body={ISSUE_BODY} />
</Button>
</Tooltip>
<Tooltip content="Settings">
<Button
aria-label="Settings"
size="sm"
variant="hidden"
onClick={() => setSettingsOpen(true)}
icon={<IconSettings />}
/>
</Tooltip>
</ButtonGroup>
</div>
</div>
{/**
* We need to keep the iframe inside of the `Explorer` loaded at all times
* so that we don't reload the iframe when we come to this tab
*/}
<Tabs.Content
className="flex flex-col flex-1"
value={Screens.Explorer}
forceMount
>
<Explorer
clientId={selectedClientId}
isVisible={selected === Screens.Explorer}
embeddedExplorerProps={{
embeddedExplorerIFrame,
setEmbeddedExplorerIFrame,
}}
/>
</Tabs.Content>
<Tabs.Content
className="flex-1 overflow-hidden"
value={Screens.Queries}
>
<TabErrorBoundary remarks="Error on Queries tab:">
<Queries
clientId={selectedClientId}
explorerIFrame={embeddedExplorerIFrame}
/>
</TabErrorBoundary>
</Tabs.Content>
<Tabs.Content
className="flex-1 overflow-hidden"
value={Screens.Mutations}
>
<TabErrorBoundary remarks="Error on Mutations tab:">
<Mutations
clientId={selectedClientId}
explorerIFrame={embeddedExplorerIFrame}
/>
</TabErrorBoundary>
</Tabs.Content>
<Tabs.Content className="flex-1 overflow-hidden" value={Screens.Cache}>
<TabErrorBoundary remarks="Error on Cache tab:">
<Cache clientId={selectedClientId} />
</TabErrorBoundary>
</Tabs.Content>
</Tabs>
</>
);
};
interface TabErrorBoundaryProps {
children?: ReactNode;
remarks?: string;
}
function TabErrorBoundary({ children, remarks }: TabErrorBoundaryProps) {
return (
<ErrorBoundary
fallbackRender={({ error }) => {
return (
<SidebarLayout>
<SidebarLayout.Main className="!overflow-y-auto">
<PageError>
<PageError.Content>
<PageError.Title>
We've run into an unexpected error
</PageError.Title>
<PageError.Body>
Please try closing and reopening the browser devtools and
refreshing the page. If the issue persists, please{" "}
<PageError.GitHubLink error={error} remarks={remarks}>
open an issue
</PageError.GitHubLink>{" "}
to help us diagnose the error.
</PageError.Body>
</PageError.Content>
<PageError.Details error={error} />
</PageError>
</SidebarLayout.Main>
</SidebarLayout>
);
}}
>
{children}
</ErrorBoundary>
);
}