Repository navigation
Expand file tree
/
Copy pathNotionThreadLink.tsx
More file actions
85 lines (79 loc) · 3.09 KB
/
Copy pathNotionThreadLink.tsx
File metadata and controls
85 lines (79 loc) · 3.09 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
import { useAtomValue } from "@effect/atom-react";
import { notionLinkForThread } from "@t3tools/client-runtime/state/notion";
import type { EnvironmentId, ScopedThreadRef, NotionThreadLink } from "@t3tools/contracts";
import { ExternalLinkIcon, PencilIcon, UnlinkIcon } from "lucide-react";
import { useLinkClickHandler } from "~/browser/useOpenLink";
import { useEnvironmentSettings } from "~/hooks/useSettings";
import { notionEnvironment } from "~/state/notion";
import { useEnvironmentQuery } from "~/state/query";
import { useAtomCommand } from "~/state/use-atom-command";
import { NotionIcon } from "../Icons";
import { MenuItem, MenuSeparator } from "../ui/menu";
import { ThreadLinkRow } from "./ThreadLinkRow";
import { openNotionPagePicker } from "./NotionPagePicker";
/** Every Notion link in the environment. Null until the server answers, or on servers without links. */
function useNotionThreadLinks(environmentId: EnvironmentId | null) {
return useEnvironmentQuery(
environmentId === null ? null : notionEnvironment.threadLinks({ environmentId, input: {} }),
).data;
}
/** The Notion page linked to the thread's tab group, if any. */
export function useThreadNotionLink(threadRef: ScopedThreadRef | null) {
const links = useNotionThreadLinks(threadRef?.environmentId ?? null);
return threadRef === null ? null : notionLinkForThread(links, threadRef.threadId);
}
export function useUnlinkNotionPage() {
const unlinkThread = useAtomCommand(notionEnvironment.unlinkThread);
return (threadRef: ScopedThreadRef) =>
unlinkThread({
environmentId: threadRef.environmentId,
input: { threadId: threadRef.threadId },
});
}
/** The thread details row for the thread's linked Notion page, showing its title when linked. */
export function NotionThreadLinkRow(props: { threadRef: ScopedThreadRef; link: NotionThreadLink }) {
const { threadRef, link } = props;
const notionEnabled = useEnvironmentSettings(
threadRef.environmentId,
(s) => s.enableNotionIntegration,
);
const canEdit = useAtomValue(
notionEnvironment.linkThread.permissionAtom(threadRef.environmentId),
);
const openLink = useLinkClickHandler(threadRef);
const unlink = useUnlinkNotionPage();
return (
<ThreadLinkRow
watchKind="notion"
linkUrl={link.url}
icon={<NotionIcon className="size-4 shrink-0" />}
label={link.title}
accessibleLabel={`Linked Notion page: ${link.title}`}
threadRef={threadRef}
url={link.url}
>
<MenuItem
render={<a href={link.url} target="_blank" rel="noreferrer" />}
onClick={(event) => openLink(event, link.url)}
>
<ExternalLinkIcon />
Open in Notion
</MenuItem>
{notionEnabled && canEdit ? (
<MenuItem onClick={() => openNotionPagePicker(threadRef, "link")}>
<PencilIcon />
Change page…
</MenuItem>
) : null}
{canEdit ? (
<>
<MenuSeparator />
<MenuItem onClick={() => void unlink(threadRef)}>
<UnlinkIcon />
Unlink page
</MenuItem>
</>
) : null}
</ThreadLinkRow>
);
}