Skip to content

Commit 2191062

Browse files
committed
fix(useTasks): ensure clientId is set for optimistic task creation
Fix reconciliation issue where tasks created without explicit clientId could not be properly matched with server responses. The clientId is now guaranteed to be set, using the optimistic id as fallback when not provided by caller.
1 parent 7b1a4e1 commit 2191062

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

frontend/src/hooks/useTasks.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,18 @@ export function useCreateTask() {
6464
payload.id ||
6565
payload.clientId ||
6666
`tmp_${Math.random().toString(36).slice(2, 9)}`,
67+
clientId:
68+
payload?.clientId ||
69+
payload?.id ||
70+
// ensure we can reconcile later even if caller didn't provide id
71+
undefined,
6772
};
6873

74+
// if no clientId provided by caller, use the optimistic id as clientId
75+
if (!optimisticTask.clientId) {
76+
(optimisticTask as any).clientId = optimisticTask.id;
77+
}
78+
6979
for (const [qk, oldData] of prevEntries) {
7080
if (Array.isArray(oldData)) {
7181
qc.setQueryData(qk, [optimisticTask, ...oldData]);
@@ -74,7 +84,11 @@ export function useCreateTask() {
7484

7585
return {
7686
prevEntries,
77-
clientId: payload?.clientId ?? payload?.id ?? null,
87+
// carry the identifier we used for the optimistic record
88+
clientId:
89+
payload?.clientId ??
90+
payload?.id ??
91+
(optimisticTask as any).clientId,
7892
};
7993
},
8094
onSuccess: (data: any, payload: any, context: any) => {

0 commit comments

Comments
 (0)