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