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

useEnrichedLinkVariants.ts

163 lines5.5 KB

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