All files / src/components/governance RuleEventRow.tsx

100% Statements 9/9
68.75% Branches 11/16
100% Functions 3/3
100% Lines 9/9

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                                                        3x   124x 124x     124x       124x 124x 81x     124x                                                                                             81x                                                                    
import {
  LAYER_CLASSES,
  layerOf,
  ruleLayerMeta,
} from "@/apps/user/pages/rules/ruleLayer";
import {
  encodeResourceId,
  GOVERNANCE_OUTCOME,
  StatusBadge,
} from "@/components/detail";
import { usePiiKinds } from "@/hooks/usePiiKinds";
import {
  describeAction,
  GovernanceEvent,
  OUTCOME_HINTS,
  OUTCOME_LABELS,
} from "@/objects/Governance";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import { ArrowDownToLine, ArrowUpFromLine } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
 
/**
 * One rule, evaluated once against one turn: which layer, which revision,
 * what came of it, and what it did in words. Governance → Rules → Activity
 * draws a turn's rules with it, and so does the decision record's Rules section
 * — one rule reads the same wherever it is shown.
 */
export const RuleEventRow: React.FC<{ event: GovernanceEvent }> = ({ event }) => {
  // A row from before layers names a type; it reads as the layer it became.
  const meta = ruleLayerMeta(layerOf(event));
  const Icon = meta.Icon;
  // Input and output are the two things a reader is scanning for — whether a
  // rule saw what was asked or what came back.
  const PhaseIcon = event.phase === "input" ? ArrowUpFromLine : ArrowDownToLine;
 
  // What actually happened, in words. A rule that stood aside says so through
  // its outcome and has nothing further to report.
  const { byKey } = usePiiKinds();
  const lines = event.actions.map((action) =>
    describeAction(action, event.detail?.[action], byKey),
  );
 
  return (
    <div className="flex items-start gap-3 px-3 py-2.5 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800">
      <div className={`p-1.5 rounded-lg shrink-0 ${LAYER_CLASSES.iconTile}`}>
        <Icon className="size-4" />
      </div>
 
      <div className="flex-1 min-w-0">
        <div className="flex items-center gap-2 flex-wrap">
          {/* The rule is what a reader wants next — but only linked while it
              still exists. An event outlives the rule it records. */}
          {event.rule_id ? (
            <Link
              to={APP_ROUTES.USER.GOVERNANCE.RULES.id.replace(
                ROUTE_PARAMS.RULE.id,
                encodeResourceId(event.rule_id),
              )}
              className="text-sm font-semibold text-gray-900 dark:text-gray-100 truncate hover:underline"
            >
              {event.rule_name ?? "Untitled rule"}
            </Link>
          ) : (
            <span className="text-sm font-semibold text-gray-500 dark:text-gray-400 truncate italic">
              {event.rule_name ?? "Deleted rule"}
            </span>
          )}
          {/* The revision that ran, not the rule's current one. */}
          {event.rule_version != null && (
            <span className="text-[10px] font-mono text-gray-400 dark:text-gray-500">
              v{event.rule_version}
            </span>
          )}
          <span
            className={`text-[11px] font-medium px-2 py-0.5 rounded-full border ${LAYER_CLASSES.tag}`}
          >
            {meta.label}
          </span>
          <StatusBadge
            status={event.outcome}
            scale={GOVERNANCE_OUTCOME}
            label={OUTCOME_LABELS[event.outcome]}
            size="xs"
          />
        </div>
 
        {lines.length > 0 ? (
          <ul className="mt-1 space-y-0.5">
            {lines.map((line, i) => (
              <li
                key={i}
                className="text-xs text-gray-600 dark:text-gray-300 truncate"
              >
                {line}
              </li>
            ))}
          </ul>
        ) : (
          <p className="mt-1 text-xs text-gray-400 dark:text-gray-500">
            {OUTCOME_HINTS[event.outcome]}
          </p>
        )}
 
        {/* Only what is this rule's own. When the turn ran, what it ran
            against and by whom is the card's header, said once. */}
        <div className="mt-1.5 flex items-center gap-2 flex-wrap text-[11px] text-gray-400 dark:text-gray-500">
          <span className="inline-flex items-center gap-1">
            <PhaseIcon className="size-3" />
            {event.phase === "input" ? "On the prompt" : "On the response"}
          </span>
          {event.enforced_in === "ai" && (
            <>
              <span>·</span>
              <span>enforced during execution</span>
            </>
          )}
        </div>
      </div>
    </div>
  );
};
 
export default RuleEventRow;