Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
23.8 KB
Raw Blame
"use client";

import { useIssueStatuses } from "@multica/core/issue-statuses/hooks";
import { useWorkspaceId } from "@multica/core/hooks";

import { memo, useState, useCallback, useMemo, useEffect, useRef } from "react";
import { ChevronRight, Plus } from "@multica/ui/icons";
import { Accordion } from "@base-ui/react/accordion";
import {
  DndContext,
  DragOverlay,
  useDroppable,
  useSensor,
  useSensors,
  type DragStartEvent,
  type DragEndEvent,
  type DragOverEvent,
} from "@dnd-kit/core";
import { SortableContext, verticalListSortingStrategy, arrayMove } from "@dnd-kit/sortable";
import { Virtuoso } from "react-virtuoso";
import { Button } from "@multica/ui/components/ui/button";
import type { Issue, IssueStatus, Project } from "@multica/core/types";
import { useViewStore } from "@multica/core/issues/stores/view-store-context";
import { StatusHeading } from "./status-heading";
import { ListRow, DraggableListRow, rowParity, type ChildProgress } from "./list-row";
import { useDragSettle } from "./use-drag-settle";
import { createIssueDropAnimation, ISSUE_DRAG_OVERLAY_CLASS } from "./drag-drop-animation";
import { ListLoadMoreFooter } from "./list-load-more-footer";
import { useT } from "../../i18n";
import {
  type DragMoveUpdates,
  makeKanbanCollision,
  statusGroupId,
  buildColumns,
  computePosition,
  findColumn,
  getMoveAnchors,
  insertIdByPosition,
  issueMatchesGroup,
  getMoveUpdates,
  IssueCardPointerSensor,
} from "../utils/drag-utils";
import type { BoardColumnGroup } from "./board-column";
import { useIssueSurfaceSelection } from "../surface/selection-context";
import { useIssuePeekActions } from "../surface/peek-context";
import type { IssueCreateDefaults } from "../surface/types";
import type {
  IssueStatusPageState,
  IssueStatusPagination,
} from "../surface/use-issue-status-branches";
import { VirtuosoSeed, VIRTUOSO_SEED_COUNT } from "../../common/virtuoso-seed";
import { DeferredTooltip } from "../../common/deferred-tooltip";
import { useRestoredScrollRef } from "../../platform";
import { toast } from "sonner";

// The seed must use the same CSS height as ListRow before scroll restoration
// runs at ref-attach. Virtuoso receives the seed's resolved pixel height below.
const LIST_ROW_HEIGHT = "var(--issue-row-height)";

const EMPTY_PROGRESS_MAP = new Map<string, ChildProgress>();
const EMPTY_IDS: string[] = [];
const EMPTY_STATUS_PAGE: IssueStatusPageState = {
  total: 0, loaded: 0, hasMore: false, isLoading: false,
  isFetching: false, isError: false, loadMore: () => {}, retry: () => {},
};

function buildListGroups(visibleStatuses: IssueStatus[]): BoardColumnGroup[] {
  return visibleStatuses.map((status) => ({
    id: statusGroupId(status),
    title: status,
    status,
    createData: { status: status },
  }));
}

