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