All files / src/components/diff/ResourceHistorySection ResourceHistorySection.tsx

100% Statements 8/8
90.9% Branches 10/11
100% Functions 3/3
100% Lines 7/7

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                            104x                                                                                                       104x                       228x         99x     5x                   228x   227x                                
import ResourceHistoryPanel from "@/components/diff/ResourceHistoryPanel/ResourceHistoryPanel";
import { ResourceHistorySource } from "@/objects/History";
import { History } from "lucide-react";
import React from "react";
 
/**
 * Bookkeeping columns every ownable resource carries. They appear in the
 * snapshot because history captures whole rows, but none of them is an edit a
 * person made, so no resource wants them in its diff.
 *
 * `is_ai_generated` and `generation_id` are hidden for a different reason:
 * they are rendered as the provenance badge on the version row itself, so
 * showing them again as changed fields would be noise.
 */
export const DEFAULT_HIDDEN_HISTORY_FIELDS = [
  "owner_user_id",
  "owner_organisation_id",
  "owner_project_id",
  "created_at",
  "updated_at",
  "is_ai_generated",
  "generation_id",
  "generated_by_agent_id",
];
 
export interface ResourceHistorySectionProps {
  /** The router (or scope) serving this resource's history endpoints. */
  source: ResourceHistorySource;
  /** Resource id. Null/undefined renders the section in its empty state. */
  resourceId?: string | null;
  /** Stable prefix for the react-query key, e.g. QUERY_KEYS.GET.RULE_HISTORY. */
  queryKey: string;
  /** Human-readable names for this resource's fields. */
  fieldLabels?: Record<string, string>;
  /** Extra fields to hide, on top of {@link DEFAULT_HIDDEN_HISTORY_FIELDS}. */
  hiddenFields?: string[];
  /** Section heading; override where "Edit History" reads oddly. */
  title?: string;
  /**
   * Open on this version instead of the newest — set when the reader arrived
   * from a result stamped with the revision that produced it.
   */
  initialVersion?: number | null;
  /** Render without the surrounding card, for pages that supply their own. */
  bare?: boolean;
  className?: string;
}
 
/**
 * A resource's edit history, ready to drop into any viewer page.
 *
 * Every historied resource shows the same thing — a version timeline, a diff,
 * and the provenance of each edit — so call sites pass what differs (which
 * resource, which field labels) and nothing else. Wiring the two fetches, the
 * query keys and the hidden bookkeeping columns by hand at each site is what
 * this exists to stop.
 *
 * ```tsx
 * <ResourceHistorySection
 *   source={getApiHandler().rule_router}
 *   resourceId={ruleId}
 *   queryKey={QUERY_KEYS.GET.RULE_HISTORY}
 *   fieldLabels={{ name: "Name", conditions: "Conditions" }}
 * />
 * ```
 */
const ResourceHistorySection: React.FC<ResourceHistorySectionProps> = ({
  source,
  resourceId,
  queryKey,
  fieldLabels,
  hiddenFields,
  initialVersion,
  title = "Edit History",
  bare = false,
  className = "",
}) => {
  const panel = (
    <ResourceHistoryPanel
      historyQueryKey={[queryKey, resourceId]}
      // Guarded rather than disabled: a viewer renders before its id resolves
      // from the route, and an unguarded fetch would request `/undefined/history`.
      fetchHistory={() =>
        resourceId ? source.history(resourceId) : Promise.resolve(null)
      }
      fetchDiff={(toVersion, fromVersion) =>
        resourceId
          ? source.historyDiff(resourceId, toVersion, fromVersion)
          : Promise.resolve(null)
      }
      fieldLabels={fieldLabels}
      hiddenFields={[...DEFAULT_HIDDEN_HISTORY_FIELDS, ...(hiddenFields ?? [])]}
      initialVersion={initialVersion}
    />
  );
 
  if (bare) return <div className={className}>{panel}</div>;
 
  return (
    <div
      className={`rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm overflow-hidden ${className}`}
    >
      <div className="flex items-center gap-2 px-5 py-3.5 border-b border-gray-100 dark:border-gray-700">
        <History className="size-4 text-gray-500 dark:text-gray-400 shrink-0" />
        <h3 className="text-sm font-semibold text-gray-700 dark:text-gray-300">
          {title}
        </h3>
      </div>
      <div className="px-5 py-4">{panel}</div>
    </div>
  );
};
 
export default ResourceHistorySection;