Repository navigation
Expand file tree
/
Copy pathGoogleFilePicker.tsx
More file actions
378 lines (378 loc) · 14.2 KB
/
Copy pathGoogleFilePicker.tsx
File metadata and controls
378 lines (378 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
import { SourceTabs } from "./SourceTabs";
import { LinkedObjectLinkPreview } from "./LinkedObjectLinkPreview";
import { GoogleIcon, GoogleSourceIcon } from "../Icons";
import { useAtomValue } from "@effect/atom-react";
import { googleFileContextRecord } from "@t3tools/client-runtime/state/google";
import {
parseGoogleFileLink,
type GoogleFileSummary,
type ScopedThreadRef,
} from "@t3tools/contracts";
import { useNavigate } from "@tanstack/react-router";
import { Atom } from "effect/reactivity";
import { useState } from "react";
import { useComposerDraftStore } from "~/composerDraftStore";
import { useEnvironmentSettings } from "~/hooks/useSettings";
import { useIssueContextStore } from "~/issueContextStore";
import { appAtomRegistry } from "~/rpc/atomRegistry";
import { useDebouncedValue } from "~/state/queries";
import { useEnvironmentQuery } from "~/state/query";
import { googleEnvironment } from "~/state/google";
import { useAtomCommand } from "~/state/use-atom-command";
import {
isAtomCommandInterrupted,
settlePromise,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";
import { toastManager } from "../ui/toast";
import { CommandPaletteContent } from "../CommandPaletteContent";
import { Button } from "../ui/button";
import {
CommandCollection,
CommandDialog,
CommandDialogPopup,
CommandGroup,
CommandGroupLabel,
CommandItem,
CommandList,
} from "../ui/command";
import {
addDays,
dayFromKey,
dayHeading,
DEFAULT_GOOGLE_FILE_PICKER_VIEWS,
eventDayKey,
eventTimeLabel,
filterGoogleFiles,
googleFileTimeLabel,
googleFilterFacets,
groupEventsByDay,
localDayKey,
startOfWeek,
type GoogleFilePickerSource,
} from "./googleFilePicker.logic";
import { GoogleFilePickerFilterBar } from "./GoogleFilePickerFilters";
import { GoogleCalendarWeekStrip } from "./GoogleCalendarWeekStrip";
type GoogleFilePickerMode = "attach" | "link";
/** The thread the picker acts on, and whether picking attaches the page or links it. */
const pickerThread = Atom.make<{
readonly threadRef: ScopedThreadRef;
readonly mode: GoogleFilePickerMode;
} | null>(null).pipe(Atom.keepAlive, Atom.withLabel("google:page-picker-thread"));
export function openGoogleFilePicker(
threadRef: ScopedThreadRef,
mode: GoogleFilePickerMode = "attach",
) {
appAtomRegistry.set(pickerThread, { threadRef, mode });
}
const closePicker = () => appAtomRegistry.set(pickerThread, null);
export function useAttachGoogleFile() {
const getFile = useAtomCommand(googleEnvironment.getFile);
return async (threadRef: ScopedThreadRef, id: string) => {
const result = await getFile({ environmentId: threadRef.environmentId, input: { id } });
if (result._tag === "Failure") return false;
const record = googleFileContextRecord(result.value);
useIssueContextStore.getState().upsert(threadRef.threadId, record);
useComposerDraftStore.getState().insertContextReference(threadRef, {
kind: record.kind,
contextId: record.contextId,
label: record.label,
});
return true;
};
}
/** Links a page to the thread's tab group, replacing the one it had. */
function useLinkGoogleFile() {
const linkThread = useAtomCommand(googleEnvironment.linkThread, { reportFailure: false });
return async (threadRef: ScopedThreadRef, fileId: string): Promise<boolean> => {
const result = await linkThread({
environmentId: threadRef.environmentId,
input: { threadId: threadRef.threadId, fileId },
});
if (result._tag === "Failure") {
if (!isAtomCommandInterrupted(result)) {
const failure = squashAtomCommandFailure(result);
toastManager.add({
type: "error",
title: "Could not link the Google file, event or email",
description: failure instanceof Error ? failure.message : undefined,
});
}
return false;
}
return true;
};
}
export function GoogleFilePickerHost() {
const target = useAtomValue(pickerThread);
return target === null ? null : (
<GoogleFilePickerDialog threadRef={target.threadRef} mode={target.mode} />
);
}
function GoogleFilePickerDialog({
threadRef,
mode,
}: {
threadRef: ScopedThreadRef;
mode: GoogleFilePickerMode;
}) {
const googleEnabled = useEnvironmentSettings(
threadRef.environmentId,
(s) => s.enableGoogleIntegration,
);
const navigate = useNavigate();
const attachPage = useAttachGoogleFile();
const linkPage = useLinkGoogleFile();
const [source, setSource] = useState<GoogleFilePickerSource>("drive");
const [views, setViews] = useState(DEFAULT_GOOGLE_FILE_PICKER_VIEWS);
const view = views[source];
// Date filters, "Today", and the opening week count from when the picker opened.
const [openedAt] = useState(() => Date.now());
const today = localDayKey(new Date(openedAt));
const [weekStart, setWeekStart] = useState(() => startOfWeek(new Date(openedAt)));
const [selectedDay, setSelectedDay] = useState<string | null>(today);
// Null reads the calendars ticked in Google Calendar.
const [calendarIds, setCalendarIds] = useState<ReadonlyArray<string> | null>(null);
const [query, setQuery] = useState("");
const [attaching, setAttaching] = useState(false);
const settled = useDebouncedValue(query.trim(), 400);
const connection = useEnvironmentQuery(
googleEnabled
? googleEnvironment.connection({ environmentId: threadRef.environmentId, input: {} })
: null,
);
const connected = connection.data?.phase === "connected";
const calendars = useEnvironmentQuery(
connected && source === "calendar"
? googleEnvironment.calendars({ environmentId: threadRef.environmentId, input: {} })
: null,
);
// With no search, the calendar shows one week at a time, read a week at a time.
const browsingWeek = source === "calendar" && settled === "";
const result = useEnvironmentQuery(
connected
? googleEnvironment.files({
environmentId: threadRef.environmentId,
input: {
query: settled,
source,
...(source === "calendar" && calendarIds ? { calendarIds: [...calendarIds] } : {}),
...(browsingWeek
? {
timeMin: weekStart.toISOString(),
timeMax: addDays(weekStart, 7).toISOString(),
}
: {}),
},
})
: null,
);
const loaded = result.data?.files;
const pasted = parseGoogleFileLink(settled) !== null;
const files = filterGoogleFiles(loaded ?? [], view, openedAt, pasted);
const facets = googleFilterFacets(loaded ?? [], view, openedAt, pasted);
const dayCounts = new Map<string, number>();
for (const event of source === "calendar" ? files : []) {
const day = eventDayKey(event);
if (day !== null) dayCounts.set(day, (dayCounts.get(day) ?? 0) + 1);
}
const days =
source === "calendar"
? groupEventsByDay(files).filter(
(group) => !browsingWeek || selectedDay === null || group.day === selectedDay,
)
: [];
const pick = async (page: GoogleFileSummary) => {
if (attaching) return;
setAttaching(true);
const choose = mode === "link" ? linkPage : attachPage;
const picked = await settlePromise(() => choose(threadRef, page.url));
setAttaching(false);
if (picked._tag === "Success" && picked.value) closePicker();
};
const row = (page: GoogleFileSummary) => (
<CommandItem
key={page.id}
value={page.id}
disabled={attaching}
onMouseDown={(event) => event.preventDefault()}
onClick={() => pick(page)}
>
<GoogleSourceIcon
fileType={page.fileType}
className="size-4 shrink-0 text-muted-foreground"
/>
<LinkedObjectLinkPreview
environmentId={threadRef.environmentId}
threadRef={threadRef}
objectLink={{ kind: "google-file", fileId: page.id, url: page.url }}
link={
page.fileType === "event" ? (
<span className="flex min-w-0 flex-1 items-center gap-2">
<span className="w-28 shrink-0 text-muted-foreground text-xs tabular-nums">
{eventTimeLabel(page)}
</span>
<span className="min-w-0 flex-1 truncate text-sm">{page.title}</span>
<span className="w-40 shrink-0 truncate text-end text-muted-foreground text-xs">
{page.owner}
</span>
</span>
) : (
<span className="flex min-w-0 flex-1 items-center gap-2">
<span className="min-w-0 flex-1 truncate text-sm">{page.title}</span>
<span className="w-40 shrink-0 truncate text-muted-foreground text-xs">
{page.owner}
</span>
<span className="w-28 shrink-0 text-end text-muted-foreground/70 text-xs tabular-nums">
{googleFileTimeLabel(page, openedAt)}
</span>
</span>
)
}
/>
</CommandItem>
);
const status =
connection.error ??
result.error ??
(connection.data === null
? "Reading Google status…"
: result.isPending || settled !== query.trim()
? "Searching Google…"
: files.length === 0 || (source === "calendar" && days.length === 0)
? view !== DEFAULT_GOOGLE_FILE_PICKER_VIEWS[source] && (loaded?.length ?? 0) > 0
? "Nothing matches these filters."
: browsingWeek
? selectedDay === null
? "No events this week."
: `No events on ${dayHeading(selectedDay, openedAt)}.`
: "No matching sources. Search or paste a Google link."
: null);
if (!googleEnabled) return null;
return (
<CommandDialog
open
onOpenChange={(open) => {
if (!open) closePicker();
}}
>
<CommandDialogPopup
aria-label={
mode === "link"
? "Link Google file, event or email"
: "Attach Google file, event or email"
}
className="overflow-hidden"
>
{connected || connection.data === null ? (
<CommandPaletteContent
inputProps={{
placeholder:
source === "gmail"
? "Search Gmail (from:, subject:, is:unread) or paste a conversation link"
: "Search Google or paste a source link",
startAddon: <GoogleIcon />,
}}
footerActionLabel={
mode === "link"
? attaching
? "Linking…"
: "Link source"
: attaching
? "Attaching…"
: "Attach source"
}
inputAccessory={
<>
<SourceTabs
className="flex gap-1 px-3 py-2"
options={[
{ id: "drive", label: "Drive" },
{ id: "calendar", label: "Calendar" },
{ id: "gmail", label: "Gmail" },
]}
activeId={source}
onSelect={setSource}
/>
<GoogleFilePickerFilterBar
{...facets}
source={source}
loading={loaded === undefined && result.error === null}
view={view}
onChange={(next) => setViews((current) => ({ ...current, [source]: next }))}
environmentId={threadRef.environmentId}
calendars={calendars.data?.calendars ?? null}
calendarIds={calendarIds}
onCalendarIdsChange={setCalendarIds}
/>
{browsingWeek ? (
<GoogleCalendarWeekStrip
weekStart={weekStart}
selectedDay={selectedDay}
today={today}
counts={dayCounts}
onSelectDay={setSelectedDay}
onWeekStartChange={(next) => {
setWeekStart(next);
// Moving a week keeps the picked weekday, so the agenda follows along.
const shift = Math.round((next.getTime() - weekStart.getTime()) / 86_400_000);
setSelectedDay((day) =>
day === null ? null : localDayKey(addDays(dayFromKey(day), shift)),
);
}}
/>
) : null}
</>
}
mode="none"
value={query}
onValueChange={setQuery}
>
{status ? (
<div className="py-10 text-center text-muted-foreground text-sm">{status}</div>
) : (
<CommandList>
{source === "calendar" ? (
days.map((group) => (
<CommandGroup key={group.day}>
<CommandGroupLabel>{dayHeading(group.day, openedAt)}</CommandGroupLabel>
{group.events.map(row)}
</CommandGroup>
))
) : (
<CommandGroup items={[...files]}>
<CommandCollection>{row}</CommandCollection>
</CommandGroup>
)}
</CommandList>
)}
{result.data?.hasMore ? (
<p className="px-4 py-2 text-xs text-muted-foreground">
{source === "calendar"
? "Showing the first events from each calendar. Search by title to narrow the results."
: "Showing the first 30 files. Search by title to narrow the results."}
</p>
) : null}
</CommandPaletteContent>
) : (
<div className="flex flex-col items-center gap-3 px-6 py-10 text-center text-sm">
<p className="text-muted-foreground">
{mode === "link"
? "Connect Google to link a Drive file, Calendar event or Gmail conversation."
: "Connect Google to attach Drive files, Calendar events and Gmail conversations."}
</p>
<Button
size="sm"
onClick={() => {
closePicker();
void navigate({ to: "/settings/integrations" });
}}
>
Connect Google
</Button>
</div>
)}
</CommandDialogPopup>
</CommandDialog>
);
}