All files / src/components/detail DetailPage.tsx

90.9% Statements 10/11
75% Branches 18/24
100% Functions 2/2
90.9% Lines 10/11

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                                                                                                                                                                                                                                                299x                       299x 299x                           299x 110x           189x                 189x 30x                 159x   159x                                                              
import AIProvenance from "@/components/badge/AIProvenance";
import GeneratedMark from "@/components/badge/GeneratedMark";
import { SkeletonLines } from "@/components/detail/Skeleton";
import ResourceHistorySection, {
  ResourceHistorySectionProps,
} from "@/components/diff/ResourceHistorySection/ResourceHistorySection";
import SubPage from "@/components/page/SubPage/SubPage";
import { WorkspaceGuard } from "@/components/owner/WorkspaceGuard";
import type { Resource } from "@/objects/Doc";
import React from "react";
 
/**
 * One resource's detail page, end to end: back link → title → actions →
 * provenance banner → body → edit history.
 *
 * `ResourceCollection` did this for list pages. Detail pages had the same
 * problem and no answer for it: fourteen `*Viewer` pages each re-derived the
 * loading branch, the not-found branch, where the AI-provenance banner sits
 * and whether the history section is remembered at all — and they disagreed.
 * Some returned a bare `<div className="p-4 text-red-500">` on error,
 * outside `SubPage` entirely, so the page lost its chrome. Some skipped the
 * history section, which the platform's audit claims are not optional about
 * (see CLAUDE.md, *Resource History & Generative Provenance*).
 *
 * The three states are the whole point: pass `loading`, `resource` and
 * `error` and the right branch renders, with the page's frame intact in all
 * three.
 *
 * ```tsx
 * <DetailPage
 *   resource={rule}
 *   loading={isLoading}
 *   title={rule?.name}
 *   icon={<ShieldAlert className="size-5" />}
 *   back={APP_ROUTES.USER.rules}
 *   label="rule"
 *   actions={[<DeleteButton key="del" … />]}
 *   history={{ source: api.rule_router, queryKey: QUERY_KEYS.GET.RULE_HISTORY,
 *              fieldLabels: { name: "Name" } }}
 * >
 *   {(rule) => <RuleBody rule={rule} />}
 * </DetailPage>
 * ```
 */
 
/**
 * The provenance columns a generated resource carries.
 *
 * Read off the resource rather than required of it: a handful of resources
 * genuinely cannot be generated (`SimulationScenario` is created under a
 * world, never by a flow), and constraining `T` to a type whose properties
 * are all optional makes TypeScript reject those outright as having "no
 * properties in common". The banner simply does not render for them.
 */
interface Provenanced {
  is_ai_generated?: boolean | null;
  generation_id?: string | null;
  generated_by_agent_id?: string | null;
}
 
export interface DetailPageProps<T> {
  /** The resource, or null/undefined once loading has finished and it was
   *  not found. */
  resource: T | null | undefined;
  loading?: boolean;
  /** A fetch failure, rendered in place of the body. */
  error?: unknown;
 
  title?: React.ReactNode;
  /** Shown instead of `title` while loading. Defaults to a skeleton. */
  loadingTitle?: React.ReactNode;
  icon?: React.ReactNode;
  description?: string;
  /** Route the back link points at. */
  back?: string;
  /** Header actions — buttons, toggles. Hidden while loading. */
  actions?: React.ReactNode[];
 
  /** Singular noun for this resource, used in the not-found copy and the
   *  provenance banner: "rule", "knowledge base". */
  label: string;
  /** The kind svc-core copies it as, when a project may make its own copy
   *  of an inherited one (`components/owner/WorkspaceGuard`). */
  copyKind?: string;
 
  /**
   * Edit history for this resource. Every user-creatable resource has one,
   * so this is deliberately not optional — pass `false` only for the
   * documented exceptions (`Model`, and system resources nobody can edit).
   */
  history: Omit<ResourceHistorySectionProps, "resourceId"> | false;
  /** The resource's id, for the history section. */
  resourceId?: string | null;
 
  /** The body. Given the loaded resource, so it never handles null. */
  children: (resource: T) => React.ReactNode;
  /** Rendered outside the loaded/not-found branch — a confirm dialog, a
   *  drawer — so it survives every state. */
  overlay?: React.ReactNode;
}
 
export function DetailPage<T>({
  resource,
  loading = false,
  error,
  title,
  loadingTitle,
  icon,
  description,
  back,
  actions,
  label,
  copyKind,
  history,
  resourceId,
  children,
  overlay,
}: DetailPageProps<T>) {
  // The header icon carries the same sparkle the list pages put on a card, so
  // a resource looks the same wherever it is drawn.
  const markedIcon = icon ? (
    <GeneratedMark
      generated={(resource as Provenanced | null)?.is_ai_generated}
      size="lg"
      label={`AI generated ${label}`}
    >
      {icon}
    </GeneratedMark>
  ) : (
    icon
  );
 
  const frame = (body: React.ReactNode, heading?: React.ReactNode) => (
    <>
      <SubPage
        title={heading ?? title ?? label}
        title_icon={markedIcon}
        description={description}
        back={back}
        links={loading ? undefined : actions}
      >
        {body}
      </SubPage>
      {overlay}
    </>
  );
 
  if (loading) {
    return frame(
      <SkeletonLines count={4} />,
      loadingTitle ?? <SkeletonLines count={1} className="h-7 w-48" wrapperClassName="" />,
    );
  }
 
  Iif (error) {
    return frame(
      <p className="text-sm text-red-600 dark:text-red-400">
        Could not load this {label}. {String((error as Error)?.message ?? "")}
      </p>,
      `Error loading ${label}`,
    );
  }
 
  if (!resource) {
    return frame(
      <p className="text-sm text-gray-500 dark:text-gray-400">
        This {label} could not be found. It may have been deleted, or the link
        may be incorrect.
      </p>,
      `${label[0].toUpperCase()}${label.slice(1)} not found`,
    );
  }
 
  const provenance = resource as Provenanced;
 
  return frame(
    <>
      <WorkspaceGuard
        resource={resource as unknown as Resource & { is_system?: boolean }}
        noun={label}
        copyKind={copyKind}
      />
      {provenance.is_ai_generated && (
        <AIProvenance
          resourceLabel={label}
          generationId={provenance.generation_id}
          generatedByAgentId={provenance.generated_by_agent_id}
          resourceId={resourceId}
          className="mb-5"
        />
      )}
 
      {children(resource)}
 
      {history !== false && (
        <ResourceHistorySection
          {...history}
          resourceId={resourceId}
          className={history.className ?? "mt-6"}
        />
      )}
    </>,
  );
}
 
export default DetailPage;