All files / src/components/settings PromptCard.tsx

74.41% Statements 32/43
80% Branches 24/30
87.5% Functions 14/16
75.6% Lines 31/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324                                                                22x                                             2x                                                                 7x                                 7x                       1x                                                 7x                                                                               7x                   1x                                                                         22x 22x 22x 22x   22x   22x 4x 4x               22x 1x 1x     22x 1x 1x 1x 1x 1x 1x 1x 1x                               1x       22x               3x                                                                        
import { useState } from 'react'
import {
  Check,
  X,
  ChevronDown,
  ChevronUp,
  Loader2,
} from 'lucide-react'
import { toast } from 'sonner'
import { api, type AIPrompt, type AIModel } from '../../api/client'
import { cn } from '../../lib/utils'
 
interface PromptEditForm {
  system_prompt: string
  user_prompt_template: string
  model: string
  is_active: boolean
}
 
interface PromptCardProps {
  prompt: AIPrompt
  models: AIModel[]
  onPromptUpdated: (updated: AIPrompt) => void
}
 
function PromptCardHeader({
  prompt,
  modelName,
}: {
  prompt: AIPrompt
  modelName: string
}) {
  return (
    <div className="flex-1">
      <div className="flex items-center gap-2">
        <h3 className="font-medium text-foreground">{prompt.name}</h3>
        {!prompt.is_active && (
          <span className="rounded bg-muted px-2 py-0.5 text-xs text-muted-foreground">
            Disabled
          </span>
        )}
      </div>
      <p className="mt-1 text-sm text-muted-foreground">
        {prompt.description}
      </p>
      <div className="mt-2 flex items-center gap-2">
        <span className="rounded bg-primary/10 px-2 py-0.5 text-xs font-medium text-primary">
          {modelName}
        </span>
      </div>
    </div>
  )
}
 
function PromptReadOnlyView({ prompt }: { prompt: AIPrompt }) {
  return (
    <div className="border-t border-border px-4 py-4">
      <div className="space-y-4">
        <div>
          <label className="mb-1 block text-sm font-medium text-muted-foreground">
            System Prompt
          </label>
          <pre className="whitespace-pre-wrap rounded-lg bg-muted p-3 text-sm text-foreground">
            {prompt.system_prompt}
          </pre>
        </div>
        <div>
          <label className="mb-1 block text-sm font-medium text-muted-foreground">
            User Prompt Template
          </label>
          <pre className="whitespace-pre-wrap rounded-lg bg-muted p-3 text-sm text-foreground">
            {prompt.user_prompt_template}
          </pre>
        </div>
      </div>
    </div>
  )
}
 
function PromptModelControls({
  editForm,
  models,
  onFormChange,
}: {
  editForm: PromptEditForm
  models: AIModel[]
  onFormChange: (form: PromptEditForm) => void
}) {
  return (
    <div className="flex gap-4">
      <div className="flex-1">
        <label className="mb-1 block text-sm font-medium text-foreground">
          Model
        </label>
        <select
          value={editForm.model}
          onChange={(e) =>
            onFormChange({ ...editForm, model: e.target.value })
          }
          className="w-full rounded-lg border border-border bg-input-background px-3 py-2 text-foreground focus:outline-none focus:ring-2 focus:ring-ring"
        >
          {models.length === 0 && (
            <option value={editForm.model}>{editForm.model}</option>
          )}
          {models.map((model) => (
            <option key={model.id} value={model.id}>
              {model.name}
            </option>
          ))}
        </select>
      </div>
      <div>
        <label className="mb-1 block text-sm font-medium text-foreground">
          Status
        </label>
        <button
          onClick={() =>
            onFormChange({ ...editForm, is_active: !editForm.is_active })
          }
          className={cn(
            'rounded-lg px-4 py-2 text-sm font-medium transition-colors',
            editForm.is_active
              ? 'bg-green-500/10 text-green-600 hover:bg-green-500/20 dark:text-green-400'
              : 'bg-muted text-muted-foreground hover:bg-muted/80'
          )}
        >
          {editForm.is_active ? 'Active' : 'Disabled'}
        </button>
      </div>
    </div>
  )
}
 
function PromptEditActions({
  saving,
  onSave,
  onCancel,
}: {
  saving: boolean
  onSave: () => void
  onCancel: () => void
}) {
  return (
    <div className="flex justify-end gap-2">
      <button
        onClick={onCancel}
        className="flex items-center gap-2 rounded-lg border border-border bg-background px-4 py-2 text-sm font-medium text-foreground transition-colors hover:bg-muted"
      >
        <X className="h-4 w-4" />
        Cancel
      </button>
      <button
        onClick={onSave}
        disabled={saving}
        className="flex items-center gap-2 rounded-lg bg-primary px-4 py-2 text-sm font-medium text-primary-foreground transition-colors hover:bg-primary/90 disabled:cursor-not-allowed disabled:opacity-50"
      >
        {saving ? (
          <Loader2 className="h-4 w-4 animate-spin" />
        ) : (
          <Check className="h-4 w-4" />
        )}
        Save Changes
      </button>
    </div>
  )
}
 
