Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

23.2 KB
Raw Blame
import { Check, User, X } from "lucide-react";
import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { AccountTypeBadge } from "@/components/AccountTypeBadge";
import { getAvatarColor, getInitials } from "@/components/UserAvatar";
import { Badge } from "@/components/ui/badge";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import { principalLabel, usePrincipal } from "@/hooks/usePrincipal";
import { displayRoleTitleFromList } from "@/lib/role";
import { cn } from "@/lib/utils";
import { useAppStore } from "@/stores/app";
import { projectNamePrefix } from "@/stores/modules/v1/common";
import { ApprovalStatus } from "@/types/proto-es/v1/common_pb";
import type { Issue } from "@/types/proto-es/v1/issue_service_pb";
import { Issue_Approver_Status } from "@/types/proto-es/v1/issue_service_pb";
import {
  type ApprovalCandidate,
  useApprovalCandidates,
} from "../../../useApprovalCandidates";
import { usePlanDetailContext } from "../../shell/PlanDetailContext";
import {
  type ApprovalStepStatus,
  computeApprovalFlowLayout,
} from "./approvalFlowLayout";

export interface FlowStep {
  index: number;
  role: string;
  status: ApprovalStepStatus;
  approver?: string; // principal of the recorded decision
}

export function deriveSteps(issue: Issue): FlowStep[] {
  const roles = issue.approvalTemplate?.flow?.roles ?? [];
  return roles.map((role, index) => {
    const approver = issue.approvers[index];
    let status: ApprovalStepStatus = "pending";
    if (approver?.status === Issue_Approver_Status.APPROVED) {
      status = "approved";
    } else if (approver?.status === Issue_Approver_Status.REJECTED) {
      status = "rejected";
    } else {
      const blocked = roles.slice(0, index).some((_, i) => {
        const prev = issue.approvers[i];
        return prev?.status !== Issue_Approver_Status.APPROVED;
      });
      status = blocked ? "pending" : "current";
    }
    return { index, role, status, approver: approver?.principal };
  });
}

export function ReviewApprovalFlow({
  issue,
  compact = false,
}: {
  issue: Issue;
  // Fold to at most four nodes (approved chip + two waiting + pending chip),
  // rendered vertically. Used by the header Plan status popover, which is too
  // narrow for the full flow.
  compact?: boolean;
}) {
  const { t } = useTranslation();
  const hostRef = useRef<HTMLDivElement>(null);
  const [width, setWidth] = useState(0);

  useEffect(() => {
    const host = hostRef.current;
    if (!host) return;
    const observer = new ResizeObserver((entries) => {
      setWidth(entries[0]?.contentRect.width ?? 0);
    });
    observer.observe(host);
    return () => observer.disconnect();
  }, []);

  const steps = useMemo(() => deriveSteps(issue), [issue]);
  const layout = useMemo(
    () =>
      // width is 0 on first paint before the ResizeObserver fires; fall back
      // to a wide value so the initial layout is horizontal and corrects once
      // the real width arrives.
      computeApprovalFlowLayout(
        steps.map((s) => s.status),
        width || 9999
      ),
    [steps, width]
  );

  // A skipped approval — or a resolved template with no approver roles — has no
  // flow to render. Show the "no approval required" note here so every caller
  // (the review section and the bypass confirm sheet) stays consistent, instead
  // of each guarding the empty case on its own (BYT-9745).
  if (issue.approvalStatus === ApprovalStatus.SKIPPED || steps.length === 0) {
    return (
      <div className="px-4 py-3 text-sm text-control-placeholder">
        {t("custom-approval.approval-flow.skip")}
      </div>
    );
  }

  if (compact) {
    return (
      <div className="min-w-0 px-4 py-3">
        <CompactVerticalFlow issue={issue} steps={steps} />
      </div>
    );
  }

  return (
    <div ref={hostRef} className="min-w-0 px-4 py-3">
      {layout.kind === "vertical" ? (
        <VerticalFlow issue={issue} steps={steps} />
      ) : (
        <HorizontalFlow
          foldedApproved={layout.foldedApproved}
          issue={issue}
          namedPending={layout.namedPending}
          steps={steps}
        />
      )}
    </div>
  );
}

