Skip to content

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

Go

fix(labels): apply label attach optimistically (#1746)

Bohan Jiang1292ecf71bc5
diff --git a/packages/core/labels/mutations.ts b/packages/core/labels/mutations.ts
index 354c72046f..23a2654d39 100644
--- a/packages/core/labels/mutations.ts
+++ b/packages/core/labels/mutations.ts
@@ -101,15 +101,37 @@ export function useAttachLabel(issueId: string) {
const wsId = useWorkspaceId();
return useMutation({
mutationFn: (labelId: string) => api.attachLabel(issueId, labelId),
+ onMutate: async (labelId) => {
+ await qc.cancelQueries({ queryKey: labelKeys.byIssue(wsId, issueId) });
+ const prev = qc.getQueryData<IssueLabelsResponse>(labelKeys.byIssue(wsId, issueId));
+ // Only patch when we already know the current label set — otherwise
+ // appending `[label]` to an empty array would wipe denormalized
+ // labels in issue list/detail caches and rollback couldn't restore
+ // them. If byIssue isn't cached yet (user clicked before the picker
+ // fetched), skip the optimistic patch and rely on onSettled refetch.
+ if (!prev) return { prev };
+ if (prev.labels.some((l) => l.id === labelId)) return { prev };
+ const list = qc.getQueryData<ListLabelsResponse>(labelKeys.list(wsId));
+ const label = list?.labels.find((l) => l.id === labelId);
+ if (!label) return { prev };
+ const next: IssueLabelsResponse = { ...prev, labels: [...prev.labels, label] };
+ qc.setQueryData<IssueLabelsResponse>(labelKeys.byIssue(wsId, issueId), next);
+ onIssueLabelsChanged(qc, wsId, issueId, next.labels);
+ return { prev };
+ },
+ onError: (_err, _id, ctx) => {
+ if (ctx?.prev) {
+ qc.setQueryData(labelKeys.byIssue(wsId, issueId), ctx.prev);
+ onIssueLabelsChanged(qc, wsId, issueId, ctx.prev.labels);
+ }
+ },
onSuccess: (data: IssueLabelsResponse) => {
// Backend may return an empty object when the post-mutation read fails
- // (it logs a warning and skips the broadcast). We only apply the list
- // when the backend gave us one — otherwise rely on onSettled's
- // invalidation to refetch.
+ // (it logs a warning and skips the broadcast). Only apply the list
+ // when the backend gave us one — otherwise the optimistic patch from
+ // onMutate stands until onSettled's invalidation refetches.
if (data && Array.isArray(data.labels)) {
qc.setQueryData<IssueLabelsResponse>(labelKeys.byIssue(wsId, issueId), data);
- // Mirror into the issues list / detail caches so list/board chips
- // update immediately for the actor without waiting for the WS event.
onIssueLabelsChanged(qc, wsId, issueId, data.labels);
}
},