function PromptEditView({
  editForm,
  models,
  saving,
  onFormChange,
  onSave,
  onCancel,
}: {
  editForm: PromptEditForm
  models: AIModel[]
  saving: boolean
  onFormChange: (form: PromptEditForm) => void
  onSave: () => void
  onCancel: () => void
}) {
  return (
    <div className="border-t border-border px-4 py-4">
      <div className="space-y-4">
        <div>
          <label className="mb-1 block text-sm font-medium text-foreground">
            System Prompt
          </label>
          <textarea
            value={editForm.system_prompt}
            onChange={(e) =>
              onFormChange({ ...editForm, system_prompt: e.target.value })
            }
            rows={6}
            className="w-full rounded-lg border border-border bg-input-background px-3 py-2 font-mono text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring"
          />
        </div>
        <div>
          <label className="mb-1 block text-sm font-medium text-foreground">
            User Prompt Template
          </label>
          <textarea
            value={editForm.user_prompt_template}
            onChange={(e) =>
              onFormChange({
                ...editForm,
                user_prompt_template: e.target.value,
              })
            }
            rows={4}
            className="w-full rounded-lg border border-border bg-input-background px-3 py-2 font-mono text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring"
          />
          <p className="mt-1 text-xs text-muted-foreground">
            Use {'{placeholders}'} for dynamic content
          </p>
        </div>
        <PromptModelControls
          editForm={editForm}
          models={models}
          onFormChange={onFormChange}
        />
        <PromptEditActions saving={saving} onSave={onSave} onCancel={onCancel} />
      </div>
    </div>
  )
}
 
export default function PromptCard({ prompt, models, onPromptUpdated }: PromptCardProps) {
  const [isEditing, setIsEditing] = useState(false)
  const [isExpanded, setIsExpanded] = useState(false)
  const [editForm, setEditForm] = useState<PromptEditForm | null>(null)
  const [saving, setSaving] = useState(false)
 
  const modelName = models.find((m) => m.id === prompt.model)?.name || prompt.model
 
  const handleEdit = () => {
    setIsEditing(true)
    setEditForm({
      system_prompt: prompt.system_prompt,
      user_prompt_template: prompt.user_prompt_template,
      model: prompt.model,
      is_active: prompt.is_active,
    })
  }
 
  const handleCancel = () => {
    setIsEditing(false)
    setEditForm(null)
  }
 
  const handleSave = async () => {
    Iif (!editForm) return
    setSaving(true)
    try {
      const updated = await api.ai.prompts.update(prompt.prompt_type, editForm)
      onPromptUpdated(updated)
      toast.success('Prompt saved successfully')
      setIsEditing(false)
      setEditForm(null)
    } catch (error) {
      console.error('Failed to save prompt:', error)
      let errorMessage = 'Failed to save prompt'
      if (error instanceof Error) {
        try {
          const parsed = JSON.parse(error.message)
          if (parsed.message) {
            errorMessage = parsed.message
          }
        } catch {
          // Not JSON
        }
      }
      toast.error(errorMessage)
    } finally {
      setSaving(false)
    }
  }
 
  return (
    <div className="rounded-lg border border-border bg-card">
      <div className="flex items-center justify-between p-4">
        <PromptCardHeader prompt={prompt} modelName={modelName} />
        <div className="flex items-center gap-2">
          {!isEditing && (
            <>
              <button
                onClick={() => setIsExpanded(!isExpanded)}
                className="rounded-lg p-2 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
                aria-label={isExpanded ? 'Collapse' : 'Expand'}
              >
                {isExpanded ? (
                  <ChevronUp className="h-5 w-5" />
                ) : (
                  <ChevronDown className="h-5 w-5" />
                )}
              </button>
              <button
                onClick={handleEdit}
                className="rounded-lg bg-primary/10 px-3 py-1.5 text-sm font-medium text-primary transition-colors hover:bg-primary/20"
              >
                Edit
              </button>
            </>
          )}
        </div>
      </div>
 
      {isExpanded && !isEditing && <PromptReadOnlyView prompt={prompt} />}
 
      {isEditing && editForm && (
        <PromptEditView
          editForm={editForm}
          models={models}
          saving={saving}
          onFormChange={setEditForm}
          onSave={handleSave}
          onCancel={handleCancel}
        />
      )}
    </div>
  )
}
 
← Back to Dashboard