Elffuss BraidHub

kikocis/continue public

cfc91aa76b54 · 5.3 KiBRawPermalink
1import { parseConfigYaml } from "@continuedev/config-yaml";
2import {
3 BookmarkIcon as BookmarkOutline,
4 PencilIcon,
5} from "@heroicons/react/24/outline";
6import { BookmarkIcon as BookmarkSolid } from "@heroicons/react/24/solid";
7import { SlashCommandDescWithSource } from "core";
8import { useContext, useMemo } from "react";
9import { useAuth } from "../../../../context/Auth";
10import { IdeMessengerContext } from "../../../../context/IdeMessenger";
11import { useBookmarkedSlashCommands } from "../../../../hooks/useBookmarkedSlashCommands";
12import { useAppSelector } from "../../../../redux/hooks";
13import { fontSize } from "../../../../util";
14import { useMainEditor } from "../../TipTapEditor";
15import { useLump } from "../LumpContext";
16import { ExploreBlocksButton } from "./ExploreBlocksButton";
17
18interface PromptCommandWithSlug extends SlashCommandDescWithSource {
19 slug?: string;
20}
21
22interface PromptRowProps {
23 prompt: PromptCommandWithSlug;
24 isBookmarked: boolean;
25 setIsBookmarked: (isBookmarked: boolean) => void;
26 onEdit?: () => void;
27}
28
29/**
30 * Displays a single prompt row with bookmark and edit controls
31 */
32function PromptRow({
33 prompt,
34 isBookmarked,
35 setIsBookmarked,
36 onEdit,
37}: PromptRowProps) {
38 const { mainEditor } = useMainEditor();
39 const { hideLump } = useLump();
40
41 const handlePromptClick = (e: React.MouseEvent) => {
42 e.stopPropagation();
43 mainEditor?.commands.insertPrompt({
44 title: prompt.name,
45 description: prompt.description,
46 content: prompt.prompt,
47 });
48 hideLump();
49 };
50
51 const handleBookmarkClick = (e: React.MouseEvent) => {
52 e.stopPropagation();
53 setIsBookmarked(!isBookmarked);
54 };
55
56 const handleEditClick = (e: React.MouseEvent) => {
57 e.stopPropagation();
58 if (onEdit) {
59 onEdit();
60 }
61 };
62
63 return (
64 <div
65 className="hover:bg-list-active hover:text-list-active-foreground flex items-center justify-between gap-3 rounded-md px-2 py-1 hover:cursor-pointer"
66 onClick={handlePromptClick}
67 style={{
68 fontSize: fontSize(-3),
69 }}
70 >
71 <div className="flex min-w-0 flex-col">
72 <span className="text-vscForeground shrink-0 font-medium">
73 {prompt.name}
74 </span>
75 <span className="line-clamp-2 text-[11px] text-gray-400">
76 {prompt.description}
77 </span>
78 </div>
79 <div className="flex items-center gap-2">
80 <PencilIcon
81 className="h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125"
82 onClick={handleEditClick}
83 />
84 <div
85 onClick={handleBookmarkClick}
86 className="cursor-pointer pt-0.5 text-gray-400 hover:brightness-125"
87 >
88 {isBookmarked ? (
89 <BookmarkSolid className="h-3 w-3" />
90 ) : (
91 <BookmarkOutline className="h-3 w-3" />
92 )}
93 </div>
94 </div>
95 </div>
96 );
97}
98
99/**
100 * Section that displays all available prompts with bookmarking functionality
101 */
102export function PromptsSection() {
103 const { selectedProfile } = useAuth();
104 const { isCommandBookmarked, toggleBookmark } = useBookmarkedSlashCommands();
105 const ideMessenger = useContext(IdeMessengerContext);
106
107 const slashCommands = useAppSelector(
108 (state) => state.config.config.slashCommands ?? [],
109 );
110
111 const handleEdit = (prompt: PromptCommandWithSlug) => {
112 if (prompt.promptFile) {
113 ideMessenger.post("openFile", {
114 path: prompt.promptFile,
115 });
116 } else if (prompt.slug) {
117 void ideMessenger.request("controlPlane/openUrl", {
118 path: `${prompt.slug}/new-version`,
119 orgSlug: undefined,
120 });
121 } else {
122 ideMessenger.post("config/openProfile", {
123 profileId: undefined,
124 element: { sourceFile: (prompt as any).sourceFile },
125 });
126 }
127 };
128
129 const sortedCommands = useMemo(() => {
130 const promptsWithSlug: PromptCommandWithSlug[] =
131 structuredClone(slashCommands);
132 // get the slugs from rawYaml
133 if (selectedProfile?.rawYaml) {
134 const parsed = parseConfigYaml(selectedProfile.rawYaml);
135 const parsedPrompts = parsed.prompts ?? [];
136
137 let index = 0;
138 for (const commandWithSlug of promptsWithSlug) {
139 // skip for local prompt files
140 if (commandWithSlug.promptFile) continue;
141
142 const yamlPrompt = parsedPrompts[index];
143 if (yamlPrompt) {
144 if ("uses" in yamlPrompt) {
145 commandWithSlug.slug = yamlPrompt.uses;
146 } else {
147 commandWithSlug.slug = `${selectedProfile?.fullSlug.ownerSlug}/${selectedProfile?.fullSlug.packageSlug}`;
148 }
149 }
150 index = index + 1;
151 }
152 }
153 return promptsWithSlug.sort((a, b) => {
154 const aBookmarked = isCommandBookmarked(a.name);
155 const bBookmarked = isCommandBookmarked(b.name);
156 if (aBookmarked && !bBookmarked) return -1;
157 if (!aBookmarked && bBookmarked) return 1;
158 return 0;
159 });
160 }, [slashCommands, isCommandBookmarked, selectedProfile]);
161
162 return (
163 <div className="flex flex-col">
164 {sortedCommands.map((prompt) => (
165 <PromptRow
166 key={prompt.name}
167 prompt={prompt}
168 isBookmarked={isCommandBookmarked(prompt.name)}
169 setIsBookmarked={() => toggleBookmark(prompt)}
170 onEdit={() => handleEdit(prompt)}
171 />
172 ))}
173 <ExploreBlocksButton blockType="prompts" />
174 </div>
175 );
176}