function ListViewImpl({
  issues,
  visibleStatuses,
  childProgressMap = EMPTY_PROGRESS_MAP,
  projectMap,
  statusPagination,
  projectId,
  onMoveIssue,
  onCreateIssue,
}: {
  issues: Issue[];
  visibleStatuses: IssueStatus[];
  childProgressMap?: Map<string, ChildProgress>;
  projectMap?: Map<string, Project>;
  statusPagination: IssueStatusPagination;
  projectId?: string;
  onMoveIssue?: (issueId: string, updates: DragMoveUpdates, onSettled?: () => void) => void;
  onCreateIssue?: (defaults: IssueCreateDefaults) => void;
}) {
  const listCollapsedStatuses = useViewStore(
    (s) => s.listCollapsedStatuses
  );
  const toggleListCollapsed = useViewStore(
    (s) => s.toggleListCollapsed
  );
  const sortBy = useViewStore((s) => s.sortBy);
  const { t } = useT("issues");
  const wsId = useWorkspaceId();
  const catalog = useIssueStatuses(wsId);

  const disableSorting = sortBy !== "position";

  const expandedStatuses = useMemo(
    () =>
      visibleStatuses.filter(
        (s) => !listCollapsedStatuses.includes(s)
      ),
    [visibleStatuses, listCollapsedStatuses]
  );

  const dragEnabled = !!onMoveIssue;

  // Side peek steps through the list top to bottom, skipping collapsed groups.
  const peek = useIssuePeekActions();

  const groups = useMemo(
    () => buildListGroups(visibleStatuses),
    [visibleStatuses],
  );
  const groupIds = useMemo(
    () => new Set(groups.map((g) => g.id)),
    [groups],
  );
  const groupMap = useMemo(
    () => new Map(groups.map((g) => [g.id, g])),
    [groups],
  );

  // --- Drag state ---
  const [activeIssue, setActiveIssue] = useState<Issue | null>(null);
  const skipDropAnimationRef = useRef(false);
  const dropAnimation = useMemo(() => createIssueDropAnimation(skipDropAnimationRef), []);
  // Shared drag/settle primitive (see use-drag-settle) — same machine as
  // board-view, so the two surfaces can't drift apart.
  const {
    columns,
    setColumns,
    columnsRef,
    isDraggingRef,
    isSettlingRef,
    recentlyMovedRef,
    settleVersion,
    beginSettle,
  } = useDragSettle(() => buildColumns(issues, groups, "status"));

  useEffect(() => {
    if (!isDraggingRef.current && !isSettlingRef.current) {
      setColumns(buildColumns(issues, groups, "status"));
    }
  }, [issues, groups, settleVersion, setColumns, isDraggingRef, isSettlingRef]);

  const issueMap = useMemo(() => {
    const map = new Map<string, Issue>();
    for (const issue of issues) map.set(issue.id, issue);
    return map;
  }, [issues]);

  const issueMapRef = useRef(issueMap);
  if (!isDraggingRef.current && !isSettlingRef.current) {
    issueMapRef.current = issueMap;
  }

  useEffect(() => {
    peek?.publishColumns([
      expandedStatuses.flatMap((status) => columns[statusGroupId(status)] ?? EMPTY_IDS),
    ]);
  }, [peek, expandedStatuses, columns]);
  useEffect(() => () => peek?.publishColumns(null), [peek]);

  const collisionDetection = useMemo(
    () => makeKanbanCollision(groupIds),
    [groupIds],
  );

  const sensors = useSensors(
    useSensor(IssueCardPointerSensor, {
      activationConstraint: { distance: 5 },
    })
  );

  const handleDragStart = useCallback(
    (event: DragStartEvent) => {
      skipDropAnimationRef.current = false;
      isDraggingRef.current = true;
      const issue = issueMapRef.current.get(event.active.id as string) ?? null;
      setActiveIssue(issue);
    },
    [isDraggingRef],
  );

  const handleDragOver = useCallback(
    (event: DragOverEvent) => {
      const { active, over } = event;
      if (!over || recentlyMovedRef.current) return;

      const activeId = active.id as string;
      const overId = over.id as string;

      setColumns((prev) => {
        const activeCol = findColumn(prev, activeId, groupIds);
        const overCol = findColumn(prev, overId, groupIds);
        if (!activeCol || !overCol || activeCol === overCol) return prev;
        const targetStatus = groups.find((group) => group.id === overCol)?.status;
        if (targetStatus && catalog.entryOf(targetStatus)?.archived_at) return prev;

        if (sortBy !== "position") return prev;

        recentlyMovedRef.current = true;
        const oldIds = prev[activeCol]!.filter((id) => id !== activeId);
        const newIds = [...prev[overCol]!];
        const overIndex = newIds.indexOf(overId);
        const insertIndex = overIndex >= 0 ? overIndex : newIds.length;
        newIds.splice(insertIndex, 0, activeId);
        return { ...prev, [activeCol]: oldIds, [overCol]: newIds };
      });
    },
    [groupIds, groups, catalog, sortBy, recentlyMovedRef, setColumns],
  );

  const handleDragEnd = useCallback(
    (event: DragEndEvent) => {
      const { active, over } = event;
      isDraggingRef.current = false;
      setActiveIssue(null);

      const resetColumns = () => {
        skipDropAnimationRef.current = true;
        setColumns(buildColumns(issues, groups, "status"));
      };

      if (!over || !onMoveIssue) {
        resetColumns();
        return;
      }

      const activeId = active.id as string;
      const overId = over.id as string;

      const cols = columnsRef.current;
      const activeCol = findColumn(cols, activeId, groupIds);
      const overCol = findColumn(cols, overId, groupIds);
      if (!activeCol || !overCol) {
        resetColumns();
        return;
      }

      let finalColumns = cols;
      if (activeCol === overCol && sortBy === "position") {
        const ids = cols[activeCol]!;
        const oldIndex = ids.indexOf(activeId);
        const newIndex = ids.indexOf(overId);
        if (oldIndex !== -1 && newIndex !== -1 && oldIndex !== newIndex) {
          const reordered = arrayMove(ids, oldIndex, newIndex);
          finalColumns = { ...cols, [activeCol]: reordered };
          setColumns(finalColumns);
        }
      }

      const finalCol = sortBy === "position"
        ? findColumn(finalColumns, activeId, groupIds)
        : overCol;
      if (!finalCol) {
        resetColumns();
        return;
      }
      const finalGroup = groupMap.get(finalCol);
      if (!finalGroup) {
        resetColumns();
        return;
      }

      const map = issueMapRef.current;
      if (finalGroup.status && map.get(activeId)?.status !== finalGroup.status && catalog.entryOf(finalGroup.status)?.archived_at) {
        resetColumns();
        return;
      }

      if (sortBy !== "position") {
        const currentIssue = map.get(activeId);
        if (!currentIssue || issueMatchesGroup(currentIssue, finalGroup)) {
          resetColumns();
          if (activeId !== overId) {
            toast.info(t(($) => $.board.manual_reorder_hint), {
              id: "issue-manual-reorder-hint",
            });
          }
          return;
        }
        // Optimistically move the row into the target group *now*. Without this
        // the sortBy != "position" branch never touched local columns on drop,
        // so the row sat in its origin group for the whole request and only
        // jumped across when the mutation settled — the same "snaps back, then
        // moves" glitch the board view had. Placement mirrors the cache
        // (insertIdByPosition) so the settle rebuild is a visual no-op.
        const targetIds = insertIdByPosition(
          (cols[finalCol] ?? []).filter((id) => id !== activeId),
          activeId,
          currentIssue.position,
          map,
        );
        setColumns((prev) => {
          const fromIds = (prev[activeCol] ?? []).filter((cid) => cid !== activeId);
          return { ...prev, [activeCol]: fromIds, [finalCol]: targetIds };
        });
        onMoveIssue(
          activeId,
          {
            ...getMoveUpdates(finalGroup, currentIssue.position, currentIssue),
            ...getMoveAnchors(targetIds, activeId),
          },
          beginSettle(),
        );
        return;
      }

      const finalIds = finalColumns[finalCol]!;
      const newPosition = computePosition(finalIds, activeId, map);
      const currentIssue = map.get(activeId);

      if (
        currentIssue &&
        issueMatchesGroup(currentIssue, finalGroup) &&
        currentIssue.position === newPosition
      ) {
        return;
      }

      // beginSettle() also bumps settleVersion on settle (board-view did, this
      // branch did not) so a failed position move reverts instead of stranding
      // the row at the drop target.
      onMoveIssue(
        activeId,
        {
          ...getMoveUpdates(finalGroup, newPosition, currentIssue),
          ...getMoveAnchors(finalIds, activeId),
        },
        beginSettle(),
      );
    },
    [issues, groups, onMoveIssue, groupIds, groupMap, sortBy, beginSettle, setColumns, columnsRef, isDraggingRef, catalog, t],
  );

  // dnd-kit fires onDragCancel — never onDragEnd — when an active drag is
  // aborted: pointercancel, window resize, tab hide, or Escape. Touch browsers
  // hit that path constantly, because a scroll gesture that starts on a row
  // moves past the 5px activation distance and *then* the browser takes the
  // gesture over for native scrolling and cancels the pointer. Without this
  // handler `isDraggingRef` stayed true for the rest of the session, which
  // froze the column mirror against cache updates and — because the accordion's
  // onValueChange is guarded by the same ref — made tapping a status header a
  // no-op, so groups could no longer be collapsed at all (MUL-6240).
  const handleDragCancel = useCallback(() => {
    skipDropAnimationRef.current = true;
    isDraggingRef.current = false;
    setActiveIssue(null);
    setColumns(buildColumns(issues, groups, "status"));
  }, [issues, groups, setColumns, isDraggingRef]);

  // The single scroll container is shared by every status panel's Virtuoso as
  // its customScrollParent, so a callback ref hands the element to the panels
  // once it mounts. Keeping one scroller (rather than one per panel) preserves
  // the current sticky-header + cross-section scroll behavior; only the rows
  // inside each expanded panel virtualize.
  const [scrollEl, setScrollEl] = useState<HTMLDivElement | null>(null);
  const [rowHeight, setRowHeight] = useState<number>();
  // Pull-based scroll restoration (MUL-4741): assign the saved offset when
  // the shared scroller attaches — the per-status seeds plus their estimate
  // spacers give it a truthful height on the first commit.
  const restoreScrollRef = useRestoredScrollRef("list");
  const attachScroller = useCallback(
    (el: HTMLDivElement | null) => {
      const row = el?.querySelector<HTMLElement>('[data-slot="issue-list-row"]');
      // Read the rendered height, not parseFloat of the custom property: the
      // source token may be a rem/calc length. With no seed rows, let Virtuoso
      // measure its first item when a group is expanded or data arrives.
      const height = row ? Number.parseFloat(getComputedStyle(row).height) : 0;
      setRowHeight(height > 0 ? height : undefined);
      setScrollEl(el);
      restoreScrollRef(el);
    },
    [restoreScrollRef],
  );

  const content = (
    <>
      <Accordion.Root
        multiple
        className="space-y-1"
        value={expandedStatuses}
        onValueChange={(value: string[]) => {
          if (isDraggingRef.current) return;
          for (const status of visibleStatuses) {
            const wasExpanded = expandedStatuses.includes(status);
            const isExpanded = value.includes(status);
            if (wasExpanded !== isExpanded) {
              toggleListCollapsed(status as IssueStatus);
            }
          }
        }}
      >
        {visibleStatuses.map((status) => {
          const isExpanded = expandedStatuses.includes(status);
          return (
            <StatusAccordionItem
              key={status}
              status={status}
              issueIds={columns[statusGroupId(status)] ?? EMPTY_IDS}
              issueMap={issueMapRef.current}
              childProgressMap={childProgressMap}
              projectMap={projectMap}
              page={statusPagination[status] ?? EMPTY_STATUS_PAGE}
              projectId={projectId}
              onCreateIssue={onCreateIssue}
              dragEnabled={dragEnabled}
              isExpanded={isExpanded}
              disableSorting={disableSorting}
              scrollParent={scrollEl}
              rowHeight={rowHeight}
            />
          );
        })}
      </Accordion.Root>
    </>
  );

  if (!dragEnabled) {
    return (
      <div ref={attachScroller} data-tab-scroll-root="list" className="flex-1 min-h-0 overflow-y-auto p-2 pt-0">
        {content}
      </div>
    );
  }

  return (
    <DndContext
      sensors={sensors}
      collisionDetection={collisionDetection}
      onDragStart={handleDragStart}
      onDragOver={handleDragOver}
      onDragEnd={handleDragEnd}
      onDragCancel={handleDragCancel}
    >
      <div ref={attachScroller} data-tab-scroll-root="list" className="flex-1 min-h-0 overflow-y-auto p-2 pt-0">
        {content}
      </div>

      <DragOverlay dropAnimation={dropAnimation}>
        {activeIssue ? (
          <div className={`max-w-2xl rounded-md border border-border bg-card px-4 py-2 ${ISSUE_DRAG_OVERLAY_CLASS}`}>
            <span className="text-caption text-muted-foreground mr-2">{activeIssue.identifier}</span>
            <span className="text-body">{activeIssue.title}</span>
          </div>
        ) : null}
      </DragOverlay>
    </DndContext>
  );
}

