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

SkillInlineText.tsx

92 lines3.0 KB

1import { Children, cloneElement, isValidElement, type ReactNode } from "react";
2import type { ServerProviderSkill } from "@t3tools/contracts";
3import { formatProviderSkillDisplayName } from "@t3tools/client-runtime/providerSkills";
4​
5import {
6 CHAT_INLINE_CHIP_CLASS_NAME,
7 CHAT_INLINE_CHIP_LABEL_CLASS_NAME,
8 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
9 CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES,
10 SKILL_CHIP_ICON_SVG,
11} from "../composerInlineChip";
12import { cn } from "~/lib/utils";
13​
14const SKILL_TOKEN_REGEX =
15 /(^|\s)\p{Sc}(?![0-9][0-9_]*(?:[kKmMbBtT]|[eE][0-9]+)?(?:\s|$))(?=[a-zA-Z0-9:_-]*[a-zA-Z])([a-zA-Z0-9][a-zA-Z0-9:_-]*)(?=\s|$)/gu;
16​
17type InlineSkill = Pick<ServerProviderSkill, "name" | "displayName">;
18​
19export function SkillInlineText(props: { text: string; skills: ReadonlyArray<InlineSkill> }) {
20 const nodes: ReactNode[] = [];
21 let cursor = 0;
22​
23 for (const match of props.text.matchAll(SKILL_TOKEN_REGEX)) {
24 const prefix = match[1] ?? "";
25 const name = match[2] ?? "";
26 const start = (match.index ?? 0) + prefix.length;
27 const rawText = `$${name}`;
28 const skill = props.skills.find((candidate) => candidate.name === name);
29 if (!skill) {
30 continue;
31 }
32​
33 if (start > cursor) {
34 nodes.push(props.text.slice(cursor, start));
35 }
36 nodes.push(<SkillChip key={`${start}:${name}`} skill={skill} rawText={rawText} />);
37 cursor = (match.index ?? 0) + match[0].length;
38 }
39​
40 if (cursor === 0) {
41 return <>{props.text}</>;
42 }
43 if (cursor < props.text.length) {
44 nodes.push(props.text.slice(cursor));
45 }
46 return <>{nodes}</>;
48​
49export function renderSkillInlineMarkdownChildren(
50 children: ReactNode,
51 skills: ReadonlyArray<InlineSkill>,
52): ReactNode {
53 return Children.map(children, (child) => {
54 if (typeof child === "string") {
55 return <SkillInlineText text={child} skills={skills} />;
56 }
57 if (!isValidElement<{ children?: ReactNode; node?: { tagName?: string } }>(child)) {
58 return child;
59 }
60 // Custom react-markdown components replace the intrinsic type, so also
61 // check the hast node they carry.
62 const markdownTagName = typeof child.type === "string" ? child.type : child.props.node?.tagName;
63 if (markdownTagName === "code" || markdownTagName === "a") {
64 return child;
65 }
66 if (!("children" in child.props)) {
67 return child;
68 }
69 return cloneElement(
70 child,
71 undefined,
72 renderSkillInlineMarkdownChildren(child.props.children, skills),
73 );
74 });
76​
77function SkillChip(props: { skill: InlineSkill; rawText: string }) {
78 return (
79 <span className="inline-flex align-middle leading-none" data-markdown-copy={props.rawText}>
80 <span className={cn(CHAT_INLINE_CHIP_CLASS_NAME, CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.skill)}>
81 <span
82 aria-hidden="true"
83 className={COMPOSER_INLINE_CHIP_ICON_CLASS_NAME}
84 dangerouslySetInnerHTML={{ __html: SKILL_CHIP_ICON_SVG }}
85 />
86 <span className={CHAT_INLINE_CHIP_LABEL_CLASS_NAME}>
87 {formatProviderSkillDisplayName(props.skill)}
88 </span>
89 </span>
90 </span>
91 );