Skip to content

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

20.5 KB
Raw Blame
import { FileText, Loader2, MessagesSquare, Pencil } from "lucide-react";
import {
  type ReactNode,
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import { useTranslation } from "react-i18next";
import { HumanizeTs } from "@/components/HumanizeTs";
import {
  ActivityRowFrame,
  ActivityRowShell,
  CommentCreator,
  CommentIconBadge,
  canEditIssueComment,
  IssueCommentRow,
  type PlanChangeReferenceRenderer,
  ReviewSubmissionIcon,
  ReviewSubmissionSentence,
} from "@/components/issue-activity/IssueCommentActivity";
import { MarkdownEditor } from "@/components/MarkdownEditor";
import { Button } from "@/components/ui/button";
import { useProjectByName } from "@/hooks/useProjectByName";
import {
  collectPlanUpdateSpecs,
  diffPlanSpecsForEvent,
} from "@/lib/plan/diffPlanSpecs";
import { pushNotification } from "@/stores";
import { useAppStore } from "@/stores/app";
import {
  getIssueCommentType,
  IssueCommentType,
  isThreadRoot,
} from "@/stores/app/issueComment";
import { projectNamePrefix } from "@/stores/modules/v1/common";
import { getTimeForPbTimestampProtoEs } from "@/types";
import type { Issue, IssueComment } from "@/types/proto-es/v1/issue_service_pb";
import { IssueComment_ThreadState } from "@/types/proto-es/v1/issue_service_pb";
import type { Plan } from "@/types/proto-es/v1/plan_service_pb";
import { sqlReviewV2Enabled } from "@/utils/featureGates";
import { hasProjectPermissionV2 } from "@/utils/iam/permission";
import { useFocusThread } from "../../hooks/useFocusThread";
import { usePlanChangeReferenceData } from "../../hooks/usePlanChangeReferenceData";
import { placementOf } from "../../shared/stores/placementSlice";
import { usePlanDetailStore } from "../../shared/stores/usePlanDetailStore";
import { usePlanDetailContext } from "../../shell/PlanDetailContext";
import { PlanSpecChangeReference } from "../PlanChangeReference";
import { CommentThreadCard } from "../threads/CommentThreadCard";
import { ReviewerIcon, reviewerName } from "../threads/ReviewResult";
import { StatementAnchorContext } from "../threads/StatementAnchorContext";
import {
  type CommentThread,
  groupThreads,
  isShownInEditor,
  needsActivityCard,
  targetSha256OfSpec,
} from "../threads/threadModel";
import { usePreviewEndLines } from "../threads/useUnresolvedThreadCounts";
import { consolidateConsecutive } from "./consolidateTimeline";
import { foldTimeline } from "./foldTimeline";
import { ReviewCommentComposer } from "./ReviewCommentComposer";
import {
  buildTimelineEntries,
  type TimelineEntry,
  type TimelineSource,
} from "./timelineEvents";

export function ReviewActivityTimeline({
  comments,
  issue,
  plan,
}: {
  comments: IssueComment[];
  issue: Issue;
  plan: Plan;
}) {
  const { t } = useTranslation();
  const page = usePlanDetailContext();
  const project = useProjectByName(`${projectNamePrefix}${page.projectId}`);
  const [expanded, setExpanded] = useState(false);
  const [runToggles, setRunToggles] = useState<ReadonlyMap<string, boolean>>(
    new Map()
  );
  const toggleRun = useCallback((runId: string, open: boolean) => {
    setRunToggles((prev) => new Map(prev).set(runId, open));
  }, []);
  const planUpdateSpecs = useMemo(
    () => collectPlanUpdateSpecs(comments),
    [comments]
  );
  const threads = useMemo(() => groupThreads(comments), [comments]);
  // Anchored threads reference the plan's current specs; hydrate them once
  // here rather than once per card.
  const referenceSpecs = useMemo(
    () => [...planUpdateSpecs, ...plan.specs],
    [planUpdateSpecs, plan.specs]
  );
  const changeReferenceResources = usePlanChangeReferenceData(referenceSpecs);
  const renderPlanChangeReference = useCallback<PlanChangeReferenceRenderer>(
    ({ siblings, spec }) => (
      <PlanSpecChangeReference
        className="font-medium"
        resources={changeReferenceResources}
        siblings={siblings}
        spec={spec}
      />
    ),
    [changeReferenceResources]
  );

  useEffect(() => {
    setExpanded(false);
    setRunToggles(new Map());
  }, [page.pageKey]);

  // A card asked for from elsewhere may sit in the folded middle, so the
  // timeline unfolds first and scrolls once the card renders.
  const activityFocus = usePlanDetailStore((s) => s.activityFocus);
  const clearActivityFocus = usePlanDetailStore((s) => s.clearActivityFocus);
  useEffect(() => {
    if (activityFocus) setExpanded(true);
  }, [activityFocus]);

  // Under SQL Review V2, Activity records each run as one line.
  const placements = usePlanDetailStore((s) => s.placements);
  const placementTargets = usePlanDetailStore((s) => s.placementTargets);
  const previewEndLines = usePreviewEndLines(
    plan.specs,
    `${projectNamePrefix}${page.projectId}`
  );
  const keepFindingCard = useMemo(() => {
    if (!sqlReviewV2Enabled()) return undefined;
    return (root: IssueComment) => {
      const thread = threads.find((candidate) => candidate.root === root);
      if (!thread) return true;
      return needsActivityCard(
        thread,
        isShownInEditor(
          root,
          plan.specs,
          (commentName, specId, sheetSha256) =>
            placementOf(
              { placements, placementTargets },
              commentName,
              specId,
              sheetSha256
            ),
          (specId) => previewEndLines.get(specId)
        )
      );
    };
  }, [placementTargets, placements, plan.specs, previewEndLines, threads]);

  const entries = useMemo(
    () =>
      buildTimelineEntries({
        planCreator: plan.creator,
        planCreateTime: plan.createTime,
        issueCreator: issue.creator,
        issueCreateTime: issue.createTime,
        issueDraft: issue.draft,
        comments,
        keepFindingCard,
        runToggles,
      }),
    [
      comments,
      issue.createTime,
      issue.creator,
      issue.draft,
      keepFindingCard,
      plan.createTime,
      plan.creator,
      runToggles,
    ]
  );
  const items = useMemo(() => {
    const renderable = entries.filter(isRenderableEntry);
    const consolidated = consolidateConsecutive(renderable);
    return foldTimeline(consolidated, expanded);
  }, [entries, expanded]);
  useEffect(() => {
    if (!activityFocus || !expanded) return;
    document
      .getElementById(activityFocus.commentName)
      ?.scrollIntoView({ behavior: "smooth", block: "center" });
    clearActivityFocus(activityFocus.nonce);
  }, [activityFocus, clearActivityFocus, expanded, items]);
  // Permission-gated only — comments are allowed regardless of issue status
  // (the backend and the issue-detail list allow commenting on done issues).
  const allowComment = Boolean(
    project && hasProjectPermissionV2(project, "bb.issueComments.create")
  );

  return (
    <div className="flex flex-col gap-y-2 px-4 pb-3 pt-4">
      <h4 className="text-sm font-medium text-main">
        {t("plan.review.activity.self")}
      </h4>
      <ul className="flex flex-col">
        {items.map((item, i) => {
          if (item.type === "fold") {
            return (
              <TornSeparator
                count={item.count}
                key={`fold-${i}`}
                onShowAll={() => setExpanded(true)}
              />
            );
          }
          const isLast = !items
            .slice(i + 1)
            .some((next) => next.type !== "fold");
          return (
            <ActivityRow
              entry={item.entry}
              issue={issue}
              isLast={isLast}
              key={item.entry.id}
              onToggleRun={toggleRun}
              plan={plan}
              renderPlanChangeReference={renderPlanChangeReference}
              threads={threads}
            />
          );
        })}
      </ul>
      {allowComment && <ReviewCommentComposer issueName={issue.name} />}
    </div>
  );
}

// A plan update whose specs are proto-equal produces no sentence; drop it
// entirely so the timeline doesn't show a verbless row (and so the connector
// line's "last row" math stays correct).
function isRenderableEntry(entry: TimelineEntry): boolean {
  if (entry.source.type !== "comment") return true;
  const comment = entry.source.comment;
  if (
    getIssueCommentType(comment) === IssueCommentType.PLAN_UPDATE &&
    comment.event.case === "planUpdate"
  ) {
    return diffPlanSpecsForEvent(comment.event.value).length > 0;
  }
  return true;
}

// Torn-paper zigzag edge (a triangle-wave stroke) tiled horizontally on each
// side of the fold marker. Encoded as an inline SVG data URI because no Tailwind
// utility produces a sawtooth line.
const TORN_EDGE_STYLE = {
  backgroundImage:
    "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='8'%3E%3Cpath d='M0 6 L12 2 L24 6' fill='none' stroke='%23cfd8e6' stroke-width='1'/%3E%3C/svg%3E\")",
  backgroundRepeat: "repeat-x",
  backgroundPosition: "center",
} as const;

function TornSeparator({
  count,
  onShowAll,
}: {
  count: number;
  onShowAll: () => void;
}) {
  const { t } = useTranslation();
  return (
    <div className="flex items-center gap-x-2 py-2">
      <div aria-hidden="true" className="h-2 flex-1" style={TORN_EDGE_STYLE} />
      <Button
        className="group shrink-0 bg-background px-2 text-control-light hover:text-control"
        onClick={onShowAll}
        size="xs"
        appearance="secondary"
      >
        {t("plan.review.activity.n-hidden-events", { count })}
        <span className="mx-1 text-control-placeholder">·</span>
        <span className="text-accent group-hover:text-accent-hover">
          {t("plan.review.activity.show-all")}
        </span>
      </Button>
      <div aria-hidden="true" className="h-2 flex-1" style={TORN_EDGE_STYLE} />
    </div>
  );
}

function ActivityRow({
  entry,
  issue,
  isLast,
  onToggleRun,
  plan,
  renderPlanChangeReference,
  threads,
}: {
  entry: TimelineEntry;
  issue: Issue;
  isLast: boolean;
  onToggleRun: (runId: string, open: boolean) => void;
  plan: Plan;
  renderPlanChangeReference: PlanChangeReferenceRenderer;
  threads: CommentThread[];
}) {
  const source = entry.source;
  if (source.type === "review-run") {
    return (
      <ReviewRunActivityRow
        isLast={isLast}
        onToggle={() => onToggleRun(entry.id, !source.open)}
        source={source}
      />
    );
  }
  if (source.type === "comment" && isThreadRoot(source.comment)) {
    const thread = threads.find(
      (candidate) => candidate.root.name === source.comment.name
    );
    if (thread) {
      return (
        <ThreadActivityRow
          isLast={isLast}
          issue={issue}
          plan={plan}
          renderPlanChangeReference={renderPlanChangeReference}
          thread={thread}
        />
      );
    }
  }
  if (source.type === "comment") {
    return (
      <ReviewCommentRow
        comment={source.comment}
        isLast={isLast}
        issue={issue}
        plan={plan}
        renderPlanChangeReference={renderPlanChangeReference}
        similarCount={entry.similarCount}
      />
    );
  }
  // Synthetic, review-only events (plan created / marked ready) have no backing
  // issue comment, so render the shared row shell with a local icon + header.
  return (
    <ActivityRowShell
      header={<SyntheticHeader source={source} />}
      icon={<SyntheticIcon type={source.type} />}
      isLast={isLast}
    />
  );
}

// One line per completed run: what it found. "Show" opens the cards of the
// findings it posted below it.
function ReviewRunActivityRow({
  isLast,
  onToggle,
  source,
}: {
  isLast: boolean;
  onToggle: () => void;
  source: Extract<TimelineSource, { type: "review-run" }>;
}) {
  const { t } = useTranslation();
  const { runType, postedCount, keptCount, supersededCount } =
    source.completion;
  // A run with nothing earlier to compare reads as a first review, with how
  // many of its findings are still open. A later one says only what changed:
  // an open count beside "still found" would read as covering those too.
  const firstReview = keptCount === 0 && supersededCount === 0;
  const open = firstReview
    ? source.findings.filter(
        (finding) => finding.threadState === IssueComment_ThreadState.OPEN
      ).length
    : 0;
  const summary = firstReview
    ? [
        postedCount === 0
          ? t("plan.review.activity.no-findings")
          : t("plan.review.activity.n-findings", { count: postedCount }),
      ]
    : [
        postedCount > 0 &&
          t("plan.review.activity.n-new", { count: postedCount }),
        keptCount > 0 &&
          t("plan.review.activity.n-still-found", { count: keptCount }),
        supersededCount > 0 &&
          t("plan.review.activity.n-no-longer-found", {
            count: supersededCount,
          }),
      ].filter((part) => part !== false);
  if (open > 0)
    summary.push(t("plan.automated-review.n-open", { count: open }));
  return (
    <ActivityRowShell
      header={
        <>
          <span className="font-medium text-main">
            {reviewerName(t, runType)}
          </span>
          <span className="wrap-break-word min-w-0 text-control-light">
            {t("plan.review.activity.reviewed-the-sql")}
          </span>
          <span className="text-control-light">·</span>
          <span className="text-control">
            {summary.join(t("plan.findings.separator"))}
          </span>
          <HumanizeTs
            className="text-xs text-control-light"
            tsMs={getTimeForPbTimestampProtoEs(source.time)}
          />
          {source.findings.length > 0 && (
            <Button
              aria-expanded={source.open}
              appearance="link"
              className="text-control-light"
              onClick={onToggle}
              size="xs"
            >
              {source.open ? t("common.hide") : t("common.show")}
            </Button>
          )}
        </>
      }
      icon={
        <CommentIconBadge
          className="bg-control-bg text-control"
          icon={<ReviewerIcon className="size-4" runType={runType} />}
        />
      }
      isLast={isLast}
    />
  );
}

function SyntheticIcon({
  type,
}: {
  type: "plan-created" | "ready-for-review";
}) {
  if (type === "ready-for-review") {
    return <ReviewSubmissionIcon />;
  }
  return (
    <CommentIconBadge
      className="bg-control-bg text-control"
      icon={<FileText className="size-4" />}
    />
  );
}

function SyntheticHeader({
  source,
}: {
  source: Extract<
    TimelineSource,
    { type: "plan-created" | "ready-for-review" }
  >;
}) {
  const { t } = useTranslation();
  return (
    <>
      <CommentCreator creator={source.creator} />
      {source.type === "plan-created" ? (
        <span className="wrap-break-word min-w-0 text-control-light">
          {t("plan.review.activity.created-this-plan")}
        </span>
      ) : (
        <ReviewSubmissionSentence />
      )}
      <HumanizeTs
        className="text-xs text-control-light"
        tsMs={getTimeForPbTimestampProtoEs(source.time)}
      />
    </>
  );
}

// A comment-backed timeline row: delegates layout/icon/header to the shared
// IssueCommentRow and owns the inline edit affordance (matching the issue page).
function ReviewCommentRow({
  comment,
  isLast,
  issue,
  plan,
  renderPlanChangeReference,
  similarCount,
}: {
  comment: IssueComment;
  isLast: boolean;
  issue: Issue;
  plan: Plan;
  renderPlanChangeReference: PlanChangeReferenceRenderer;
  similarCount?: number;
}) {
  const { t } = useTranslation();
  const page = usePlanDetailContext();
  const project = useProjectByName(`${projectNamePrefix}${page.projectId}`);
  const [isEditing, setIsEditing] = useState(false);
  const [editContent, setEditContent] = useState(comment.comment);
  const [saving, setSaving] = useState(false);
  const pageKeyRef = useRef(page.pageKey);
  const commentNameRef = useRef(comment.name);
  pageKeyRef.current = page.pageKey;
  commentNameRef.current = comment.name;

  useEffect(() => {
    setIsEditing(false);
    setEditContent(comment.comment);
    setSaving(false);
  }, [page.pageKey]);

  useEffect(() => {
    if (!isEditing) {
      setEditContent(comment.comment);
    }
  }, [comment.comment, isEditing]);

  const allowEdit = canEditIssueComment(comment, project);

  const save = async () => {
    if (!editContent || editContent === comment.comment) {
      setIsEditing(false);
      return;
    }
    const actionPageKey = page.pageKey;
    const actionCommentName = comment.name;
    try {
      setSaving(true);
      await useAppStore.getState().updateIssueComment({
        issueCommentName: comment.name,
        comment: editContent,
      });
      if (
        pageKeyRef.current !== actionPageKey ||
        commentNameRef.current !== actionCommentName
      ) {
        return;
      }
      setIsEditing(false);
    } catch (error) {
      if (
        pageKeyRef.current !== actionPageKey ||
        commentNameRef.current !== actionCommentName
      ) {
        return;
      }
      pushNotification({
        module: "bytebase",
        style: "CRITICAL",
        title: t("common.failed"),
        description: String(error),
      });
    } finally {
      if (
        pageKeyRef.current === actionPageKey &&
        commentNameRef.current === actionCommentName
      ) {
        setSaving(false);
      }
    }
  };

  let body: ReactNode;
  if (comment.comment) {
    body = isEditing ? (
      <div>
        <MarkdownEditor
          content={editContent}
          onChange={setEditContent}
          onSubmit={() => void save()}
        />
        <div className="mt-2 flex items-center justify-end gap-x-2">
          <Button
            onClick={() => setIsEditing(false)}
            size="xs"
            appearance="secondary"
          >
            {t("common.cancel")}
          </Button>
          <Button
            disabled={
              saving ||
              editContent.trim().length === 0 ||
              editContent === comment.comment
            }
            onClick={() => void save()}
            size="xs"
          >
            {saving && <Loader2 className="size-3.5 animate-spin" />}
            {t("common.save")}
          </Button>
        </div>
      </div>
    ) : (
      <MarkdownEditor content={comment.comment} mode="preview" />
    );
  }

  const subjectSuffix =
    allowEdit && !isEditing ? (
      <Button
        onClick={() => {
          setEditContent(comment.comment);
          setIsEditing(true);
        }}
        size="xs"
        appearance="secondary"
      >
        <Pencil className="size-3.5" />
      </Button>
    ) : undefined;

  return (
    <IssueCommentRow
      body={body}
      comment={comment}
      isLast={isLast}
      issue={issue}
      linkless
      plan={plan}
      renderPlanChangeReference={renderPlanChangeReference}
      similarCount={similarCount}
      subjectSuffix={subjectSuffix}
    />
  );
}

// A thread occupies one timeline entry at its root's position: the root with
// its statement anchor, the replies, and the thread footer, in one card.
function ThreadActivityRow({
  isLast,
  issue,
  plan,
  renderPlanChangeReference,
  thread,
}: {
  isLast: boolean;
  issue: Issue;
  plan: Plan;
  renderPlanChangeReference: PlanChangeReferenceRenderer;
  thread: CommentThread;
}) {
  const page = usePlanDetailContext();
  const project = useProjectByName(`${projectNamePrefix}${page.projectId}`);
  const { focusInStatement } = useFocusThread();
  const anchor = thread.root.statementAnchor;
  const spec = useMemo(
    () => plan.specs.find((candidate) => candidate.id === anchor?.spec),
    [anchor?.spec, plan.specs]
  );
  const targetSha256 = useMemo(() => targetSha256OfSpec(spec), [spec]);
  const placement = usePlanDetailStore((s) =>
    anchor
      ? placementOf(s, thread.root.name, anchor.spec, targetSha256)
      : undefined
  );

  const viewInStatement = () =>
    focusInStatement(
      thread.root,
      placement?.state === "CURRENT" ? placement.range.startLine : undefined
    );

  return (
    <ActivityRowFrame
      icon={
        <CommentIconBadge
          className="bg-control-bg text-control"
          icon={<MessagesSquare className="size-4" />}
        />
      }
      id={thread.root.name}
      isLast={isLast}
    >
      <CommentThreadCard
        className="ml-3"
        renderContext={
          anchor
            ? (onCollapse) => (
                <StatementAnchorContext
                  anchor={anchor}
                  onCollapse={onCollapse}
                  onViewInStatement={viewInStatement}
                  placement={placement}
                  plan={plan}
                  renderPlanChangeReference={renderPlanChangeReference}
                  project={project}
                />
              )
            : undefined
        }
        issue={issue}
        project={project}
        spec={spec}
        thread={thread}
      />
    </ActivityRowFrame>
  );
}