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

SkillInlineText.tsx

121 lines3.7 KB

1import type { ScopedThreadRef, ServerProviderSkill } from "@t3tools/contracts";
2import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
3import { Children, cloneElement, isValidElement, type ReactNode } from "react";
4import { matchInlineSkills } from "@t3tools/shared/inlineSkills";
5​
6import { useRightPanelStore } from "~/rightPanelStore";
7import { SKILL_CHIP_ICON_SVG } from "../composerInlineChip";
8import { ContextChipPopover } from "../contextChipParts";
9import { Button } from "../ui/button";
10​
11function SkillInlineText(props: {
12 text: string;
13 skills: ReadonlyArray<ServerProviderSkill>;
14 threadRef: ScopedThreadRef | undefined;
15}) {
16 const nodes: ReactNode[] = [];
17 let cursor = 0;
18​
19 for (const { start, end, skill, rawText } of matchInlineSkills(props.text, props.skills)) {
20 if (start > cursor) {
21 nodes.push(props.text.slice(cursor, start));
22 }
23 nodes.push(
24 <SkillChipPopover
25 key={`${start}:${skill.name}`}
26 label={formatProviderSkillDisplayName(skill)}
27 description={skillPopoverDescription(skill)}
28 copyMarkdown={rawText}
29 onViewInstructions={skillInstructionsOpener(props.threadRef, skill)}
30 />,
31 );
32 cursor = end;
33 }
34​
35 if (cursor === 0) {
36 return <>{props.text}</>;
37 }
38 if (cursor < props.text.length) {
39 nodes.push(props.text.slice(cursor));
40 }
41 return <>{nodes}</>;
43​
44export function renderSkillInlineMarkdownChildren(
45 children: ReactNode,
46 skills: ReadonlyArray<ServerProviderSkill>,
47 threadRef: ScopedThreadRef | undefined,
48): ReactNode {
49 return Children.map(children, (child) => {
50 if (typeof child === "string") {
51 return <SkillInlineText text={child} skills={skills} threadRef={threadRef} />;
52 }
53 if (!isValidElement<{ children?: ReactNode; node?: { tagName?: string } }>(child)) {
54 return child;
55 }
56 // Custom markdown components replace intrinsic types; check their hast tags.
57 const markdownTagName = typeof child.type === "string" ? child.type : child.props.node?.tagName;
58 if (markdownTagName === "code" || markdownTagName === "a") {
59 return child;
60 }
61 if (!("children" in child.props)) {
62 return child;
63 }
64 return cloneElement(
65 child,
66 undefined,
67 renderSkillInlineMarkdownChildren(child.props.children, skills, threadRef),
68 );
69 });
71​
72export function skillPopoverDescription(
73 skill: Pick<ServerProviderSkill, "description" | "shortDescription">,
74) {
75 return skill.description?.trim() || skill.shortDescription?.trim() || null;
77​
78export function skillInstructionsOpener(
79 threadRef: ScopedThreadRef | null | undefined,
80 skill: Pick<ServerProviderSkill, "path"> | undefined,
81) {
82 if (!threadRef || !skill?.path) return undefined;
83 return () => useRightPanelStore.getState().openFile(threadRef, skill.path);
85​
86export function SkillChipPopover(props: {
87 label: string;
88 description: string | null | undefined;
89 copyMarkdown?: string | undefined;
90 onViewInstructions: (() => void) | undefined;
91}) {
92 return (
93 <ContextChipPopover
94 kind="skill"
95 icon={<SkillChipIcon />}
96 label={props.label}
97 accessibleLabel={`Skill ${props.label}`}
98 copyMarkdown={props.copyMarkdown}
99 >
100 <div className="space-y-3 p-2 text-sm">
101 <p className="font-medium">{props.label}</p>
102 <p>{props.description || "No description is available for this skill."}</p>
103 {props.onViewInstructions ? (
104 <Button variant="outline" size="sm" onClick={props.onViewInstructions}>
105 View instructions
106 </Button>
107 ) : null}
108 </div>
109 </ContextChipPopover>
110 );
112​
113function SkillChipIcon() {
114 return (
115 <span
116 aria-hidden="true"
117 className="contents"
118 dangerouslySetInnerHTML={{ __html: SKILL_CHIP_ICON_SVG }}
119 />
120 );