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