All files / src/apps/user/pages/knowledge RepositoryDetailsModal.tsx

92.3% Statements 12/13
100% Branches 14/14
75% Functions 3/4
92.3% Lines 12/13

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                                6x           6x     6x                             6x                                               17x 17x     17x 17x           17x 4x               17x                                                                 8x                                                                                            
import {
  Appearance,
  AppearanceFields,
  appearanceOf,
  AppearanceSwatch,
} from "@/components/appearance/AppearancePicker";
import { REPOSITORY_ICON_REGISTRY } from "@/components/appearance/icons";
import { Modal, ModalActions } from "@/components/detail";
import { FolderGit2 } from "lucide-react";
import React, { useState } from "react";
 
import { DEFAULT_CATEGORY, KnowledgeCategoryKey } from "./categories";
import CategoryPicker from "./CategoryPicker";
 
/** How a brand-new repository presents itself until the user picks
 *  otherwise — the registry's own default glyph on the neutral tone. */
const DEFAULT_APPEARANCE: Appearance = {
  icon: REPOSITORY_ICON_REGISTRY.fallback,
  accent: "slate",
};
 
const INPUT =
  "w-full rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 px-3 py-2 text-sm text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-400 dark:focus:ring-gray-500";
 
const LABEL =
  "block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1";
 
export interface NewRepository {
  name: string;
  description: string;
  icon: string;
  accent: string;
  /** What kind of knowledge it holds, which decides how agents use it. */
  category: KnowledgeCategoryKey;
}
 
/** A repository's details — its name, what kind of knowledge it holds, what
 *  it groups, and how it looks — for a new, empty one or to edit an existing
 *  one. Changing an existing one's kind is refused while one of its sources
 *  is in a repository of the kind it is leaving; `error` says why. */
const RepositoryDetailsModal: React.FC<{
  onClose: () => void;
  onSubmit: (values: NewRepository) => void;
  loading: boolean;
  /** An existing repository's details; a new one starts blank. */
  initial?: NewRepository;
  title?: string;
  confirmLabel?: string;
  loadingLabel?: string;
  /** Why the last submit was refused. */
  error?: string | null;
  /** The kind a new one starts as. */
  defaultCategory?: KnowledgeCategoryKey;
}> = ({
  onClose,
  onSubmit,
  loading,
  initial,
  title = "New repository",
  confirmLabel = "Create",
  loadingLabel = "Creating…",
  error,
  defaultCategory = DEFAULT_CATEGORY,
}) => {
  const [name, setName] = useState(initial?.name ?? "");
  const [category, setCategory] = useState<KnowledgeCategoryKey>(
    initial?.category ?? defaultCategory,
  );
  const [description, setDescription] = useState(initial?.description ?? "");
  const [appearance, setAppearance] = useState<Appearance>(
    initial
      ? appearanceOf(REPOSITORY_ICON_REGISTRY, initial.icon, initial.accent)
      : DEFAULT_APPEARANCE,
  );
 
  const submit = () =>
    onSubmit({
      name: name.trim(),
      description: description.trim(),
      icon: appearance.icon,
      accent: appearance.accent,
      category,
    });
 
  return (
    <Modal
      title={title}
      icon={FolderGit2}
      size="xl"
      busy={loading}
      onClose={onClose}
      footer={
        <ModalActions
          onCancel={onClose}
          onConfirm={submit}
          confirmLabel={confirmLabel}
          loadingLabel={loadingLabel}
          loading={loading}
          disabled={!name.trim()}
        />
      }
    >
      <div className="space-y-4">
        <div>
          <span className={LABEL}>What kind of knowledge?</span>
          <CategoryPicker value={category} onChange={setCategory} />
        </div>
        <div>
          <label htmlFor="repository-name" className={LABEL}>
            Name
          </label>
          <input
            id="repository-name"
            autoFocus
            className={INPUT}
            placeholder="e.g. Product Docs & Support KB"
            value={name}
            onChange={(e) => setName(e.target.value)}
          />
        </div>
        <div>
          <label htmlFor="repository-description" className={LABEL}>
            Description <span className="text-gray-400">(optional)</span>
          </label>
          <textarea
            id="repository-description"
            rows={3}
            className={`${INPUT} resize-none`}
            placeholder="What knowledge does this collection group?"
            value={description}
            onChange={(e) => setDescription(e.target.value)}
          />
        </div>
        <div>
          <span className={LABEL}>Appearance</span>
          <div className="flex items-start gap-3">
            <AppearanceSwatch
              registry={REPOSITORY_ICON_REGISTRY}
              icon={appearance.icon}
              accent={appearance.accent}
              size="lg"
            />
            <div className="flex-1 min-w-0">
              <AppearanceFields
                registry={REPOSITORY_ICON_REGISTRY}
                value={appearance}
                onChange={setAppearance}
                labels={false}
              />
            </div>
          </div>
        </div>
        {error && (
          <p role="alert" className="text-sm text-red-600 dark:text-red-400">
            {error}
          </p>
        )}
      </div>
    </Modal>
  );
};
 
export default RepositoryDetailsModal;