function HorizontalFlow({
  foldedApproved,
  issue,
  namedPending,
  steps,
}: {
  foldedApproved: number;
  issue: Issue;
  namedPending: number;
  steps: FlowStep[];
}) {
  const anchorIndex = steps.findIndex(
    (s) => s.status === "current" || s.status === "rejected"
  );
  const approved = steps.filter((s) => s.status === "approved");
  const foldedApprovedSteps = approved.slice(0, foldedApproved);
  const namedSteps = steps.filter(
    (s) =>
      !foldedApprovedSteps.includes(s) &&
      (anchorIndex === -1 || s.index <= anchorIndex + namedPending)
  );
  const foldedPendingSteps =
    anchorIndex === -1 ? [] : steps.slice(anchorIndex + 1 + namedPending);

  // Each item carries a stable key tied to its identity (chip name or step
  // index) so React reconciles correctly when the fold counts change with the
  // viewport — an array index key would graft a node onto the wrong slot.
  const items: { key: string; node: ReactNode }[] = [];
  if (foldedApprovedSteps.length > 0) {
    items.push({
      key: "approved-chip",
      node: <ApprovedChip steps={foldedApprovedSteps} />,
    });
  }
  for (const step of namedSteps) {
    items.push({
      key: `step-${step.index}`,
      node: <FlowNode issue={issue} step={step} />,
    });
  }
  if (foldedPendingSteps.length > 0) {
    items.push({
      key: "pending-chip",
      node: <PendingChip issue={issue} steps={foldedPendingSteps} />,
    });
  }

  return (
    <div className="flex min-w-0 items-start gap-x-2 overflow-hidden">
      {items.map((item, i) => (
        <div key={item.key} className="flex min-w-0 items-start gap-x-2">
          {i > 0 && (
            <div
              aria-hidden
              className="mt-3 h-px w-8 shrink-0 bg-control-border"
            />
          )}
          {item.node}
        </div>
      ))}
    </div>
  );
}

function VerticalFlow({ issue, steps }: { issue: Issue; steps: FlowStep[] }) {
  return (
    <div className="flex flex-col">
      {steps.map((step, i) => (
        <FlowNode
          key={step.index}
          isLast={i === steps.length - 1}
          issue={issue}
          step={step}
          vertical
        />
      ))}
    </div>
  );
}

function StatusDot({
  status,
  index,
}: {
  status: ApprovalStepStatus;
  index: number;
}) {
  return (
    <div
      className={cn(
        "flex size-6 shrink-0 items-center justify-center rounded-full text-xs font-medium",
        status === "approved" && "bg-success text-accent-text",
        status === "rejected" && "bg-error text-accent-text",
        status === "current" && "bg-accent text-accent-text",
        status === "pending" && "bg-control-bg text-control"
      )}
    >
      {status === "approved" && <Check className="size-3.5" />}
      {status === "rejected" && <X className="size-3.5" />}
      {status === "current" && <User className="size-3.5" />}
      {status === "pending" && index}
    </div>
  );
}

function FlowNode({
  issue,
  step,
  vertical = false,
  isLast = false,
}: {
  issue: Issue;
  step: FlowStep;
  vertical?: boolean;
  isLast?: boolean;
}) {
  const { t } = useTranslation();
  const page = usePlanDetailContext();
  const roleList = useAppStore((state) => state.roleList);
  const approver = usePrincipal(step.approver);
  const roleName = displayRoleTitleFromList(step.role, roleList);

  const subtitle =
    step.status === "approved"
      ? t("plan.review.approval-flow.approved-by", {
          user: principalLabel(approver),
        })
      : step.status === "rejected"
        ? t("plan.review.approval-flow.rejected-by", {
            user: principalLabel(approver),
          })
        : undefined;

  return (
    <div
      className={cn(
        "flex min-w-0 items-start gap-x-2",
        !vertical && "shrink-0"
      )}
    >
      {vertical ? (
        // Timeline rail: the status dot plus a connector line dropping to the
        // next node — the vertical counterpart of HorizontalFlow's between-node
        // connectors. The rail stretches to the row height (self-stretch) so the
        // flex-1 line reaches the next dot; spacing comes from the text's
        // padding-bottom rather than a container gap, so the line stays unbroken.
        <div className="flex flex-col items-center self-stretch">
          <StatusDot index={step.index + 1} status={step.status} />
          {!isLast && <div className="w-px flex-1 bg-control-border" />}
        </div>
      ) : (
        <StatusDot index={step.index + 1} status={step.status} />
      )}
      <div className={cn("min-w-0", vertical && !isLast && "pb-3")}>
        <div className="flex items-center gap-x-1.5">
          <span className="truncate text-sm font-medium text-main">
            {roleName}
          </span>
          {step.status === "current" && (
            <Badge className="px-1.5 py-0 text-xs" variant="secondary">
              {t("plan.review.approval-flow.current")}
            </Badge>
          )}
        </div>
        {subtitle ? (
          <div className="mt-1 flex h-4 min-w-0 items-center gap-x-1.5 text-xs leading-4 text-control-light">
            <span className="truncate">{subtitle}</span>
            <AccountTypeBadge compact email={approver?.email} />
          </div>
        ) : step.status === "current" || step.status === "pending" ? (
          <NodeReviewers
            issue={issue}
            projectId={page.projectId}
            role={step.role}
          />
        ) : null}
      </div>
    </div>
  );
}

