All files / src/apps/user/pages/rules links.ts

100% Statements 20/20
100% Branches 11/11
100% Functions 4/4
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          107x             23x       12x 12x       23x                     277x 277x 277x 277x 277x           23x 23x               5x       5x   4x 5x    
import { encodeResourceId } from "@/components/detail";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
 
/** A rule's own page — the user's, or a built-in's read-only one. */
export function ruleHref(id: string): string {
  return APP_ROUTES.USER.GOVERNANCE.RULES.id.replace(
    ROUTE_PARAMS.RULE.id,
    encodeResourceId(id),
  );
}
 
/** The query parameter the builder reads a built-in rule's id from. */
export const FROM_BUILT_IN_PARAM = "from";
 
/** The builder, opened with a built-in rule's definition filled in. */
export function builtInRuleHref(id: string): string {
  const query = new URLSearchParams({ [FROM_BUILT_IN_PARAM]: id });
  return `${APP_ROUTES.USER.GOVERNANCE.RULES.new}?${query.toString()}`;
}
 
/** The query parameter the builder reads the layer to open on from. */
export const LAYER_PARAM = "layer";
 
/**
 * The builder, opened on one layer — a layer's "Add" on the Rules tab —
 * and, from a resource's page, pinned to it (`scope` and `target`), and
 * started from a built-in rule (`from`, read as `FROM_BUILT_IN_PARAM`).
 */
export function newRuleHref(
  layer: string,
  opts: { scope?: string; target?: string; from?: string } = {},
): string {
  const query = new URLSearchParams({ [LAYER_PARAM]: layer });
  if (opts.scope) query.set("scope", opts.scope);
  if (opts.target) query.set("target", opts.target);
  if (opts.from) query.set(FROM_BUILT_IN_PARAM, opts.from);
  return `${APP_ROUTES.USER.GOVERNANCE.RULES.new}?${query.toString()}`;
}
 
/** What the builder, editing a Tools rule, is asked to change before the
 *  person reviews and saves it (`domainEdit.ts`): a source to add to a
 *  rule that allows only some, or one to stop ruling out. */
export const ADD_TRUSTED_PARAM = "add_trusted";
export const REMOVE_BLOCKED_PARAM = "remove_blocked";
 
/** A rule's edit page — with a change for the person to review, when the
 *  note under an answer offers one. */
export function editRuleHref(
  id: string,
  change?: { kind: "add_trusted" | "remove_blocked"; domain: string },
): string {
  const path = APP_ROUTES.USER.GOVERNANCE.RULES.edit.replace(
    ROUTE_PARAMS.RULE.id,
    encodeResourceId(id),
  );
  if (!change) return path;
  const param =
    change.kind === "add_trusted" ? ADD_TRUSTED_PARAM : REMOVE_BLOCKED_PARAM;
  return `${path}?${new URLSearchParams({ [param]: change.domain }).toString()}`;
}