|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { PromptDetail, UserPromptsResponse } from '@/api'; |
| 4 | +import { Badge } from '@/components/ui/badge'; |
| 5 | +import { Button } from '@/components/ui/button'; |
| 6 | +import { |
| 7 | + Card, |
| 8 | + CardContent, |
| 9 | + CardDescription, |
| 10 | + CardFooter, |
| 11 | + CardHeader, |
| 12 | + CardTitle, |
| 13 | +} from '@/components/ui/card'; |
| 14 | +import { |
| 15 | + Dialog, |
| 16 | + DialogClose, |
| 17 | + DialogContent, |
| 18 | + DialogDescription, |
| 19 | + DialogFooter, |
| 20 | + DialogHeader, |
| 21 | + DialogTitle, |
| 22 | + DialogTrigger, |
| 23 | +} from '@/components/ui/dialog'; |
| 24 | +import { Textarea } from '@/components/ui/textarea'; |
| 25 | +import { apiClient } from '@/lib/api/client'; |
| 26 | +import { useTranslations } from 'next-intl'; |
| 27 | +import { useRouter } from 'next/navigation'; |
| 28 | +import { useCallback, useEffect, useState } from 'react'; |
| 29 | +import { toast } from 'sonner'; |
| 30 | + |
| 31 | +type PromptType = 'agent_system' | 'agent_query'; |
| 32 | + |
| 33 | +const PROMPT_TYPES: PromptType[] = ['agent_system', 'agent_query']; |
| 34 | + |
| 35 | +interface PromptCardProps { |
| 36 | + promptType: PromptType; |
| 37 | + detail: PromptDetail | undefined; |
| 38 | + onSaved: () => void; |
| 39 | +} |
| 40 | + |
| 41 | +const PromptCard = ({ promptType, detail, onSaved }: PromptCardProps) => { |
| 42 | + const page_prompts = useTranslations('page_prompts'); |
| 43 | + const common_action = useTranslations('common.action'); |
| 44 | + const [content, setContent] = useState(detail?.content ?? ''); |
| 45 | + const [saving, setSaving] = useState(false); |
| 46 | + const [resetOpen, setResetOpen] = useState(false); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + setContent(detail?.content ?? ''); |
| 50 | + }, [detail?.content]); |
| 51 | + |
| 52 | + const handleSave = useCallback(async () => { |
| 53 | + setSaving(true); |
| 54 | + try { |
| 55 | + await apiClient.defaultApi.promptsUserPut({ |
| 56 | + updateUserPromptsRequest: { |
| 57 | + prompts: { [promptType]: content }, |
| 58 | + }, |
| 59 | + }); |
| 60 | + toast.success(page_prompts('toast.save_success')); |
| 61 | + onSaved(); |
| 62 | + } finally { |
| 63 | + setSaving(false); |
| 64 | + } |
| 65 | + }, [content, promptType, page_prompts, onSaved]); |
| 66 | + |
| 67 | + const handleReset = useCallback(async () => { |
| 68 | + await apiClient.defaultApi.promptsUserPromptTypeDelete({ |
| 69 | + promptType: promptType as never, |
| 70 | + }); |
| 71 | + toast.success(page_prompts('toast.reset_success')); |
| 72 | + setResetOpen(false); |
| 73 | + onSaved(); |
| 74 | + }, [promptType, page_prompts, onSaved]); |
| 75 | + |
| 76 | + const isCustomized = detail?.customized === true; |
| 77 | + |
| 78 | + return ( |
| 79 | + <Card> |
| 80 | + <CardHeader> |
| 81 | + <div className="flex items-center justify-between gap-2"> |
| 82 | + <CardTitle>{page_prompts(`${promptType}.title` as never)}</CardTitle> |
| 83 | + <Badge variant={isCustomized ? 'default' : 'secondary'}> |
| 84 | + {isCustomized |
| 85 | + ? page_prompts('status.customized') |
| 86 | + : page_prompts('status.default')} |
| 87 | + </Badge> |
| 88 | + </div> |
| 89 | + <CardDescription> |
| 90 | + {page_prompts(`${promptType}.description` as never)} |
| 91 | + </CardDescription> |
| 92 | + </CardHeader> |
| 93 | + |
| 94 | + <CardContent> |
| 95 | + <Textarea |
| 96 | + className="min-h-[160px] max-h-[360px] font-mono text-sm resize-y" |
| 97 | + value={content} |
| 98 | + onChange={(e) => setContent(e.target.value)} |
| 99 | + placeholder={detail?.content ?? ''} |
| 100 | + /> |
| 101 | + </CardContent> |
| 102 | + |
| 103 | + <CardFooter className="justify-end gap-2"> |
| 104 | + {isCustomized && ( |
| 105 | + <Dialog open={resetOpen} onOpenChange={setResetOpen}> |
| 106 | + <DialogTrigger asChild> |
| 107 | + <Button variant="outline"> |
| 108 | + {page_prompts('action.reset')} |
| 109 | + </Button> |
| 110 | + </DialogTrigger> |
| 111 | + <DialogContent> |
| 112 | + <DialogHeader> |
| 113 | + <DialogTitle>{page_prompts('action.reset_confirm')}</DialogTitle> |
| 114 | + <DialogDescription> |
| 115 | + {page_prompts('action.reset_confirm_description')} |
| 116 | + </DialogDescription> |
| 117 | + </DialogHeader> |
| 118 | + <DialogFooter> |
| 119 | + <DialogClose asChild> |
| 120 | + <Button variant="outline">{common_action('cancel')}</Button> |
| 121 | + </DialogClose> |
| 122 | + <Button variant="destructive" onClick={handleReset}> |
| 123 | + {page_prompts('action.reset')} |
| 124 | + </Button> |
| 125 | + </DialogFooter> |
| 126 | + </DialogContent> |
| 127 | + </Dialog> |
| 128 | + )} |
| 129 | + <Button onClick={handleSave} disabled={saving}> |
| 130 | + {page_prompts('action.save')} |
| 131 | + </Button> |
| 132 | + </CardFooter> |
| 133 | + </Card> |
| 134 | + ); |
| 135 | +}; |
| 136 | + |
| 137 | +export const PromptSettings = ({ data }: { data: UserPromptsResponse }) => { |
| 138 | + const router = useRouter(); |
| 139 | + |
| 140 | + const handleSaved = useCallback(() => { |
| 141 | + setTimeout(router.refresh, 300); |
| 142 | + }, [router]); |
| 143 | + |
| 144 | + return ( |
| 145 | + <div className="flex flex-col gap-6"> |
| 146 | + {PROMPT_TYPES.map((promptType) => ( |
| 147 | + <PromptCard |
| 148 | + key={promptType} |
| 149 | + promptType={promptType} |
| 150 | + detail={data[promptType]} |
| 151 | + onSaved={handleSaved} |
| 152 | + /> |
| 153 | + ))} |
| 154 | + </div> |
| 155 | + ); |
| 156 | +}; |
0 commit comments