# All files / src/components/workspace WorkspaceFormFields.tsx

42.85% Statements 6/14
95.65% Branches 22/23
33.33% Functions 4/12
42.85% Lines 6/14

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

    9x

    3x

    6x

    6x

    12x

    12x

     /**
     * Shared workspace form fields used by both the create page and the inline-edit
     * detail page. Each field renders its own label + input/control.
     *
     * @module
     */

    import { type JSX } from "react";
    import type { Workspace, Environment, PersonaData } from "../../hooks/types.js";
    import styles from "./WorkspaceFormFields.module.scss";

    /** Fields managed by the workspace form. */
    export interface WorkspaceFormValues {
      name: string;
      description: string;
      repoUrl: string;
      environmentId: string;
      defaultPersonaId: string;
      useWorktrees: boolean;
      workingDirectory: string;
    }

    /** Build a blank set of defaults, optionally seeded from a workspace. */
    export function defaultFormValues(ws?: Workspace, environmentId?: string): WorkspaceFormValues {
      return {
        name: ws?.name ?? "",
        description: ws?.description ?? "",
        repoUrl: ws?.repoUrl ?? "",
        environmentId: ws?.linkedEnvironmentIds[0] ?? environmentId ?? "",
        defaultPersonaId: ws?.defaultPersonaId ?? "",
        useWorktrees: ws?.useWorktrees ?? true,
        workingDirectory: ws?.workingDirectory ?? "",
      };
    }

    /** Props for {@link WorkspaceFormFields}. */
    interface WorkspaceFormFieldsProps {
      values: WorkspaceFormValues;
      onChange: (values: WorkspaceFormValues) => void;
      environments: Environment[];
      personas: PersonaData[];
      /** Validation errors keyed by field name. */
      errors?: Partial<Record<keyof WorkspaceFormValues, string>>;
      /** Whether the form is in a submitting state. */
      disabled?: boolean;
      /** Whether the name field should receive autofocus on mount. */
      autoFocusName?: boolean;
    }

    const MAX_NAME_LENGTH: number = 100;

    /** Shared form fields for workspace create/edit. */
    export function WorkspaceFormFields({
      values,
      onChange,
      environments,
      personas,
      errors,
      disabled,
      autoFocusName,
    }: WorkspaceFormFieldsProps): JSX.Element {
      const set = <K extends keyof WorkspaceFormValues>(key: K, val: WorkspaceFormValues[K]): void => {
        onChange({ ...values, [key]: val });
      };

      return (
        <div className={styles.formContent}>
          {/* Name */}
          <div className={styles.section}>
            <label className={styles.label} htmlFor="ws-name">
              Name
            </label>
            <input
              id="ws-name"
              className={styles.titleInput}
              type="text"
              value={values.name}
              onChange={(e) => set("name", e.target.value)}
              placeholder="Workspace name"
              maxLength={MAX_NAME_LENGTH}
              autoFocus={autoFocusName}
              disabled={disabled}
              data-testid="workspace-form-name"
            />
            {errors?.name && (
              <span className={styles.fieldError} data-testid="workspace-form-error-name">
                {errors.name}
              </span>
            )}
          </div>

          {/* Description */}
          <div className={styles.section}>
            <label className={styles.label} htmlFor="ws-description">
              Description
            </label>
            <textarea
              id="ws-description"
              className={styles.descriptionTextarea}
              value={values.description}
              onChange={(e) => set("description", e.target.value)}
              placeholder="Optional description (Markdown supported)"
              disabled={disabled}
              data-testid="workspace-form-description"
            />
          </div>

          {/* Repository URL */}
          <div className={styles.section}>
            <label className={styles.label} htmlFor="ws-repo">
              Repository URL
            </label>
            <input
              id="ws-repo"
              className={styles.titleInput}
              type="text"
              value={values.repoUrl}
              onChange={(e) => set("repoUrl", e.target.value)}
              placeholder="https://github.com/org/repo"
              disabled={disabled}
              data-testid="workspace-form-repo"
            />
            {errors?.repoUrl && (
              <span className={styles.fieldError} data-testid="workspace-form-error-repoUrl">
                {errors.repoUrl}
              </span>
            )}
          </div>

          {/* Environment */}
          <div className={styles.section}>
            <label className={styles.label} htmlFor="ws-environment">
              Environment
            </label>
            <select
              id="ws-environment"
              className={styles.selectField}
              value={values.environmentId}
              onChange={(e) => set("environmentId", e.target.value)}
              disabled={disabled}
              data-testid="workspace-form-environment"
            >
              <option value="">Select environment…</option>
              {environments.map((env) => (
                <option key={env.id} value={env.id}>
                  {env.displayName || env.id}
                </option>
              ))}
            </select>
            {errors?.environmentId && (
              <span className={styles.fieldError} data-testid="workspace-form-error-environmentId">
                {errors.environmentId}
              </span>
            )}
          </div>

          {/* Default Persona */}
          <div className={styles.section}>
            <label className={styles.label} htmlFor="ws-persona">
              Default Persona
            </label>
            <select
              id="ws-persona"
              className={styles.selectField}
              value={values.defaultPersonaId}
              onChange={(e) => set("defaultPersonaId", e.target.value)}
              disabled={disabled}
              data-testid="workspace-form-persona"
            >
              <option value="">(Inherit)</option>
              {personas.map((p) => (
                <option key={p.id} value={p.id}>
                  {p.name}
                </option>
              ))}
            </select>
          </div>

          {/* Worktree isolation */}
          <div className={styles.section}>
            <label className={styles.checkboxRow}>
              <input
                type="checkbox"
                checked={values.useWorktrees}
                onChange={(e) => set("useWorktrees", e.target.checked)}
                disabled={disabled}
                data-testid="workspace-form-worktrees"
              />
              <span className={styles.checkboxLabel}>Enable worktree isolation</span>
            </label>
          </div>

          {/* Working directory */}
          <div className={styles.section}>
            <label className={styles.label} htmlFor="ws-workdir">
              Working Directory
            </label>
            <input
              id="ws-workdir"
              className={styles.titleInput}
              type="text"
              value={values.workingDirectory}
              onChange={(e) => set("workingDirectory", e.target.value)}
              placeholder="Default (server default)"
              disabled={disabled}
              data-testid="workspace-form-workdir"
            />
          </div>
        </div>
      );
    }