// The reviewer line under a current/pending node: an avatar stack + "N
// reviewers" count, where the whole row is a hover target that reveals the full
// reviewer list (BYT-9711). Rendered for future stages too, not just current.
function NodeReviewers({
  issue,
  projectId,
  role,
}: {
  issue: Issue;
  projectId: string;
  role: string;
}) {
  const { t } = useTranslation();
  const page = usePlanDetailContext();
  // Whether the project IAM policy is in cache yet — used to tell "still
  // loading" (show nothing) apart from "genuinely no eligible reviewers".
  const policyLoaded = useAppStore(
    (state) =>
      state.projectPoliciesByName[`${projectNamePrefix}${projectId}`] !==
      undefined
  );
  const { candidates } = useApprovalCandidates(
    issue,
    projectId,
    role,
    page.plan?.lastPlanEditor ?? ""
  );
  if (candidates.length === 0) {
    // No principal of any type holds this step's role — say so rather than
    // render nothing (BYT-9711). Stay silent until the policy resolves so it
    // doesn't flash during load.
    if (!policyLoaded) return null;
    return (
      <div className="mt-1 flex h-4 items-center text-xs leading-4 text-control-placeholder">
        {t("plan.review.approval-flow.no-reviewers")}
      </div>
    );
  }
  const visible = candidates.slice(0, 3);
  const overflow = candidates.length - visible.length;
  const lastPlanEditorCandidate = candidates.find((candidate) =>
    candidate.ineligibilities.includes("last-plan-editor")
  );
  // A role can resolve to thousands of candidates; render a bounded, scrollable
  // slice in the popover and summarize the rest rather than mounting every row.
  const MAX_LISTED = 50;
  const listed = candidates.slice(0, MAX_LISTED);
  const remaining = candidates.length - listed.length;

  return (
    <div className="mt-1">
      <Popover>
        <PopoverTrigger
          delay={100}
          nativeButton={false}
          openOnHover
          render={
            <div className="flex h-5 w-fit cursor-default items-center gap-x-1.5">
              <div className="flex items-center">
                {visible.map(({ user }) => (
                  <InitialsAvatar
                    className="-ml-1 size-5 text-xs ring-2 ring-background first:ml-0"
                    key={user.name}
                    name={principalLabel(user)}
                  />
                ))}
                {overflow > 0 && (
                  <span className="-ml-1 flex size-5 items-center justify-center rounded-full bg-control-bg font-medium text-control text-xs ring-2 ring-background first:ml-0">
                    +{overflow}
                  </span>
                )}
              </div>
              <span className="text-xs text-control-light">
                {t("plan.review.approval-flow.n-reviewers", {
                  count: candidates.length,
                })}
              </span>
            </div>
          }
        />
        <PopoverContent align="start" className="w-72" side="bottom">
          <div className="flex max-h-64 flex-col gap-y-2 overflow-y-auto">
            {listed.map((candidate) => (
              <CandidateRow candidate={candidate} key={candidate.user.name} />
            ))}
          </div>
          {remaining > 0 && (
            <div className="mt-2 border-t pt-1.5 text-xs text-control-placeholder">
              {t("plan.review.approval-flow.and-n-more", { count: remaining })}
            </div>
          )}
        </PopoverContent>
      </Popover>
      {lastPlanEditorCandidate && (
        <div className="mt-1 truncate text-xs text-warning">
          {lastPlanEditorCandidate.user.title ||
            lastPlanEditorCandidate.user.email}
          {": "}
          {t("plan.review.candidate-last-changed-plan")}
        </div>
      )}
    </div>
  );
}

function CandidateRow({ candidate }: { candidate: ApprovalCandidate }) {
  const { t } = useTranslation();
  const { user } = candidate;
  return (
    <div className="flex items-center gap-x-2">
      <InitialsAvatar
        className="size-6 shrink-0 text-xs"
        name={principalLabel(user)}
      />
      <div className="min-w-0">
        <div className="flex items-center gap-x-1.5">
          <span className="truncate text-sm text-main">
            {principalLabel(user)}
          </span>
          <AccountTypeBadge compact email={user.email} />
        </div>
        <div className="truncate text-xs text-control-light">{user.email}</div>
        {candidate.ineligibilities.includes("last-plan-editor") && (
          <div className="text-xs text-warning">
            {t("plan.review.candidate-last-changed-plan")}
          </div>
        )}
      </div>
    </div>
  );
}