function StatusAccordionItem({
  status,
  issueIds,
  issueMap,
  childProgressMap,
  projectMap,
  page,
  projectId,
  onCreateIssue,
  dragEnabled,
  isExpanded,
  disableSorting,
  scrollParent,
  rowHeight,
}: {
  status: IssueStatus;
  issueIds: string[];
  issueMap: Map<string, Issue>;
  childProgressMap: Map<string, ChildProgress>;
  projectMap?: Map<string, Project>;
  page: IssueStatusPageState;
  projectId?: string;
  onCreateIssue?: (defaults: IssueCreateDefaults) => void;
  dragEnabled: boolean;
  isExpanded: boolean;
  disableSorting: boolean;
  scrollParent: HTMLElement | null;
  rowHeight: number | undefined;
}) {
  const { t } = useT("issues");
  const selection = useIssueSurfaceSelection();
  const selectedIds = selection.selectedIds;
  const select = selection.select;
  const deselect = selection.deselect;

  const issues = useMemo(
    () => issueIds.flatMap((id) => {
      const issue = issueMap.get(id);
      return issue ? [issue] : [];
    }),
    [issueIds, issueMap],
  );

  const selectedCount = issueIds.filter((id) => selectedIds.has(id)).length;
  const allSelected = issues.length > 0 && selectedCount === issues.length;
  const someSelected = selectedCount > 0;

  const statusWsId = useWorkspaceId();
  const statusCatalog = useIssueStatuses(statusWsId);
  const { setNodeRef: setDroppableRef, isOver: droppableIsOver } = useDroppable({
    id: statusGroupId(status),
    disabled: !dragEnabled,
  });
  const isOver = droppableIsOver && !statusCatalog.entryOf(status)?.archived_at;

  const addIssueLabel = t(($) => $.list.add_issue_tooltip);

  // The infinite-scroll sentinel rides Virtuoso's Footer so it sits at the true
  // end of the virtualized rows and still fires loadMore when scrolled to it.
  const listComponents = useMemo(
    () => ({
      // Always a non-undefined object: react-virtuoso throws if `components`
      // is ever undefined (MUL-4474). The footer itself renders null for a
      // short, non-paginated section.
      Footer: () => (
        <ListLoadMoreFooter
          hasMore={page.hasMore}
          isLoading={page.isLoading || page.isFetching}
          total={page.total}
          onLoadMore={page.loadMore}
          isError={page.isError}
          onRetry={page.retry}
        />
      ),
    }),
    [page],
  );

  const computeItemKey = (_index: number, issue: Issue) => issue.id;
  const itemContent = (index: number, issue: Issue) =>
    dragEnabled ? (
      <DraggableListRow
        issue={issue}
        parity={rowParity(index)}
        childProgress={childProgressMap.get(issue.id)}
        project={
          issue.project_id && issue.project_id !== projectId
            ? projectMap?.get(issue.project_id)
            : undefined
        }
        disableSorting={disableSorting}
      />
    ) : (
      <ListRow
        issue={issue}
        parity={rowParity(index)}
        childProgress={childProgressMap.get(issue.id)}
        project={
          issue.project_id && issue.project_id !== projectId
            ? projectMap?.get(issue.project_id)
            : undefined
        }
      />
    );

  // Rows virtualize into the page's shared scroll parent. Only render when the
  // section is expanded and non-empty — a Virtuoso in a collapsed (0-height /
  // hidden) panel has no viewport to measure. While the shared scroll parent
  // is still null (callback ref not settled after a route-return remount),
  // seed a bounded slice of real rows so the first painted frame isn't blank;
  // once it's set, mount the Virtuoso with a matching `initialItemCount` so the
  // measurement frame keeps those rows instead of flashing empty (MUL-4750).
  // The droppable, SortableContext, sticky header, and collapse are unchanged;
  // virtualization only decides whether an off-screen row is in the DOM.
  const rows =
    isExpanded && issues.length > 0 ? (
      scrollParent ? (
        <Virtuoso
          customScrollParent={scrollParent}
          data={issues}
          computeItemKey={computeItemKey}
          initialItemCount={Math.min(issues.length, VIRTUOSO_SEED_COUNT)}
          defaultItemHeight={rowHeight}
          increaseViewportBy={{ top: 400, bottom: 400 }}
          components={listComponents}
          itemContent={itemContent}
        />
      ) : (
        <VirtuosoSeed
          data={issues}
          itemContent={itemContent}
          computeItemKey={computeItemKey}
          estimatedItemHeight={LIST_ROW_HEIGHT}
        />
      )
    ) : null;

  return (
    <Accordion.Item value={status} ref={dragEnabled ? setDroppableRef : undefined}>
      <Accordion.Header
        data-slot="issue-group-header"
        data-mc="issue-group-header"
        data-status={status}
        className={`group/header sticky top-0 z-10 flex h-[var(--issue-group-header-height,2.5rem)] items-center rounded-issue-group-header bg-issue-group-header transition-colors hover:bg-issue-group-header-hover ${
          isOver && !isExpanded
            ? "ring-2 ring-brand/25 bg-accent/15"
            : ""
        }`}
      >
        <div className="pl-3 flex items-center">
          <input
            type="checkbox"
            checked={allSelected}
            ref={(el) => {
              if (el) el.indeterminate = someSelected && !allSelected;
            }}
            onChange={() => {
              if (allSelected) {
                deselect(issueIds);
              } else {
                select(issueIds);
              }
            }}
            className="cursor-pointer accent-primary"
          />
        </div>
        <Accordion.Trigger className="group/trigger flex flex-1 items-center gap-2 px-2 h-full text-left outline-none cursor-pointer">
          <ChevronRight className="size-3.5 shrink-0 text-muted-foreground transition-transform group-aria-expanded/trigger:rotate-90" />
          <StatusHeading status={status} count={page.total} />
        </Accordion.Trigger>
        {onCreateIssue && !statusCatalog.entryOf(status)?.archived_at && (
          <div className="pr-2">
            {/* Lazy-mounted tooltip machinery — see DeferredTooltip. */}
            <DeferredTooltip
              content={addIssueLabel}
              trigger={
                <Button
                  variant="ghost"
                  size="icon-sm"
                  className="text-muted-foreground opacity-0 group-hover/header:opacity-100 transition-opacity"
                  aria-label={addIssueLabel}
                  onClick={() => {
                    const defaults = {
                      status: status,
                      ...(projectId ? { project_id: projectId } : {}),
                    };
                    onCreateIssue(defaults);
                  }}
                >
                  <Plus className="size-3.5" />
                </Button>
              }
            />
          </div>
        )}
      </Accordion.Header>
      <Accordion.Panel>
        {issues.length > 0 ? (
          dragEnabled ? (
            <SortableContext items={issueIds} strategy={verticalListSortingStrategy}>
              {rows}
            </SortableContext>
          ) : (
            rows
          )
        ) : page.isError ? (
          <ListLoadMoreFooter
            hasMore={page.hasMore}
            isLoading={page.isLoading || page.isFetching}
            total={page.total}
            onLoadMore={page.loadMore}
            isError
            onRetry={page.retry}
            errorLabel={t(($) => $.detail.section_load_failed_retry)}
          />
        ) : (
          <p className="py-6 text-center text-caption text-muted-foreground">
            {t(($) => $.list.empty_status)}
          </p>
        )}
      </Accordion.Panel>
    </Accordion.Item>
  );
}

/**
 * Memoized: the surface controller re-renders on loading-flag flips (e.g. a
 * query enabling when the view changes) — without memo every such flip
 * re-rendered this entire view tree (hundreds of ms). All props are
 * referentially stable useMemo/useCallback outputs from the controller.
 */
export const ListView = memo(ListViewImpl);