Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

MarkdownThreadLink.tsx

74 lines3.0 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import { useGT, useMessages } from "gt-react";
3import {
4 formatHostedThreadLink,
5 guessThreadLinkRef,
6 threadLinkCandidates,
7 type ThreadLink,
8} from "@t3tools/shared/threadLinks";
9import { unknownThreadLinkLabel } from "@t3tools/shared/threadLinksCopy";
10​
11import { configuredHostedAppUrl } from "../../hostedPairing";
12import { useThreadRefs, useThreadShell } from "../../state/entities";
13import { useEnvironmentIdentities } from "../../state/environments";
14import { orchestrationEnvironment } from "../../state/orchestration";
15import { useEnvironmentQuery } from "../../state/query";
16import { UnresolvedChip } from "../contextChipParts";
17import { ThreadContextChip } from "../ThreadContextChip";
18​
19/** `label` is the link's written text; an autolink has none. */
20export function MarkdownThreadLink(props: {
21 readonly environmentId: EnvironmentId;
22 readonly link: ThreadLink;
23 readonly label: string | null;
24}) {
25 const gt = useGT();
26 const messages = useMessages();
27 const candidates = threadLinkCandidates(props.link, props.environmentId);
28 const writtenRef = candidates[0]!;
29 const written = useThreadShell(writtenRef);
30 // A linked machine's threads are reachable only through its server, not from this client.
31 const reachable = useEnvironmentIdentities().some(
32 (environment) => environment.environmentId === writtenRef.environmentId,
33 );
34 const lookup = useEnvironmentQuery(
35 written === null && reachable
36 ? orchestrationEnvironment.threadLinkMetadata({
37 environmentId: writtenRef.environmentId,
38 input: { threadIds: candidates.map((ref) => ref.threadId) },
39 })
40 : null,
41 );
42 const settled = lookup.isSuccess && !lookup.isPending;
43 const target =
44 written !== null
45 ? { ref: writtenRef, metadata: written }
46 : (candidates.flatMap((ref) => {
47 const metadata = lookup.data?.[ref.threadId];
48 return metadata ? [{ ref, metadata }] : [];
49 })[0] ?? null);
50 const missing = target === null && (settled || !reachable);
51 const guess = missing && props.link.environmentId === null;
52 const refs = useThreadRefs(guess);
53 const remoteRef = guess
54 ? guessThreadLinkRef(
55 props.link,
56 refs.filter((ref) => ref.environmentId !== writtenRef.environmentId),
57 )
58 : null;
59 const remote = useThreadShell(remoteRef);
60 const resolved = target ?? (remoteRef && remote ? { ref: remoteRef, metadata: remote } : null);
61 if (missing && resolved === null) {
62 const unknown = unknownThreadLinkLabel(props.link, props.environmentId, messages);
63 return <UnresolvedChip label={props.label ?? unknown} tooltip={unknown} />;
64 }
65 const ref = resolved?.ref ?? writtenRef;
66 const title = resolved?.metadata.title.trim() || props.label || gt("Thread");
67 return (
68 <ThreadContextChip
69 record={{ ...ref, title }}
70 projectId={resolved?.metadata.projectId ?? null}
71 copyMarkdown={formatHostedThreadLink(ref, title, configuredHostedAppUrl())}
72 />
73 );