function ApprovedChip({ steps }: { steps: FlowStep[] }) {
  const { t } = useTranslation();
  return (
    <Popover>
      <PopoverTrigger
        delay={100}
        nativeButton={false}
        openOnHover
        render={
          <div className="flex shrink-0 cursor-default items-start gap-x-2">
            <div className="flex size-6 shrink-0 items-center justify-center rounded-full bg-success text-accent-text">
              <Check className="size-3.5" />
            </div>
            <div className="min-w-0">
              <span className="text-sm font-medium text-control">
                {t("plan.review.approval-flow.n-approved", { n: steps.length })}
              </span>
              <div className="mt-1 flex h-5 items-center">
                <ChipAvatars principals={steps.map((s) => s.approver ?? "")} />
              </div>
            </div>
          </div>
        }
      />
      <PopoverContent align="start" className="w-64" side="bottom">
        <div className="flex flex-col gap-y-2">
          {steps.map((step) => (
            <ApprovedPopoverRow key={step.index} step={step} />
          ))}
        </div>
      </PopoverContent>
    </Popover>
  );
}

function ApprovedPopoverRow({ step }: { step: FlowStep }) {
  const { t } = useTranslation();
  const roleList = useAppStore((state) => state.roleList);
  const approver = usePrincipal(step.approver);
  const roleName = displayRoleTitleFromList(step.role, roleList);
  return (
    <div className="flex items-start gap-x-2">
      <span className="flex size-5 shrink-0 items-center justify-center rounded-full bg-success text-accent-text">
        <Check className="size-3" />
      </span>
      <div className="min-w-0 flex-1">
        <div className="truncate text-sm font-medium text-main">{roleName}</div>
        <div className="mt-0.5 flex items-center gap-x-1 text-xs text-control-light">
          <InitialsAvatar
            className="size-5 shrink-0 text-xs"
            name={principalLabel(approver)}
          />
          <span className="truncate">
            {t("plan.review.approval-flow.approved-by", {
              user: principalLabel(approver),
            })}
          </span>
          <AccountTypeBadge compact email={approver?.email} />
        </div>
      </div>
    </div>
  );
}

// Small circular initials avatar. `className` carries the per-call size/ring
// variant; the common geometry + color derivation lives here.
function InitialsAvatar({
  className,
  name,
}: {
  className: string;
  name: string;
}) {
  return (
    <span
      className={cn(
        "flex items-center justify-center rounded-full font-medium text-accent-text",
        className
      )}
      style={{ backgroundColor: getAvatarColor(name) }}
    >
      {getInitials(name)}
    </span>
  );
}

function ChipAvatars({ principals }: { principals: string[] }) {
  return (
    <span className="flex items-center">
      {principals.slice(0, 3).map((principal, i) => (
        <PrincipalAvatar
          className="-ml-1 size-5 text-xs ring-1 ring-background first:ml-0"
          key={`${principal}-${i}`}
          principal={principal}
        />
      ))}
    </span>
  );
}

function PrincipalAvatar({
  className,
  principal,
}: {
  className: string;
  principal: string;
}) {
  return (
    <InitialsAvatar
      className={className}
      name={principalLabel(usePrincipal(principal))}
    />
  );
}

function PendingChip({ issue, steps }: { issue: Issue; steps: FlowStep[] }) {
  const { t } = useTranslation();
  return (
    <Popover>
      <PopoverTrigger
        delay={100}
        nativeButton={false}
        openOnHover
        render={
          <div className="flex shrink-0 cursor-default items-start gap-x-2">
            <div className="size-6 shrink-0 rounded-full border-2 border-dashed border-control-border" />
            <span className="text-sm font-medium text-control">
              {t("plan.review.approval-flow.n-pending", { n: steps.length })}
            </span>
          </div>
        }
      />
      <PopoverContent align="start" className="w-64" side="bottom">
        <div className="flex flex-col gap-y-2">
          {steps.map((step) => (
            <PendingPopoverRow issue={issue} key={step.index} step={step} />
          ))}
        </div>
      </PopoverContent>
    </Popover>
  );
}

