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

100% Statements 16/16
94.11% Branches 16/17
100% Functions 2/2
100% Lines 16/16

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                                              2x 20x 20x 20x     20x   11x         20x   20x 10x             10x 4x                       6x 20x 20x         3x                         3x        
import { SkeletonLines, encodeResourceId, useResourceId } from "@/components/detail";
import { originIn, readOnlyOrigin } from "@/components/owner";
import SubPage from "@/components/page/SubPage/SubPage";
import { useWorkspace } from "@/context/WorkspaceContext";
import { isSystemManaged } from "@/objects/Doc";
import { Rule } from "@/objects/Rule";
import { APP_ROUTES } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import React from "react";
import { Navigate } from "react-router-dom";
 
import { RuleBuilder } from "./NewRule";
 
/**
 * Edit a rule in the builder that made it (`RuleBuilder`). Only a rule this
 * workspace owns and the caller may change (`can_change`, svc-core's own
 * check): a built-in, an inherited or a required one is read-only here, one
 * from another workspace is changed in its own, and a governance writer
 * changes only what they created — each goes back to the rule's page, which
 * says why and offers what can be done.
 */
const EditRule: React.FC = () => {
  const ruleId = useResourceId("ruleId");
  const { getApiHandler } = useApiHandler();
  const workspace = useWorkspace();
 
  // The rule page's key, so opening Edit from it reads the cached rule.
  const { data, isLoading } = useQuery<Rule | null>({
    queryKey: [`${QUERY_KEYS.GET.RULE_BY_ID}:${ruleId}`],
    queryFn: () => getApiHandler().rule_router.get(ruleId!),
    enabled: !!ruleId,
  });
  // Bound to an explicit type: the pinned TypeScript cannot infer react-query
  // v5's return type. See docs/guides/frontend-components.md.
  const rule: Rule | null = data ?? null;
 
  if (isLoading) {
    return (
      <SubPage title="Edit Rule" back={APP_ROUTES.USER.rules}>
        <SkeletonLines count={4} wrapperClassName="space-y-4 max-w-2xl" />
      </SubPage>
    );
  }
 
  if (!rule) {
    return (
      <SubPage title="Edit Rule" back={APP_ROUTES.USER.rules}>
        <p className="text-sm text-gray-500 dark:text-gray-400">
          This rule could not be found. It may have been deleted, or it belongs
          to a workspace you cannot see.
        </p>
      </SubPage>
    );
  }
 
  // A project's parent is only known once the workspace resolves; until
  // then a parent's rule would read as somebody else's.
  const settled = workspace.kind === "personal" || !!workspace.info;
  const origin = settled ? originIn(rule, workspace) : null;
  if (
    isSystemManaged(rule) ||
    rule.can_change === false ||
    (origin && (readOnlyOrigin(origin) || origin === "elsewhere"))
  ) {
    return (
      <Navigate
        replace
        to={APP_ROUTES.USER.GOVERNANCE.RULES.id.replace(
          ":ruleId",
          encodeResourceId(rule.id),
        )}
      />
    );
  }
 
  // Keyed by the rule, so the form starts from it and never from the last
  // one edited.
  return <RuleBuilder key={rule.id} editing={rule} />;
};
 
export default EditRule;