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