// One row inside the folded-pending popover: step number, role, candidate
// avatars, and reviewer count — mirrors a live flow node on the light surface.
function PendingPopoverRow({ issue, step }: { issue: Issue; step: FlowStep }) {
  const { t } = useTranslation();
  const page = usePlanDetailContext();
  const roleList = useAppStore((state) => state.roleList);
  const { candidates } = useApprovalCandidates(
    issue,
    page.projectId,
    step.role,
    page.plan?.lastPlanEditor ?? ""
  );
  const roleName = displayRoleTitleFromList(step.role, roleList);
  const visible = candidates.slice(0, 4);
  const overflow = candidates.length - visible.length;

  return (
    <div className="flex items-start gap-x-2">
      <span className="flex size-5 shrink-0 items-center justify-center rounded-full bg-control-bg font-medium text-control text-xs">
        {step.index + 1}
      </span>
      <div className="min-w-0 flex-1">
        <div className="truncate text-sm font-medium text-main">{roleName}</div>
        {candidates.length > 0 && (
          <div className="mt-1 flex items-center gap-x-1.5">
            <div className="flex items-center">
              {visible.map(({ user }) => (
                <InitialsAvatar
                  className="-ml-1 size-5 text-xs ring-2 ring-background first:ml-0"
                  key={user.name}
                  name={principalLabel(user)}
                />
              ))}
              {overflow > 0 && (
                <span className="-ml-1 flex size-5 items-center justify-center rounded-full bg-control-bg text-control text-xs ring-2 ring-background first:ml-0">
                  +{overflow}
                </span>
              )}
            </div>
            <span className="text-xs text-control-light">
              {t("plan.review.approval-flow.n-reviewers", {
                count: candidates.length,
              })}
            </span>
          </div>
        )}
      </div>
    </div>
  );
}

// Compact fold used by the header Plan status popover: collapse all approved
// steps into one chip, name up to two upcoming (waiting) steps, and fold the
// rest into a "N pending" chip — at most four rows, stacked vertically.
function CompactVerticalFlow({
  issue,
  steps,
}: {
  issue: Issue;
  steps: FlowStep[];
}) {
  const approved = steps.filter((step) => step.status === "approved");
  const rest = steps.filter((step) => step.status !== "approved");
  const named = rest.slice(0, 2);
  const folded = rest.slice(2);
  const hasFolded = folded.length > 0;

  // Rows render in order: an approved fold, up to two named steps, then a pending
  // fold. `isLast` (which drops the trailing connector) is a self-contained
  // expression per row, so it doesn't depend on push order.
  const rows: ReactNode[] = [];
  if (approved.length > 0) {
    rows.push(
      <ApprovedFoldRow
        isLast={named.length === 0 && !hasFolded}
        key="approved-fold"
        steps={approved}
      />
    );
  }
  named.forEach((step, i) => {
    rows.push(
      <FlowNode
        isLast={i === named.length - 1 && !hasFolded}
        issue={issue}
        key={`step-${step.index}`}
        step={step}
        vertical
      />
    );
  });
  if (hasFolded) {
    rows.push(<PendingFoldRow isLast key="pending-fold" steps={folded} />);
  }

  return <div className="flex flex-col">{rows}</div>;
}

function ApprovedFoldRow({
  steps,
  isLast,
}: {
  steps: FlowStep[];
  isLast: boolean;
}) {
  const { t } = useTranslation();
  return (
    <div className="flex min-w-0 items-start gap-x-2">
      <div className="flex flex-col items-center self-stretch">
        <StatusDot index={0} status="approved" />
        {!isLast && <div className="w-px flex-1 bg-control-border" />}
      </div>
      <div className={cn("min-w-0", !isLast && "pb-3")}>
        <span className="text-sm font-medium text-control">
          {t("plan.review.approval-flow.n-approved", { n: steps.length })}
        </span>
        <div className="mt-1 flex h-4 items-center">
          <ChipAvatars principals={steps.map((step) => step.approver ?? "")} />
        </div>
      </div>
    </div>
  );
}

function PendingFoldRow({
  steps,
  isLast,
}: {
  steps: FlowStep[];
  isLast: boolean;
}) {
  const { t } = useTranslation();
  return (
    <div className="flex min-w-0 items-start gap-x-2">
      <div className="flex flex-col items-center self-stretch">
        <div className="size-6 shrink-0 rounded-full border-2 border-dashed border-control-border" />
        {!isLast && <div className="w-px flex-1 bg-control-border" />}
      </div>
      <div className={cn("min-w-0", !isLast && "pb-3")}>
        <span className="text-sm font-medium text-control">
          {t("plan.review.approval-flow.n-pending", { n: steps.length })}
        </span>
      </div>
    </div>
  );
}