All files / src/components/panels TokensPanel.tsx

93.93% Statements 31/33
77.77% Branches 14/18
91.66% Functions 11/12
93.93% Lines 31/33

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                  7x                                                           253x 253x 253x 253x 253x   253x 6x 6x     6x 6x 6x 6x 6x 6x 6x     253x 4x     253x 4x 4x 4x   4x     253x                                               759x               4x                                   80x             58x             3x     506x                           70x                      
import { useState, type JSX, type FormEvent } from "react";
import { X } from "lucide-react";
import type { ToastVariant } from "../../context/ToastContext.js";
import type { TokenInfo } from "../../hooks/types.js";
import { ICON_MD } from "../../utils/iconSize.js";
import { ConfirmDialog } from "../display/index.js";
import styles from "./SettingsPanel.module.scss";
 
/** Token type options for the add form. */
const TOKEN_TYPES: Array<{ value: string; label: string }> = [
  { value: "env_var", label: "Environment Variable" },
  { value: "file", label: "File" },
];
 
/** Props for the TokensPanel component. */
interface TokensPanelProps {
  /** List of stored token metadata. */
  tokens: TokenInfo[];
  /** Stores or updates a token. */
  onSetToken: (
    name: string,
    value: string,
    tokenType: string,
    envVar: string,
    filePath: string,
  ) => void;
  /** Deletes a token by name. */
  onDeleteToken: (name: string) => void;
  /** Display a toast notification. */
  onShowToast?: (message: string, variant?: ToastVariant) => void;
}
 
/** Token management panel with list, add form, and delete confirmation. */
export function TokensPanel({
  tokens,
  onSetToken,
  onDeleteToken,
  onShowToast,
}: TokensPanelProps): JSX.Element {
  const [name, setName] = useState("");
  const [value, setValue] = useState("");
  const [tokenType, setTokenType] = useState("env_var");
  const [target, setTarget] = useState("");
  const [confirmDeleteToken, setConfirmDeleteToken] = useState<string | null>(null);
 
  const handleSubmit = (e: FormEvent): void => {
    e.preventDefault();
    Iif (!name || !value) {
      return;
    }
    const envVar = tokenType === "env_var" ? target || name.toUpperCase() + "_TOKEN" : "";
    const filePath = tokenType === "file" ? target : "";
    onSetToken(name, value, tokenType, envVar, filePath);
    onShowToast?.("Token saved successfully", "success");
    setName("");
    setValue("");
    setTarget("");
  };
 
  const handleDelete = (tokenName: string): void => {
    setConfirmDeleteToken(tokenName);
  };
 
  const handleConfirmDelete = (): void => {
    if (confirmDeleteToken) {
      onDeleteToken(confirmDeleteToken);
      onShowToast?.("Token deleted", "info");
    }
    setConfirmDeleteToken(null);
  };
 
  return (
    <>
      <ConfirmDialog
        isOpen={confirmDeleteToken !== null}
        title="Delete Token?"
        description={
          confirmDeleteToken ? `"${confirmDeleteToken}" will be permanently removed.` : undefined
        }
        onConfirm={handleConfirmDelete}
        onCancel={() => setConfirmDeleteToken(null)}
      />
      <section className={styles.section}>
        <h3 className={styles.sectionTitle}>Tokens</h3>
        <p className={styles.sectionDescription}>
          API tokens are auto-pushed to environments when set or updated. Values are write-only.
        </p>
 
        {tokens.length === 0 ? (
          <div className={styles.emptyStateInfo}>
            Add your first API token to enable service integrations.
          </div>
        ) : (
          <div className={styles.tokenList}>
            {tokens.map((t) => (
              <div key={t.name} className={styles.tokenRow}>
                <span className={styles.tokenBadge}>{t.tokenType}</span>
                <span className={styles.tokenName}>{t.name}</span>
                <span className={styles.tokenTarget}>
                  {t.tokenType === "env_var" ? t.envVar : t.filePath}
                </span>
                <button
                  className={styles.deleteButton}
                  onClick={() => handleDelete(t.name)}
                  title={`Delete ${t.name}`}
                  aria-label={`Delete ${t.name}`}
                >
                  <X size={ICON_MD} aria-hidden="true" />
                </button>
              </div>
            ))}
          </div>
        )}
 
        <form className={styles.addForm} onSubmit={handleSubmit}>
          <div className={styles.formRow}>
            <input
              className={styles.input}
              type="text"
              placeholder="Token name"
              value={name}
              onChange={(e) => setName(e.target.value)}
            />
            <input
              className={styles.input}
              type="password"
              placeholder="Value"
              value={value}
              onChange={(e) => setValue(e.target.value)}
            />
          </div>
          <div className={styles.formRow}>
            <select
              className={styles.select}
              value={tokenType}
              onChange={(e) => setTokenType(e.target.value)}
            >
              {TOKEN_TYPES.map((tt) => (
                <option key={tt.value} value={tt.value}>
                  {tt.label}
                </option>
              ))}
            </select>
            <input
              className={styles.input}
              type="text"
              placeholder={
                tokenType === "env_var"
                  ? "Env var name (e.g. API_TOKEN)"
                  : "File path (e.g. /home/user/.token)"
              }
              value={target}
              onChange={(e) => setTarget(e.target.value)}
            />
            <button className={styles.addButton} type="submit">
              Add Token
            </button>
          </div>
        </form>
      </section>
    </>
  );
}