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