Files
multica/packages/views/issues/surface/use-issue-surface-data.ts
Naiyuan Qing ade6b34e5f MUL-3903: Extract shared issue surfaces (#4774)
* MUL-3903 refactor project issue surface state

Co-authored-by: multica-agent <github@multica.ai>

* Refactor project issue surface ownership

Co-authored-by: multica-agent <github@multica.ai>

* Extract shared issue surface entrypoints

Co-authored-by: multica-agent <github@multica.ai>

* Fix issue surface create defaults and selection reset

Co-authored-by: multica-agent <github@multica.ai>

* test(editor): add missing AbortSignal to suggestion items() calls

The suggestion items() contract gained a required signal param; the
mention/slash test call sites were never updated, breaking pnpm typecheck
for @multica/views.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(issues): server-side assignee_types filter on ListIssues

ListGroupedIssues has taken assignee_types since squads shipped, but
ListIssues never did — so the workspace Members/Agents tabs had to fetch
the unfiltered workspace list and post-filter loaded pages client-side,
which made column totals and load-more pagination reflect the unfiltered
counts.

Add the same parse + WHERE clause to ListIssues (count query shares the
WHERE, so totals agree), thread the param through the TS client, and
widen MyIssuesFilter so scoped list caches can carry it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(issues): route issue cache writes through a membership-aware coordinator

useUpdateIssue, useBatchUpdateIssues, and the WS issue:updated handler
each maintained their own similar-but-diverging patch/invalidate rules.
Consolidate them into cache-coordinator.ts (applyIssueChange /
rollbackIssueChange / invalidateIssueDerivatives) so local writes and
remote echoes follow one rules table by construction.

The coordinator is membership-aware via surface/membership.ts
(true | false | unknown against each list cache's own filter contract):

- a change that moves an issue off a filtered surface removes the card
  surgically (bucket total decremented) — fixes assignee changes leaving
  stale cards on My Assigned with no local safety net (previously only
  the WS echo recovered it), and replaces the blanket invalidate-myAll
  net for project moves (MUL-3669) with per-key precision
- possible entry into a loaded list marks that key stale — never
  hard-insert; page/slot is server knowledge
- stale keys flush on settle for mutations (a mid-flight refetch would
  stomp the optimistic state) and immediately for WS
- batch updates now patch detail + inbox like single updates; the
  off-screen bucket-count recovery previously exclusive to the WS path
  now covers local mutations too

Preserved invariants: synchronous optimistic patches (dnd-kit), MUL-3375
control-field stripping, and no refetch of surgically reconciled lists
(the drag-flicker fix).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(issues): resolve surfaces via core query plan/repository with window-keyed remount

Read-path convergence and the loading/empty semantics that fall out of
it:

- scope -> API params moves from scope.ts helpers into
  surface/query-plan.ts; workspace members/agents become server-filtered
  scoped plans (assignee_types) and the client postFilter machinery is
  deleted — tab counts and load-more are now exact
- query selection moves behind surface/repository.ts; the views data
  hook no longer branches on workspace-vs-scoped plumbing
- IssueSurfaceContent remounts on data-window change (wsId + scope):
  keepPreviousData placeholders keep sort/filter changes flicker-free
  within one window but must never let project A's (or workspace A's)
  cards impersonate B's with no loading state — cold window shows the
  skeleton, warm window hits cache instantly
- isEmpty is only asserted from full-window data; the gantt
  scheduled-only projection can't prove the window is empty, so GanttView's
  own "no scheduled issues" empty state renders instead of the generic
  create-issue one
- per-card project lookups hoist into a surface-level projectMap (drops
  a per-card useQuery), create-defaults typing tightens to
  IssueCreateDefaults

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* perf(issues): count-only arithmetic for off-window status/membership changes

An issue beyond a list's loaded page window used to force a full
first-page refetch just to fix two column counts. When the change is
CERTAIN (base entity known, membership definitive) the coordinator now
does the arithmetic locally:

- stayed a member + status changed: move one unit of total between the
  two buckets (loaded arrays untouched; hasMore stays consistent)
- left the list (reassigned / re-projected): old status bucket total -1
- member-to-member reassignment: counts unaffected, not even a stale key

Entering a list and any uncertainty (no base, unknown membership) still
refetch — the right page/slot is server knowledge. Branches on membership
OUTCOMES, not on which field changed, so future dimensions (team) join
automatically. Biggest win is the WS path: agents flipping off-screen
statuses no longer trigger refetch storms.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(issues): deferred view-refresh indicator during placeholder revalidation

Sort/date changes (and any grouped-board filter change) revalidate behind
the previous snapshot — correct, but on a slow network the click felt
dead: content stays put and isLoading never fires. Surface the state as
isRefreshing (isPlaceholderData of the active query) and render a shared
ViewRefreshIndicator in every issues header: a fixed-width slot (zero
layout shift) whose spinner fades in after 300ms, so sub-second responses
show nothing (NN/g) while slow ones get a working signal.

Bound to the revalidation STATE, not to any particular control — any
current or future server-side view change lights it automatically.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: multica-agent <github@multica.ai>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 14:11:10 +08:00

327 lines
9.4 KiB
TypeScript

"use client";
import { useMemo } from "react";
import { useQuery, type QueryKey } from "@tanstack/react-query";
import type { Issue, IssueAssigneeGroup, Project } from "@multica/core/types";
import { BOARD_STATUSES } from "@multica/core/issues/config";
import { projectListOptions } from "@multica/core/projects/queries";
import {
childIssueProgressOptions,
type AssigneeGroupedIssuesFilter,
type IssueSortParam,
type MyIssuesFilter,
} from "@multica/core/issues/queries";
import {
issueSurfaceAssigneeGroupsOptions,
issueSurfaceGanttOptions,
issueSurfaceListOptions,
} from "@multica/core/issues/surface/repository";
import type { IssueSurfaceQueryPlan } from "@multica/core/issues/surface/query-plan";
import type { IssueStatus } from "@multica/core/types";
import {
applyIssueFilters,
filterAssigneeGroups,
type IssueFilterState,
type IssueFilters,
} from "../utils/filter";
import type { ChildProgress } from "../components/list-row";
import {
useIssueSurfaceActivity,
type IssueSurfaceActivity,
} from "./activity";
const EMPTY_ISSUES: Issue[] = [];
const EMPTY_CHILD_PROGRESS = new Map<string, ChildProgress>();
const EMPTY_PROJECTS: Project[] = [];
export interface IssueSurfaceData {
surfaceIssues: Issue[];
projectIssues: Issue[];
issues: Issue[];
swimlaneIssues: Issue[];
filteredGanttIssues: Issue[];
assigneeGroups?: IssueAssigneeGroup[];
assigneeGroupQueryKey?: QueryKey;
assigneeGroupFilter?: AssigneeGroupedIssuesFilter;
filter: MyIssuesFilter;
loadMoreScope?: string;
loadMoreFilter?: MyIssuesFilter;
ganttIssues: Issue[];
visibleStatuses: typeof BOARD_STATUSES;
hiddenStatuses: typeof BOARD_STATUSES;
activeFilters: Omit<IssueFilters, "statusFilters" | "runningIssueIds">;
activity: IssueSurfaceActivity;
childProgressMap: Map<string, ChildProgress>;
projectMap: Map<string, Project>;
isLoading: boolean;
/** The window's data is being revalidated while the previous snapshot is
* shown as a placeholder (sort/date change, or any grouped-board filter
* change). Drives the header's deferred refresh indicator — content stays
* put, so this is NOT a loading state. */
isRefreshing: boolean;
isEmpty: boolean;
}
export function useIssueSurfaceData({
wsId,
queryPlan,
projectId,
usesAssigneeBoard,
usesGantt,
sort,
statusFilters,
priorityFilters,
assigneeFilters,
includeNoAssignee,
creatorFilters,
projectFilters,
includeNoProject,
labelFilters,
agentRunningFilter,
showSubIssues,
loadProjects,
}: {
wsId: string;
queryPlan: IssueSurfaceQueryPlan;
projectId?: string;
usesAssigneeBoard: boolean;
usesGantt: boolean;
sort: IssueSortParam;
statusFilters: IssueStatus[];
priorityFilters: IssueFilterState["priorityFilters"];
assigneeFilters: IssueFilterState["assigneeFilters"];
includeNoAssignee: boolean;
creatorFilters: IssueFilterState["creatorFilters"];
projectFilters: string[];
includeNoProject: boolean;
labelFilters: string[];
agentRunningFilter: boolean;
showSubIssues: boolean;
loadProjects: boolean;
}): IssueSurfaceData {
const activity = useIssueSurfaceActivity();
const filterContext = useMemo(
() => ({ activityByIssueId: activity.activityByIssueId }),
[activity.activityByIssueId],
);
const assigneeGroupFilter = useMemo<AssigneeGroupedIssuesFilter>(
() => ({
...queryPlan.groupedScopeFilter,
statuses: statusFilters.length > 0 ? statusFilters : [...BOARD_STATUSES],
priorities: priorityFilters,
assignee_filters: assigneeFilters,
include_no_assignee: includeNoAssignee,
creator_filters: creatorFilters,
project_ids: projectFilters,
include_no_project: includeNoProject,
label_ids: labelFilters,
}),
[
assigneeFilters,
creatorFilters,
includeNoAssignee,
includeNoProject,
labelFilters,
priorityFilters,
projectFilters,
queryPlan.groupedScopeFilter,
statusFilters,
],
);
const activeAssigneeGroupsOptions = issueSurfaceAssigneeGroupsOptions(
wsId,
queryPlan,
assigneeGroupFilter,
sort,
);
const statusIssuesQuery = useQuery({
...issueSurfaceListOptions(wsId, queryPlan, sort),
enabled: !usesAssigneeBoard && !usesGantt,
});
const assigneeGroupsQuery = useQuery({
...activeAssigneeGroupsOptions,
enabled: usesAssigneeBoard,
});
const ganttIssuesQuery = useQuery({
...issueSurfaceGanttOptions(wsId, projectId ?? ""),
enabled: usesGantt,
});
const bucketedIssues = useMemo(() => {
return usesAssigneeBoard
? (assigneeGroupsQuery.data?.groups.flatMap((group) => group.issues) ?? [])
: (statusIssuesQuery.data ?? EMPTY_ISSUES);
}, [assigneeGroupsQuery.data?.groups, statusIssuesQuery.data, usesAssigneeBoard]);
const ganttIssues = ganttIssuesQuery.data ?? EMPTY_ISSUES;
const surfaceIssues = usesGantt ? ganttIssues : bucketedIssues;
const baseFilterState = useMemo<IssueFilterState>(
() => ({
statusFilters,
priorityFilters,
assigneeFilters,
includeNoAssignee,
creatorFilters,
projectFilters,
includeNoProject,
labelFilters,
workingOnly: agentRunningFilter,
showSubIssues,
}),
[
agentRunningFilter,
assigneeFilters,
creatorFilters,
includeNoAssignee,
includeNoProject,
labelFilters,
priorityFilters,
projectFilters,
showSubIssues,
statusFilters,
],
);
const issues = useMemo(
() => applyIssueFilters(surfaceIssues, baseFilterState, filterContext),
[baseFilterState, filterContext, surfaceIssues],
);
const statuslessFilterState = useMemo<IssueFilterState>(
() => ({
...baseFilterState,
statusFilters: [],
}),
[baseFilterState],
);
const swimlaneIssues = useMemo(
() => applyIssueFilters(surfaceIssues, statuslessFilterState, filterContext),
[filterContext, statuslessFilterState, surfaceIssues],
);
const filteredGanttIssues = useMemo(
() => applyIssueFilters(ganttIssues, baseFilterState, filterContext),
[baseFilterState, filterContext, ganttIssues],
);
// The assignee-grouped board renders straight from `groups`, bypassing the
// flat applyIssueFilters output — re-apply the client-only display filters
// (Show sub-issues + agents-working) per group.
const filteredAssigneeGroups = useMemo(
() =>
filterAssigneeGroups(assigneeGroupsQuery.data?.groups, {
showSubIssues,
agentRunningFilter,
runningIssueIds: activity.runningIssueIds,
}),
[
activity.runningIssueIds,
agentRunningFilter,
assigneeGroupsQuery.data?.groups,
showSubIssues,
],
);
const { data: childProgressMap = EMPTY_CHILD_PROGRESS } = useQuery(
childIssueProgressOptions(wsId),
);
const { data: projects = EMPTY_PROJECTS } = useQuery({
...projectListOptions(wsId),
enabled: loadProjects,
});
const projectMap = useMemo(
() => new Map(projects.map((project) => [project.id, project])),
[projects],
);
const visibleStatuses = useMemo(() => {
if (statusFilters.length > 0) {
return BOARD_STATUSES.filter((s) => statusFilters.includes(s));
}
return BOARD_STATUSES;
}, [statusFilters]);
const hiddenStatuses = useMemo(
() => BOARD_STATUSES.filter((s) => !visibleStatuses.includes(s)),
[visibleStatuses],
);
const activeFilters = useMemo(
() => ({
priorityFilters,
assigneeFilters,
includeNoAssignee,
creatorFilters,
projectFilters,
includeNoProject,
labelFilters,
agentRunningFilter,
showSubIssues,
}),
[
agentRunningFilter,
assigneeFilters,
creatorFilters,
includeNoAssignee,
includeNoProject,
labelFilters,
priorityFilters,
projectFilters,
showSubIssues,
],
);
const isLoading = usesAssigneeBoard
? assigneeGroupsQuery.isLoading
: usesGantt
? ganttIssuesQuery.isLoading
: statusIssuesQuery.isLoading;
// Placeholder-backed revalidation of the ACTIVE query only. First loads are
// isLoading (no previous data to place-hold); gantt has no placeholder
// phase (its key carries no sort/filter).
const isRefreshing = usesAssigneeBoard
? assigneeGroupsQuery.isPlaceholderData
: usesGantt
? false
: statusIssuesQuery.isPlaceholderData;
return {
surfaceIssues,
projectIssues: surfaceIssues,
issues,
swimlaneIssues,
filteredGanttIssues,
assigneeGroups: usesAssigneeBoard ? filteredAssigneeGroups : undefined,
assigneeGroupQueryKey: usesAssigneeBoard
? activeAssigneeGroupsOptions.queryKey
: undefined,
assigneeGroupFilter: usesAssigneeBoard ? assigneeGroupFilter : undefined,
filter: queryPlan.queryFilter,
loadMoreScope: queryPlan.loadMoreScope,
loadMoreFilter: queryPlan.loadMoreFilter,
ganttIssues,
visibleStatuses,
hiddenStatuses,
activeFilters,
activity,
childProgressMap,
projectMap,
isLoading,
isRefreshing,
// isEmpty asserts "this window has no issues". The board/list/swimlane
// data IS the full window, so an empty result proves it. The gantt query
// is a scheduled-only PROJECTION — an empty subset cannot prove the
// window is empty, so never claim it (same "uncertain → don't assert"
// rule as surface membership). GanttView renders its own accurate
// "no scheduled issues" empty state instead of the generic create-issue
// one.
isEmpty: !isLoading && !usesGantt && surfaceIssues.length === 0,
};
}