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

useEnrichedLinkVariants.ts

210 lines7.4 KB

1import { modelChipIcon } from "../lib/contextChipIconSource";
2import { useMemo } from "react";
3import {
4 assistantCitationLabel,
5 parseAssistantCitationHref,
6} from "@t3tools/shared/assistantCitations";
7import { Image } from "react-native";
8import type { LinkPillContent, MarkdownStyle } from "react-native-enriched-markdown";
9import type { ComposerContextRecord } from "@t3tools/contracts";
10import type { MessageResolver } from "../i18n/messages";
11import { contextChipAccent, contextChipPresentation } from "../lib/composerChipPresentation";
12import { CONTEXT_CHIP_ICON_SOURCES } from "../lib/contextChipIcons.generated";
13import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
14import { contextChipIconSource } from "../lib/contextChipIconSource";
15import { markdownFileIconSource } from "../lib/markdownFileIcons";
16import { markdownLinkIcon } from "../lib/markdownLinkIcons";
17import { useUniwindTheme } from "../lib/useUniwindTheme";
18import { resolveMarkdownFileIcon, resolveMarkdownLinkPresentation } from "../lib/markdownLinks";
19import type {
20 MarkdownLinkCustomization,
21 SelectableMarkdownTextProps,
22} from "./SelectableMarkdownText.types";
23import {
24 enrichedContextLinkPresentation,
25 enrichedLinkVariantPattern,
26 enrichedModelReference,
27 enrichedSkillReference,
28 enrichedThreadLinkPresentation,
29} from "../lib/enrichedLinkPresentation";
30import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
31​
32function chipCustomization(
33 chip: ReturnType<typeof enrichedContextLinkPresentation>,
34): MarkdownLinkCustomization | undefined {
35 if (!chip) return undefined;
36 return {
37 label: chip.label,
38 icon: contextChipIconSource(chip.icon),
39 chip: { accent: chip.accent, tintsIcon: chip.icon.kind === "chip" },
40 };
42​
43export function enrichedContextLinkCustomization(
44 href: string,
45 records?: ReadonlyArray<ComposerContextRecord>,
46 resolve?: MessageResolver,
47 locale?: string,
48) {
49 return chipCustomization(enrichedContextLinkPresentation(href, records, resolve, locale));
51​
52export function enrichedThreadLinkCustomization(href: string) {
53 return chipCustomization(enrichedThreadLinkPresentation(href));
55​
56export function withChipIcon(
57 customization: MarkdownLinkCustomization | undefined,
58 iconUri: string | undefined,
59): MarkdownLinkCustomization | undefined {
60 return iconUri && customization?.chip
61 ? {
62 ...customization,
63 icon: { uri: iconUri },
64 chip: { ...customization.chip, tintsIcon: false },
65 }
66 : customization;
68​
69function presentation(
70 url: string,
71 props: Pick<SelectableMarkdownTextProps, "linkCustomization" | "skills" | "models" | "textStyle">,
72): MarkdownLinkCustomization | undefined {
73 const custom = props.linkCustomization?.(url);
74 if (custom) return custom;
75 const citation = parseAssistantCitationHref(url);
76 if (citation)
77 return {
78 label: assistantCitationLabel(citation),
79 icon: CONTEXT_CHIP_ICON_SOURCES["message-circle"],
80 chip: { accent: contextChipPresentation("citation").accent, tintsIcon: true },
81 };
82 const context = enrichedContextLinkCustomization(url);
83 if (context) return context;
84 const skill = enrichedSkillReference(url, props.skills);
85 if (skill)
86 return {
87 label: formatProviderSkillDisplayName(skill),
88 icon: CONTEXT_CHIP_ICON_SOURCES.skill,
89 chip: { accent: contextChipPresentation("skill").accent, tintsIcon: true },
90 };
91 const model = enrichedModelReference(url, props.models);
92 if (model) {
93 const icon = modelChipIcon(model);
94 return {
95 label: model.label,
96 icon: icon.source,
97 chip: {
98 accent: icon.accent,
99 tintsIcon: icon.tintsIcon,
100 },
101 };
102 }
103 const link = resolveMarkdownLinkPresentation(url);
104 if (link.kind === "file")
105 return {
106 label: link.label,
107 icon: markdownFileIconSource(link.icon),
108 chip: { accent: contextChipPresentation("mention").accent, tintsIcon: false },
109 };
110 const icon = link.kind === "external" ? markdownLinkIcon(link.host) : null;
111 return icon
112 ? {
113 color: props.textStyle.linkColor,
114 icon: icon.source,
115 ...(icon.tints ? { iconTintColor: props.textStyle.linkColor } : {}),
116 }
117 : undefined;
119​
120export function useEnrichedLinkVariants(
121 urls: ReadonlyArray<string>,
122 props: SelectableMarkdownTextProps,
123 fileHrefs: ReadonlySet<string>,
124) {
125 const { linkCustomization, skills, models, textStyle, fileEmbedLabel } = props;
126 const theme = useUniwindTheme();
127 const { fontFamilies } = useAppearancePreferences();
128 const links = useMemo(() => {
129 const byUrl = new Map<string, MarkdownLinkCustomization>();
130 for (const url of urls) {
131 if (byUrl.has(url)) continue;
132 const label = fileHrefs.has(url) ? fileEmbedLabel?.(url, false) : null;
133 const value = label
134 ? {
135 label,
136 color: textStyle.fileTextColor,
137 icon: markdownFileIconSource(resolveMarkdownFileIcon(label)),
138 }
139 : presentation(url, { linkCustomization, skills, models, textStyle });
140 if (value) byUrl.set(url, value);
141 }
142 return byUrl;
143 }, [urls, linkCustomization, skills, models, textStyle, fileHrefs, fileEmbedLabel]);
144 const chipForegrounds = useMemo(
145 () =>
146 new Map(
147 [...links].map(([url, link]) => [
148 url,
149 link.chip
150 ? (contextChipAccent(link.chip.accent, theme, textStyle.color) ?? textStyle.color)
151 : undefined,
152 ]),
153 ),
154 [links, textStyle, theme],
155 );
156 const icons = useMemo(
157 () =>
158 new Map(
159 [...links].map(([url, link]) => [
160 url,
161 {
162 iconUri: link.icon ? Image.resolveAssetSource(link.icon)?.uri : undefined,
163 iconTintColor: link.chip?.tintsIcon ? chipForegrounds.get(url) : link.iconTintColor,
164 },
165 ]),
166 ),
167 [chipForegrounds, links],
168 );
169 // A link that shows its own text wraps with its icon in front; a pill is one unbreakable box,
170 // so only links that swap in a short label become pills.
171 const linkVariants = useMemo<MarkdownStyle["linkVariants"]>(
172 () =>
173 Object.fromEntries(
174 [...links].map(([url, link]) => {
175 const chipForeground = chipForegrounds.get(url);
176 const icon = icons.get(url);
177 const inlineIconUri = link.label === undefined && icon?.iconUri;
178 return [
179 enrichedLinkVariantPattern(url),
180 {
181 ...(inlineIconUri
182 ? { inlineIcon: { iconUri: inlineIconUri, iconTintColor: icon.iconTintColor } }
183 : {
184 pill: {
185 borderColor: "transparent",
186 borderWidth: 0,
187 borderRadius: 6,
188 paddingHorizontal: 5,
189 paddingVertical: 2,
190 },
191 }),
192 fontFamily: chipForeground ? fontFamilies.medium : textStyle.fontFamily,
193 color: chipForeground ?? link.color ?? textStyle.fileTextColor,
194 underline: false,
195 backgroundColor: "transparent",
196 },
197 ];
198 }),
199 ),
200 [chipForegrounds, fontFamilies, icons, links, textStyle],
201 );
202 const linkPillContent = useMemo<Record<string, LinkPillContent>>(
203 () =>
204 Object.fromEntries(
205 [...links].map(([url, link]) => [url, { label: link.label, ...icons.get(url) }]),
206 ),
207 [icons, links],
208 );
209 return { linkVariants, linkPillContent };