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

useEnrichedLinkVariants.ts

167 lines5.7 KB

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 { resolveMarkdownFileIcon, 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 };
35​
36export function enrichedContextLinkCustomization(
37 href: string,
38 records?: ReadonlyArray<ComposerContextRecord>,
39) {
40 return chipCustomization(enrichedContextLinkPresentation(href, records));
42​
43export function enrichedThreadLinkCustomization(href: string) {
44 return chipCustomization(enrichedThreadLinkPresentation(href));
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;
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;
91​
92export function useEnrichedLinkVariants(
93 urls: ReadonlyArray<string>,
94 props: SelectableMarkdownTextProps,
95 fileHrefs: ReadonlySet<string>,
96) {
97 const { linkCustomization, skills, textStyle, fileEmbedLabel } = props;
98 const theme = useUniwindTheme();
99 const links = useMemo(() => {
100 const byUrl = new Map<string, MarkdownLinkCustomization>();
101 for (const url of urls) {
102 if (byUrl.has(url)) continue;
103 const label = fileHrefs.has(url) ? fileEmbedLabel?.(url, false) : null;
104 const value = label
105 ? {
106 label,
107 color: textStyle.fileTextColor,
108 icon: markdownFileIconSource(resolveMarkdownFileIcon(label)),
109 }
110 : presentation(url, { linkCustomization, skills, textStyle });
111 if (value) byUrl.set(url, value);
112 }
113 return byUrl;
114 }, [urls, linkCustomization, skills, textStyle, fileHrefs, fileEmbedLabel]);
115 const chipForegrounds = useMemo(
116 () =>
117 new Map(
118 [...links].map(([url, link]) => [
119 url,
120 link.chip
121 ? (contextChipAccent(link.chip.accent, theme, textStyle.color) ?? textStyle.color)
122 : undefined,
123 ]),
124 ),
125 [links, textStyle, theme],
126 );
127 const linkVariants = useMemo<MarkdownStyle["linkVariants"]>(
128 () =>
129 Object.fromEntries(
130 [...links].map(([url, link]) => {
131 const chipForeground = chipForegrounds.get(url);
132 return [
133 enrichedLinkVariantPattern(url),
134 {
135 pill: {
136 borderColor: "transparent",
137 borderWidth: 0,
138 borderRadius: 6,
139 paddingHorizontal: 5,
140 paddingVertical: 2,
141 },
142 fontFamily: chipForeground ? FONT_FAMILIES.medium : textStyle.fontFamily,
143 color: chipForeground ?? link.color ?? textStyle.fileTextColor,
144 underline: false,
145 backgroundColor: "transparent",
146 },
147 ];
148 }),
149 ),
150 [chipForegrounds, links, textStyle],
151 );
152 const linkPillContent = useMemo<Record<string, LinkPillContent>>(
153 () =>
154 Object.fromEntries(
155 [...links].map(([url, link]) => [
156 url,
157 {
158 label: link.label,
159 iconUri: link.icon ? Image.resolveAssetSource(link.icon)?.uri : undefined,
160 iconTintColor: link.chip?.tintsIcon ? chipForegrounds.get(url) : link.iconTintColor,
161 },
162 ]),
163 ),
164 [chipForegrounds, links],
165 );
166 return { linkVariants, linkPillContent };