All files / src/components/personas PersonaManager.tsx

65.21% Statements 15/23
80% Branches 20/25
57.14% Functions 8/14
70% Lines 14/20

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                                                              25x 25x   25x 2x 2x     25x                                     21x 21x 21x         3x                                                 2x                                       2x                                                 2x 2x                
import { useState, type JSX } from "react";
import type { PersonaData } from "../../hooks/types.js";
import { Button } from "../display/Button.js";
import { ConfirmDialog } from "../display/index.js";
import styles from "./PersonaManager.module.scss";
 
/** Props for the PersonaManager component. */
export interface PersonaManagerProps {
  /** All personas. */
  personas: PersonaData[];
  /** The app-level default persona ID. */
  appDefaultPersonaId: string;
  /** Callback to delete a persona. */
  onDeletePersona: (personaId: string) => Promise<void>;
  /** Callback to set the app-level default persona. */
  onSetAppDefaultPersonaId: (personaId: string) => Promise<void>;
  /** Navigate to the new persona page. */
  onNavigateToNew: () => void;
  /** Navigate to a persona's detail page for editing. */
  onNavigateToPersona: (personaId: string) => void;
}
 
/** Persona list view — shows cards and navigates to detail pages for create/edit. */
export function PersonaManager({
  personas,
  appDefaultPersonaId,
  onDeletePersona,
  onSetAppDefaultPersonaId,
  onNavigateToNew,
  onNavigateToPersona,
}: PersonaManagerProps): JSX.Element {
  const [confirmDelete, setConfirmDelete] = useState<string | null>(null);
  const personaToDelete = confirmDelete ? personas.find((p) => p.id === confirmDelete) : undefined;
 
  const handleDelete = async (id: string): Promise<void> => {
    await onDeletePersona(id);
    setConfirmDelete(null);
  };
 
  return (
    <div className={styles.container}>
      <div className={styles.header}>
        <h2>Personas</h2>
        <Button
          variant="primary"
          size="md"
          onClick={onNavigateToNew}
          data-testid="persona-new-button"
        >
          + New Persona
        </Button>
      </div>
 
      {personas.length === 0 ? (
        <p className={styles.empty}>No personas yet. Create one to get started.</p>
      ) : (
        <div className={styles.list}>
          {personas.map((p) => {
            const isAppDefault = appDefaultPersonaId === p.id;
            const isScript = p.type === "script";
            return (
              <div
                key={p.id}
                className={styles.card}
                data-testid={`persona-card-${p.id}`}
                onClick={() => onNavigateToPersona(p.id)}
                role="button"
                tabIndex={0}
                onKeyDown={(e) => {
                  Iif (e.currentTarget === e.target && (e.key === "Enter" || e.key === " ")) {
                    e.preventDefault();
                    onNavigateToPersona(p.id);
                  }
                }}
              >
                <div className={styles.cardHeader}>
                  <span className={styles.cardTitle}>
                    <strong>{p.name}</strong>
                    <span className={styles.typeBadge} data-testid={`persona-type-badge-${p.id}`}>
                      {isScript ? "Script" : "Agent"}
                    </span>
                    {isAppDefault && (
                      <span
                        className={styles.defaultBadge}
                        data-testid={`persona-default-badge-${p.id}`}
                      >
                        App Default
                      </span>
                    )}
                  </span>
                  <div className={styles.cardActions} onClick={(e) => e.stopPropagation()}>
                    {!isAppDefault && (
                      <Button
                        variant="ghost"
                        size="sm"
                        onClick={() => {
                          onSetAppDefaultPersonaId(p.id).catch(() => undefined);
                        }}
                        data-testid={`persona-set-default-${p.id}`}
                        title="Set as app default persona"
                      >
                        Set Default
                      </Button>
                    )}
                    <Button variant="ghost" size="sm" onClick={() => onNavigateToPersona(p.id)}>
                      Edit
                    </Button>
                    <Button
                      variant="ghost"
                      size="sm"
                      onClick={() => setConfirmDelete(p.id)}
                      data-testid={`persona-delete-${p.id}`}
                    >
                      Delete
                    </Button>
                  </div>
                </div>
                {p.description && <p className={styles.description}>{p.description}</p>}
                <div className={styles.meta}>
                  {p.runtime && <span>Runtime: {p.runtime}</span>}
                  {p.model && <span>Model: {p.model}</span>}
                  {p.maxTurns > 0 && <span>Max turns: {p.maxTurns}</span>}
                </div>
              </div>
            );
          })}
        </div>
      )}
 
      <ConfirmDialog
        isOpen={confirmDelete !== null}
        title="Delete Persona?"
        description={`"${personaToDelete?.name ?? ""}" will be permanently removed.`}
        confirmLabel="Delete"
        onConfirm={() => {
          if (confirmDelete) {
            handleDelete(confirmDelete).catch(() => undefined);
          }
        }}
        onCancel={() => setConfirmDelete(null)}
      />
    </div>
  );
}