All files / src/components/owner OriginBadge.tsx

100% Statements 10/10
94.11% Branches 16/17
100% Functions 1/1
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                22x       164x 164x 164x 3x   164x 164x 164x 164x                                            
import React from "react";
import { ArrowDownToLine, ShieldAlert } from "lucide-react";
import { useWorkspace } from "@/context/WorkspaceContext";
import type { Resource } from "@/objects/Doc";
import { originIn } from "./origin";
 
/** Says when a row in a project's list is its parent's: inherited, or (a
 *  rule) required of every project. Nothing for the workspace's own work. */
export const OriginBadge: React.FC<{
  resource: Resource & { is_system?: boolean };
  className?: string;
}> = ({ resource, className = "" }) => {
  const ws = useWorkspace();
  const origin = originIn(resource, ws);
  if (origin !== "inherited" && origin !== "required") return null;
  const parent = ws.info?.parent;
  const from =
    parent?.kind === "personal" ? "your personal workspace" : (parent?.name ?? "the parent");
  const required = origin === "required";
  const Icon = required ? ShieldAlert : ArrowDownToLine;
  return (
    <span
      title={
        required
          ? `Required of every project by ${from}. It governs this project and can't be changed here.`
          : `Inherited from ${from}. Usable here, changed only there.`
      }
      className={`inline-flex items-center gap-1 max-w-[12rem] text-[10px] px-1.5 py-0.5 rounded-full border ${
        required
          ? "bg-amber-50 dark:bg-amber-950/40 text-amber-700 dark:text-amber-300 border-amber-200 dark:border-amber-800"
          : "bg-sky-50 dark:bg-sky-950/40 text-sky-700 dark:text-sky-300 border-sky-200 dark:border-sky-800"
      } ${className}`.trim()}
    >
      <Icon className="size-3 shrink-0" />
      <span className="truncate">
        {required ? "Required" : "Inherited"}
      </span>
    </span>
  );
};
 
export default OriginBadge;