Open History to see the last commit
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 { 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 { resolveMarkdownLinkIcon, resolveMarkdownLinkPresentation } from "../lib/markdownLinks";
16import type {
17 MarkdownLinkCustomization,
18 SelectableMarkdownTextProps,
19} from "./SelectableMarkdownText.types";
20import {
21 enrichedContextLinkPresentation,
22 enrichedLinkVariantPattern,
23 enrichedSkillDisplayName,
24 enrichedSkillReference,
25} from "../lib/enrichedLinkPresentation";
26
27export function enrichedContextLinkCustomization(
28 href: string,
29 records?: ReadonlyArray<ComposerContextRecord>,
30): MarkdownLinkCustomization | undefined {
31 const context = enrichedContextLinkPresentation(href, records);
32 if (!context) return undefined;
33 return {
34 label: context.label,
35 icon: contextChipIconSource(context.icon),
36 chip: { accent: context.accent, tintsIcon: context.icon.kind === "chip" },
37 };
38}
39
40function presentation(
41 url: string,
42 props: Pick<SelectableMarkdownTextProps, "linkCustomization" | "skills" | "textStyle">,
43): MarkdownLinkCustomization | undefined {
44 const custom = props.linkCustomization?.(url);
45 if (custom) return custom;
46 const context = enrichedContextLinkCustomization(url);
47 if (context) return context;
48 const skill = enrichedSkillReference(url, props.skills);
49 if (skill)
50 return {
51 label: enrichedSkillDisplayName(skill),
52 icon: CONTEXT_CHIP_ICON_SOURCES.skill,
53 chip: { accent: contextChipPresentation("skill").accent, tintsIcon: true },
54 };
55 const link = resolveMarkdownLinkPresentation(url);
56 if (link.kind === "file")
57 return {
58 label: link.label,
59 icon: markdownFileIconSource(link.icon),
60 chip: { accent: contextChipPresentation("mention").accent, tintsIcon: false },
61 };
62 const icon = link.kind === "external" ? resolveMarkdownLinkIcon(link.host) : null;
63 return icon
64 ? {
65 color: props.textStyle.linkColor,
66 icon: markdownLinkIconSource(icon),
67 iconTintColor: props.textStyle.linkColor,
68 }
69 : undefined;
70}
71
72export function useEnrichedLinkVariants(
73 assets: ReadonlyArray<MarkdownDocumentAsset>,
74 props: SelectableMarkdownTextProps,
75) {
76 const { linkCustomization, skills, textStyle } = props;
77 const links = useMemo(() => {
78 const byUrl = new Map<string, MarkdownLinkCustomization>();
79 for (const asset of assets) {
80 if (asset.kind !== "link" || byUrl.has(asset.url)) continue;
81 const value = presentation(asset.url, { linkCustomization, skills, textStyle });
82 if (value) byUrl.set(asset.url, value);
83 }
84 return byUrl;
85 }, [assets, linkCustomization, skills, textStyle]);
86 const chipForegrounds = useMemo(
87 () =>
88 new Map(
89 [...links].map(([url, link]) => [
90 url,
91 link.chip ? (link.chip.accent ?? textStyle.color) : undefined,
92 ]),
93 ),
94 [links, textStyle],
95 );
96 const linkVariants = useMemo<MarkdownStyle["linkVariants"]>(
97 () =>
98 Object.fromEntries(
99 [...links].map(([url, link]) => {
100 const chipForeground = chipForegrounds.get(url);
101 return [
102 enrichedLinkVariantPattern(url),
103 {
104 pill: {
105 borderColor: "transparent",
106 borderWidth: 0,
107 borderRadius: 6,
108 paddingHorizontal: 5,
109 paddingVertical: 2,
110 },
111 fontFamily: chipForeground ? FONT_FAMILIES.medium : textStyle.fontFamily,
112 color: chipForeground ?? link.color ?? textStyle.fileTextColor,
113 underline: false,
114 backgroundColor: "transparent",
115 },
116 ];
117 }),
118 ),
119 [chipForegrounds, links, textStyle],
120 );
121 const linkPillContent = useMemo<Record<string, LinkPillContent>>(
122 () =>
123 Object.fromEntries(
124 [...links].map(([url, link]) => [
125 url,
126 {
127 label: link.label,
128 iconUri: link.icon ? Image.resolveAssetSource(link.icon)?.uri : undefined,
129 iconTintColor: link.chip?.tintsIcon ? chipForegrounds.get(url) : link.iconTintColor,
130 },
131 ]),
132 ),
133 [chipForegrounds, links],
134 );
135 return { linkVariants, linkPillContent };
136}