|
| 1 | +import Editor, { type Theme } from "@monaco-editor/react"; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Card, |
| 5 | + CardBody, |
| 6 | + CardFooter, |
| 7 | + DarkModeContext, |
| 8 | + LinkButton, |
| 9 | + Text, |
| 10 | +} from "@stacklok/ui-kit"; |
| 11 | +import { |
| 12 | + Dispatch, |
| 13 | + SetStateAction, |
| 14 | + useContext, |
| 15 | + useEffect, |
| 16 | + useState, |
| 17 | +} from "react"; |
| 18 | +import { usePostSystemPrompt } from "../hooks/use-post-system-prompt"; |
| 19 | +import { Check } from "lucide-react"; |
| 20 | + |
| 21 | +type DarkModeContextValue = { |
| 22 | + preference: "dark" | "light" | null; |
| 23 | + override: "dark" | "light" | null; |
| 24 | +}; |
| 25 | + |
| 26 | +const DEFAULT_VALUE = `You are a security expert conducting a thorough code review.\nIdentify potential security vulnerabilities, suggest improvements, and explain security best practices.`; |
| 27 | + |
| 28 | +function inferDarkMode( |
| 29 | + contextValue: |
| 30 | + | null |
| 31 | + | [DarkModeContextValue, Dispatch<SetStateAction<DarkModeContextValue>>], |
| 32 | +): Theme { |
| 33 | + if (contextValue === null) return "light"; |
| 34 | + |
| 35 | + // Handle override |
| 36 | + if (contextValue[0].override === "dark") return "vs-dark"; |
| 37 | + if (contextValue[0].override === "light") return "light"; |
| 38 | + |
| 39 | + // Handle preference |
| 40 | + if (contextValue[0].preference === "dark") return "vs-dark"; |
| 41 | + return "light"; |
| 42 | +} |
| 43 | + |
| 44 | +function useSavedStatus() { |
| 45 | + const [saved, setSaved] = useState<boolean>(false); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + const id = setTimeout(() => setSaved(false), 2000); |
| 49 | + return () => clearTimeout(id); |
| 50 | + }, [saved]); |
| 51 | + |
| 52 | + return { saved, setSaved }; |
| 53 | +} |
| 54 | + |
| 55 | +export function SystemPromptEditor({ className }: { className?: string }) { |
| 56 | + const context = useContext(DarkModeContext); |
| 57 | + const theme: Theme = inferDarkMode(context); |
| 58 | + |
| 59 | + const [value, setValue] = useState<string>(() => DEFAULT_VALUE); |
| 60 | + |
| 61 | + const { mutate, isPending } = usePostSystemPrompt(); |
| 62 | + |
| 63 | + const { saved, setSaved } = useSavedStatus(); |
| 64 | + |
| 65 | + return ( |
| 66 | + <Card className={className}> |
| 67 | + <CardBody> |
| 68 | + <Text className="text-primary">Custom prompt</Text> |
| 69 | + <Text className="text-secondary mb-4"> |
| 70 | + Pass custom instructions to your LLM to augment it's behavior, and |
| 71 | + save time & tokens. |
| 72 | + </Text> |
| 73 | + <div className="border border-gray-200 rounded overflow-hidden"> |
| 74 | + <Editor |
| 75 | + options={{ |
| 76 | + minimap: { enabled: false }, |
| 77 | + }} |
| 78 | + value={value} |
| 79 | + onChange={(v) => setValue(v ?? "")} |
| 80 | + height="20rem" |
| 81 | + defaultLanguage="Markdown" |
| 82 | + theme={theme} |
| 83 | + className="bg-base" |
| 84 | + defaultValue="<!-- Add any additional prompts you would like to pass to your LLM here. -->" |
| 85 | + /> |
| 86 | + </div> |
| 87 | + </CardBody> |
| 88 | + <CardFooter className="justify-end gap-2"> |
| 89 | + <LinkButton variant="secondary">Cancel</LinkButton> |
| 90 | + <Button |
| 91 | + isPending={isPending} |
| 92 | + isDisabled={saved} |
| 93 | + onPress={() => { |
| 94 | + mutate(value, { |
| 95 | + onSuccess: () => setSaved(true), |
| 96 | + }); |
| 97 | + }} |
| 98 | + > |
| 99 | + {saved ? ( |
| 100 | + <> |
| 101 | + <span>Saved</span> <Check /> |
| 102 | + </> |
| 103 | + ) : ( |
| 104 | + "Save changes" |
| 105 | + )} |
| 106 | + </Button> |
| 107 | + </CardFooter> |
| 108 | + </Card> |
| 109 | + ); |
| 110 | +} |
0 commit comments