All files / src/apps/user/pages/rules PiiKindsInput.tsx

90.47% Statements 19/21
86.66% Branches 13/15
88.88% Functions 8/9
95% Lines 19/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                                    5x           11x 11x 11x 11x   11x 3x 3x 3x     3x   9x 9x         11x     4x                     7x             1x             14x           21x                   3x                                      
import { usePiiKinds } from "@/hooks/usePiiKinds";
import { parseListValue, sentenceCase } from "@/objects/Rule";
import React from "react";
 
/**
 * Which kinds of personal data a PII rule acts on — the "What to redact"
 * parameter of `filter_pii` / `redact_pii`, and the value of a "PII types
 * found" condition.
 *
 * The kinds are the detectors' own registry (`usePiiKinds`), grouped by where
 * each identifier is issued, each with an example of what it catches. The
 * value is the comma-separated text the builder stores every list in.
 *
 * `allowDefault` is the parameter's case: left empty, a rule redacts the
 * default set, so "Use the default set" is a choice of its own — the
 * checkboxes then show what that set is rather than accepting edits, and
 * unticking it starts from the same selection.
 */
const PiiKindsInput: React.FC<{
  value: string;
  onChange: (value: string) => void;
  allowDefault?: boolean;
  label: string;
}> = ({ value, onChange, allowDefault = false, label }) => {
  const { byRegion, defaults, kinds } = usePiiKinds();
  const chosen = parseListValue(value);
  const usingDefault = allowDefault && chosen.length === 0;
  const shown = new Set(usingDefault ? defaults : chosen);
 
  const toggle = (key: string) => {
    const next = new Set(shown);
    Iif (next.has(key)) next.delete(key);
    else next.add(key);
    // Kept in the registry's order, so a stored rule reads the same way the
    // picker lists it.
    onChange(
      kinds
        .map((kind) => kind.key)
        .filter((k) => next.has(k))
        .join(","),
    );
  };
 
  if (kinds.length === 0) {
    // Before the catalogue lands (or if it never does), the value is still
    // editable as the text the builder stores.
    return (
      <input
        aria-label={label}
        placeholder="comma, separated, kinds"
        value={value}
        onChange={(e) => onChange(e.target.value)}
        className="px-2.5 py-1.5 text-sm rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200 w-full"
      />
    );
  }
 
  return (
    <fieldset className="space-y-2" aria-label={label}>
      {allowDefault && (
        <label className="flex items-center gap-2 text-xs text-gray-700 dark:text-gray-200">
          <input
            type="checkbox"
            checked={usingDefault}
            onChange={() => onChange(usingDefault ? defaults.join(",") : "")}
          />
          Use the default set ({defaults.length} kinds)
        </label>
      )}
      <div className="max-h-72 space-y-3 overflow-auto rounded-lg border border-gray-200 dark:border-gray-700 p-2.5">
        {byRegion.map((group) => (
          <div key={group.region} className="space-y-1">
            <p className="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
              {group.label}
            </p>
            <div className="grid grid-cols-1 gap-x-4 gap-y-1 sm:grid-cols-2">
              {group.kinds.map((kind) => (
                <label
                  key={kind.key}
                  title={kind.note || undefined}
                  className="flex items-start gap-2 text-xs text-gray-700 dark:text-gray-200"
                >
                  <input
                    type="checkbox"
                    className="mt-0.5 shrink-0"
                    checked={shown.has(kind.key)}
                    disabled={usingDefault}
                    onChange={() => toggle(kind.key)}
                  />
                  <span className="min-w-0">
                    <span className="block">{sentenceCase(kind.label)}</span>
                    <span className="block font-mono text-[11px] text-gray-400 dark:text-gray-500 break-all">
                      {kind.example}
                    </span>
                  </span>
                </label>
              ))}
            </div>
          </div>
        ))}
      </div>
    </fieldset>
  );
};
 
export default PiiKindsInput;