Elffuss BraidHub

kikocis/continue public

1ff75182e19e · 5.5 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 const canEdit =
64 prompt.promptFile && !prompt.promptFile.startsWith("builtin:");
65
66 return (
67 <div
68 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"
69 onClick={handlePromptClick}
70 style={{
71 fontSize: fontSize(-3),
72 }}
73 >
74 <div className="flex min-w-0 flex-col">
75 <span className="text-vscForeground shrink-0 font-medium">
76 {prompt.name}
77 </span>
78 <span className="line-clamp-2 text-[11px] text-gray-400">
79 {prompt.description}
80 </span>
81 </div>
82 <div className="flex items-center gap-2">
83 <PencilIcon
84 className={`h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125 ${!canEdit ? "pointer-events-none cursor-not-allowed opacity-50" : ""}`}
85 onClick={canEdit ? handleEditClick : undefined}
86 aria-disabled={!canEdit}
87 />
88 <div
89 onClick={handleBookmarkClick}
90 className="cursor-pointer pt-0.5 text-gray-400 hover:brightness-125"
91 >
92 {isBookmarked ? (
93 <BookmarkSolid className="h-3 w-3" />
94 ) : (
95 <BookmarkOutline className="h-3 w-3" />
96 )}
97 </div>
98 </div>
99 </div>
100 );
101}
102
103/**
104 * Section that displays all available prompts with bookmarking functionality
105 */
106export function PromptsSection() {
107 const { selectedProfile } = useAuth();
108 const { isCommandBookmarked, toggleBookmark } = useBookmarkedSlashCommands();
109 const ideMessenger = useContext(IdeMessengerContext);
110
111 const slashCommands = useAppSelector(
112 (state) => state.config.config.slashCommands ?? [],
113 );
114
115 const handleEdit = (prompt: PromptCommandWithSlug) => {
116 if (prompt.promptFile) {
117 ideMessenger.post("openFile", {
118 path: prompt.promptFile,
119 });
120 } else if (prompt.slug) {
121 void ideMessenger.request("controlPlane/openUrl", {
122 path: `${prompt.slug}/new-version`,
123 orgSlug: undefined,
124 });
125 } else {
126 ideMessenger.post("config/openProfile", {
127 profileId: undefined,
128 element: { sourceFile: (prompt as any).sourceFile },
129 });
130 }
131 };
132
133 const sortedCommands = useMemo(() => {
134 const promptsWithSlug: PromptCommandWithSlug[] =
135 structuredClone(slashCommands);
136 // get the slugs from rawYaml
137 if (selectedProfile?.rawYaml) {
138 const parsed = parseConfigYaml(selectedProfile.rawYaml);
139 const parsedPrompts = parsed.prompts ?? [];
140
141 let index = 0;
142 for (const commandWithSlug of promptsWithSlug) {
143 // skip for local prompt files
144 if (commandWithSlug.promptFile) continue;
145
146 const yamlPrompt = parsedPrompts[index];
147 if (yamlPrompt) {
148 if ("uses" in yamlPrompt) {
149 commandWithSlug.slug = yamlPrompt.uses;
150 } else {
151 commandWithSlug.slug = `${selectedProfile?.fullSlug.ownerSlug}/${selectedProfile?.fullSlug.packageSlug}`;
152 }
153 }
154 index = index + 1;
155 }
156 }
157 return promptsWithSlug.sort((a, b) => {
158 const aBookmarked = isCommandBookmarked(a.name);
159 const bBookmarked = isCommandBookmarked(b.name);
160 if (aBookmarked && !bBookmarked) return -1;
161 if (!aBookmarked && bBookmarked) return 1;
162 return 0;
163 });
164 }, [slashCommands, isCommandBookmarked, selectedProfile]);
165
166 return (
167 <div className="flex flex-col">
168 {sortedCommands.map((prompt) => (
169 <PromptRow
170 key={prompt.name}
171 prompt={prompt}
172 isBookmarked={isCommandBookmarked(prompt.name)}
173 setIsBookmarked={() => toggleBookmark(prompt)}
174 onEdit={() => handleEdit(prompt)}
175 />
176 ))}
177 <ExploreBlocksButton blockType="prompts" />
178 </div>
179 );
180}