| 1 | import { ConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { PencilIcon } from "@heroicons/react/24/outline"; |
| 3 | import { useContext } from "react"; |
| 4 | import { useAuth } from "../../../context/Auth"; |
| 5 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 6 | |
| 7 | type SectionKey = Exclude< |
| 8 | keyof ConfigYaml, |
| 9 | "name" | "version" | "schema" | "metadata" | "env" | "requestOptions" |
| 10 | >; |
| 11 | |
| 12 | interface EditBlockButtonProps<T extends SectionKey> { |
| 13 | blockType: T; |
| 14 | block?: NonNullable<Omit<ConfigYaml, "env">[T]>[number]; |
| 15 | className?: string; |
| 16 | sourceFile?: string; |
| 17 | } |
| 18 | |
| 19 | function isUsesBlock(block: any): block is { uses: string } { |
| 20 | return typeof block !== "string" && "uses" in block; |
| 21 | } |
| 22 | |
| 23 | export default function EditBlockButton<T extends SectionKey>({ |
| 24 | block, |
| 25 | blockType, |
| 26 | className = "", |
| 27 | sourceFile, |
| 28 | }: EditBlockButtonProps<T>) { |
| 29 | const ideMessenger = useContext(IdeMessengerContext); |
| 30 | const { selectedProfile } = useAuth(); |
| 31 | |
| 32 | const openUrl = (path: string) => |
| 33 | ideMessenger.request("controlPlane/openUrl", { |
| 34 | path, |
| 35 | orgSlug: undefined, |
| 36 | }); |
| 37 | |
| 38 | const handleEdit = () => { |
| 39 | if (selectedProfile?.profileType === "local") { |
| 40 | ideMessenger.post("config/openProfile", { |
| 41 | profileId: undefined, |
| 42 | element: { sourceFile }, |
| 43 | }); |
| 44 | } else if (block && isUsesBlock(block)) { |
| 45 | void openUrl(`${block.uses}/new-version`); |
| 46 | } else if (selectedProfile?.fullSlug) { |
| 47 | const slug = `${selectedProfile.fullSlug.ownerSlug}/${selectedProfile.fullSlug.packageSlug}`; |
| 48 | void openUrl(`${slug}/new-version`); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | return ( |
| 53 | <PencilIcon |
| 54 | className={`h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125 ${className}`} |
| 55 | onClick={handleEdit} |
| 56 | /> |
| 57 | ); |
| 58 | } |