Skip to content

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

8.2 KB
Raw Blame
import { create } from "@bufbuild/protobuf";
import { Plus, X } from "lucide-react";
import { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { issueServiceClientConnect } from "@/api";
import { HumanizeTs } from "@/components/HumanizeTs";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import { cn } from "@/lib/utils";
import { extractUserEmail, pushNotification } from "@/stores";
import { getTimeForPbTimestampProtoEs } from "@/types";
import type { Color } from "@/types/proto-es/google/type/color_pb";
import { IssueStatus } from "@/types/proto-es/v1/common_pb";
import {
  IssueSchema,
  UpdateIssueRequestSchema,
} from "@/types/proto-es/v1/issue_service_pb";
import { colorToHex, hasProjectPermissionV2 } from "@/utils";
import { usePlanDetailContext } from "../shell/PlanDetailContext";

export function PlanDetailMeta() {
  const { t } = useTranslation();
  const page = usePlanDetailContext();
  const { patchState } = page;
  const project = page.project;
  const creatorEmail = useMemo(
    () => extractUserEmail(page.plan.creator),
    [page.plan.creator]
  );
  const createdTimeMs = getTimeForPbTimestampProtoEs(page.plan.createTime);
  const allowChangeLabels = useMemo(() => {
    if (!project || !page.issue || page.issue.status !== IssueStatus.OPEN) {
      return false;
    }
    return hasProjectPermissionV2(project, "bb.issues.update");
  }, [page.issue, project]);

  if (page.isCreating) {
    return (
      <div className="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-control-placeholder">
        <InlineLabels
          allowChange
          issueLabels={project?.issueLabels ?? []}
          labels={page.creationIssueLabels}
          onUpdate={page.setCreationIssueLabels}
          required={false}
        />
      </div>
    );
  }

  const handleLabelsUpdate = async (labels: string[]) => {
    if (!page.issue) return;
    const issuePatch = create(IssueSchema, page.issue);
    issuePatch.labels = labels;
    try {
      const response = await issueServiceClientConnect.updateIssue(
        create(UpdateIssueRequestSchema, {
          issue: issuePatch,
          updateMask: { paths: ["labels"] },
        })
      );
      patchState({ issue: response });
      pushNotification({
        module: "bytebase",
        style: "SUCCESS",
        title: t("common.updated"),
      });
    } catch (error) {
      pushNotification({
        module: "bytebase",
        style: "CRITICAL",
        title: t("common.failed"),
        description: String(error),
      });
    }
  };

  return (
    <div className="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-control-placeholder">
      <span>{t("plan.meta.created-by", { user: creatorEmail })}</span>
      {createdTimeMs !== undefined && (
        <>
          <span aria-hidden="true">·</span>
          <HumanizeTs tsMs={createdTimeMs} />
        </>
      )}

      {page.issue && (
        <>
          <span aria-hidden="true">·</span>
          <InlineLabels
            allowChange={allowChangeLabels}
            issueLabels={project?.issueLabels ?? []}
            labels={page.issue.labels || []}
            onUpdate={handleLabelsUpdate}
            required={page.issue.draft && (project?.forceIssueLabels ?? false)}
          />
        </>
      )}
    </div>
  );
}

function InlineLabels({
  allowChange,
  issueLabels,
  labels,
  onUpdate,
  required,
}: {
  allowChange: boolean;
  issueLabels: Array<{ color?: Color; value: string }>;
  labels: string[];
  onUpdate: (labels: string[]) => Promise<void> | void;
  /** The project forces issue labels, so submitting for review needs one. */
  required: boolean;
}) {
  const { t } = useTranslation();
  const [open, setOpen] = useState(false);
  const [isUpdating, setIsUpdating] = useState(false);

  const toggleLabel = async (value: string) => {
    const next = labels.includes(value)
      ? labels.filter((label) => label !== value)
      : [...labels, value];
    try {
      setIsUpdating(true);
      await onUpdate(next);
    } finally {
      setIsUpdating(false);
    }
  };

  const chips = labels.map((value) => {
    const option = issueLabels.find((item) => item.value === value);
    return (
      <span
        key={value}
        className="inline-flex items-center gap-1 rounded-xs bg-control-bg px-1.5 py-0.5 text-xs text-control"
      >
        <span
          className="size-2.5 shrink-0 rounded-sm"
          style={{
            backgroundColor: option?.color
              ? colorToHex(option.color)
              : undefined,
          }}
        />
        <span className="truncate">{value}</span>
        {allowChange && (
          <Button
            appearance="secondary"
            size="xs"
            aria-label={t("common.remove")}
            className={cn(
              "inline-flex shrink-0 items-center justify-center text-control-placeholder transition-colors",
              isUpdating
                ? "cursor-not-allowed opacity-60"
                : "hover:text-control"
            )}
            disabled={isUpdating}
            onClick={() => void toggleLabel(value)}
            type="button"
          >
            <X className="size-3" />
          </Button>
        )}
      </span>
    );
  });

  const triggerDisabled = !allowChange || isUpdating;

  return (
    <div className="flex flex-wrap items-center gap-1">
      {chips}
      <Popover open={open} onOpenChange={allowChange ? setOpen : undefined}>
        <PopoverTrigger
          render={
            <Button
              appearance="secondary"
              size="xs"
              className={cn(
                "inline-flex items-center gap-1 rounded-xs border border-dashed border-control-border px-1.5 py-0.5 text-xs text-control-placeholder transition-colors",
                allowChange &&
                  !isUpdating &&
                  "hover:border-control hover:text-control",
                triggerDisabled && "cursor-not-allowed opacity-60"
              )}
              disabled={triggerDisabled}
              type="button"
            />
          }
        >
          <Plus className="size-3" />
          <span>{t("issue.labels")}</span>
          {/* Only while unsatisfied: once a label is picked the trigger is an
              "add another" affordance, and the requirement is already met. */}
          {required && labels.length === 0 && (
            <span className="text-error">*</span>
          )}
        </PopoverTrigger>
        <PopoverContent
          side="bottom"
          align="start"
          initialFocus={false}
          finalFocus={false}
          className="w-56 overflow-hidden bg-background p-0"
        >
          <div className="max-h-60 overflow-y-auto">
            {issueLabels.length === 0 ? (
              <div className="px-3 py-6 text-sm text-control-placeholder">
                {t("common.no-data")}
              </div>
            ) : (
              issueLabels.map((option) => {
                const isSelected = labels.includes(option.value);
                return (
                  <Button
                    appearance="secondary"
                    size="md"
                    key={option.value}
                    className="flex w-full min-w-0 justify-start gap-x-2 px-3 py-2 text-left text-sm transition-colors hover:bg-control-bg"
                    disabled={isUpdating}
                    onClick={() => void toggleLabel(option.value)}
                    type="button"
                  >
                    <Checkbox checked={isSelected} />
                    <span
                      className="size-4 shrink-0 rounded-sm"
                      style={{
                        backgroundColor: option.color
                          ? colorToHex(option.color)
                          : undefined,
                      }}
                    />
                    <span
                      className="min-w-0 flex-1 truncate"
                      title={option.value}
                    >
                      {option.value}
                    </span>
                  </Button>
                );
              })
            )}
          </div>
        </PopoverContent>
      </Popover>
    </div>
  );
}