Skip to content

Commit 96034fa

Browse files
committed
feat(fork): optimistic sidebar entry + Fork | prefix for forked tasks (#4353)
* feat(fork): optimistic sidebar entry + rename to 'Fork | Chat Name' Adds the forked task to the sidebar immediately by writing an optimistic entry into the task list cache in onSuccess, before the background refetch arrives. Without this, the sidebar showed no new task until the invalidation refetch completed after navigation. Also moves the Fork prefix to the front of the name ('Fork | Chat Name') so it is immediately scannable in the sidebar. * fix(fork): cancel in-flight queries before optimistic sidebar write
1 parent a41b89d commit 96034fa

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

apps/sim/app/api/mothership/chats/[chatId]/fork/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ export const POST = withRouteHandler(
7474
: []
7575

7676
const newId = generateId()
77-
const baseTitle = (parent.title ?? 'New task').replace(/ \| Fork$/, '')
78-
const title = `${baseTitle} | Fork`
77+
const baseTitle = (parent.title ?? 'New task').replace(/^Fork \| /, '')
78+
const title = `Fork | ${baseTitle}`
7979
const now = new Date()
8080

8181
const [newChat] = await db

apps/sim/hooks/queries/tasks.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,25 @@ export function useForkTask(workspaceId?: string) {
609609
const queryClient = useQueryClient()
610610
return useMutation({
611611
mutationFn: forkChat,
612+
onSuccess: async (data, variables) => {
613+
await queryClient.cancelQueries({ queryKey: taskKeys.list(workspaceId) })
614+
const existing = queryClient.getQueryData<TaskMetadata[]>(taskKeys.list(workspaceId))
615+
if (existing) {
616+
const sourceTask = existing.find((t) => t.id === variables.chatId)
617+
const baseName = (sourceTask?.name ?? 'New task').replace(/^Fork \| /, '')
618+
const optimisticTask: TaskMetadata = {
619+
id: data.id,
620+
name: `Fork | ${baseName}`,
621+
updatedAt: new Date(),
622+
isActive: false,
623+
isUnread: false,
624+
}
625+
queryClient.setQueryData<TaskMetadata[]>(taskKeys.list(workspaceId), [
626+
optimisticTask,
627+
...existing,
628+
])
629+
}
630+
},
612631
onSettled: () => {
613632
queryClient.invalidateQueries({ queryKey: taskKeys.list(workspaceId) })
614633
},

0 commit comments

Comments
 (0)