Repository navigation
Expand file tree
/
Copy pathThreadLinkRow.tsx
More file actions
141 lines (137 loc) · 5.32 KB
/
Copy pathThreadLinkRow.tsx
File metadata and controls
141 lines (137 loc) · 5.32 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
import { useAtomValue } from "@effect/atom-react";
import { threadLinkWatchFor } from "@t3tools/client-runtime/state/threadLinkWatches";
import type { ScopedThreadRef, ThreadLinkKind } from "@t3tools/contracts";
import { parseComposerObjectLink } from "@t3tools/source-control-definitions/composerObjectLinks";
import { EyeIcon, EyeOffIcon } from "lucide-react";
import { useState, type ReactNode } from "react";
import { useLinkClickHandler } from "~/browser/useOpenLink";
import { cn } from "~/lib/utils";
import { useEnvironmentQuery } from "~/state/query";
import { threadLinkWatchEnvironment } from "~/state/threadLinkWatches";
import { useAtomCommand } from "~/state/use-atom-command";
import { ContextPreviewBody } from "../contextPreviewRegistry";
import { Menu, MenuItem, MenuPopup, MenuSeparator, MenuTrigger } from "../ui/menu";
import { CursorPreviewCard } from "./CursorPreviewCard";
import {
isPreviewableObjectLink,
useLinkedObjectRecord,
type PreviewableObjectLink,
} from "./LinkedObjectLinkPreview";
import { ThreadDetailsControl } from "./ThreadDetailsControl";
import { THREAD_DETAILS_PANEL_LABEL_CLASS } from "./threadDetailsPanelStyles";
/** The thread's watch of its link of `kind`, or null. Null too on servers without link watches. */
function useThreadLinkWatch(threadRef: ScopedThreadRef, kind: ThreadLinkKind) {
const watches = useEnvironmentQuery(
threadLinkWatchEnvironment.watches({ environmentId: threadRef.environmentId, input: {} }),
).data;
return {
supported: watches != null,
watch: threadLinkWatchFor(watches, threadRef.threadId, kind),
};
}
/**
* One linked item in the thread details panel's Links section; its menu holds the link's actions.
* Hovering it shows the card a chip for the same link would show. An eye marks a link the
* thread's agent is woken by when it changes.
*/
export function ThreadLinkRow(props: {
threadRef: ScopedThreadRef;
url: string;
icon: ReactNode;
label: string;
trailing?: ReactNode;
accessibleLabel: string;
watchKind: ThreadLinkKind;
/** The link as stored, which a watch records; `url` may be a live read of it. */
linkUrl: string;
children: ReactNode;
}) {
const [menuOpen, setMenuOpen] = useState(false);
const objectLink = parseComposerObjectLink(props.url);
const { supported, watch } = useThreadLinkWatch(props.threadRef, props.watchKind);
const canWatch = useAtomValue(
threadLinkWatchEnvironment.setWatching.permissionAtom(props.threadRef.environmentId),
);
const setWatching = useAtomCommand(threadLinkWatchEnvironment.setWatching);
// A watch of a link this row no longer shows ends on the server's next read.
const watching = watch !== null && watch.url === props.linkUrl;
const toggleWatch = () =>
void setWatching({
environmentId: props.threadRef.environmentId,
input: { threadId: props.threadRef.threadId, kind: props.watchKind, watching: !watching },
});
return (
<li className="flex h-8 items-center">
<Menu open={menuOpen} onOpenChange={setMenuOpen}>
<CursorPreviewCard
suppressed={menuOpen}
trigger={
<MenuTrigger
render={
<ThreadDetailsControl
variant="ghost"
size="sm"
part="row"
aria-label={
watching
? `${props.accessibleLabel}. Watching for changes`
: props.accessibleLabel
}
/>
}
>
{props.icon}
<span
className={cn(
"min-w-0 flex-1 truncate text-left",
THREAD_DETAILS_PANEL_LABEL_CLASS,
)}
>
{props.label}
</span>
{watching ? (
<EyeIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
) : null}
{props.trailing}
</MenuTrigger>
}
>
{isPreviewableObjectLink(objectLink) ? (
<ThreadLinkPreviewBody threadRef={props.threadRef} objectLink={objectLink} />
) : (
<p className="break-all text-muted-foreground">{props.url}</p>
)}
</CursorPreviewCard>
<MenuPopup align="end">
{supported && canWatch ? (
<>
<MenuItem onClick={toggleWatch}>
{watching ? <EyeOffIcon /> : <EyeIcon />}
{watching ? "Stop watching" : "Watch for changes"}
</MenuItem>
<MenuSeparator />
</>
) : null}
{props.children}
</MenuPopup>
</Menu>
</li>
);
}
/** Mounts only while the card is open, so the linked object is read on hover, not per row. */
function ThreadLinkPreviewBody(props: {
threadRef: ScopedThreadRef;
objectLink: PreviewableObjectLink;
}) {
const openLink = useLinkClickHandler(props.threadRef);
const { available, record, failed } = useLinkedObjectRecord(
props.threadRef.environmentId,
props.objectLink,
true,
);
if (record !== null) {
return <ContextPreviewBody kind={record.kind} preview={{ record, onOpenLink: openLink }} />;
}
if (available && !failed) return <p className="text-muted-foreground">Loading…</p>;
return <p className="break-all text-muted-foreground">{props.objectLink.url}</p>;
}