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

useEnrichedLinkVariants.ts

181 lines6.1 KB

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