({ workspaceId, tableId }: RowMutationContext)
| 624 | } |
| 625 | |
| 626 | export function useCreateTableRow({ workspaceId, tableId }: RowMutationContext) { |
| 627 | const queryClient = useQueryClient() |
| 628 | const router = useRouter() |
| 629 | |
| 630 | return useMutation({ |
| 631 | mutationFn: async ( |
| 632 | variables: Omit<InsertTableRowBodyInput, 'workspaceId' | 'data'> & { |
| 633 | data: Record<string, unknown> |
| 634 | } |
| 635 | ) => { |
| 636 | return requestJson(createTableRowContract, { |
| 637 | params: { tableId }, |
| 638 | body: { |
| 639 | workspaceId, |
| 640 | data: variables.data as RowData, |
| 641 | position: variables.position, |
| 642 | afterRowId: variables.afterRowId, |
| 643 | beforeRowId: variables.beforeRowId, |
| 644 | }, |
| 645 | }) |
| 646 | }, |
| 647 | onSuccess: (response) => { |
| 648 | const row = response.data.row |
| 649 | if (!row) return |
| 650 | |
| 651 | const groups = |
| 652 | queryClient.getQueryData<TableDefinition>(tableKeys.detail(tableId))?.schema |
| 653 | .workflowGroups ?? [] |
| 654 | const stamped = withOptimisticAutoFireExec(groups, row) |
| 655 | reconcileCreatedRow(queryClient, tableId, stamped) |
| 656 | // Bump the run-state counter for any auto-fire groups stamped pending so |
| 657 | // the "X running" badge + gutter Stop show immediately (the row had no |
| 658 | // prior executions, so the stamped set is the full delta). |
| 659 | const stampedCount = countNewlyInFlight({}, stamped.executions ?? {}) |
| 660 | if (stampedCount > 0) bumpRunState(queryClient, tableId, { [row.id]: stampedCount }) |
| 661 | |
| 662 | // `reconcileCreatedRow` only patches the default-order view. Filtered / |
| 663 | // column-sorted rows queries can't be reconciled from that heuristic |
| 664 | // (membership, sort position, and `totalCount` are query-specific), so |
| 665 | // refetch them — active ones update now, inactive ones on next view. The |
| 666 | // default view stays optimistic, so the common case never refetches. |
| 667 | queryClient.invalidateQueries({ |
| 668 | queryKey: tableKeys.rowsRoot(tableId), |
| 669 | exact: false, |
| 670 | predicate: (query) => !isDefaultOrderRowsQuery(query.queryKey), |
| 671 | }) |
| 672 | }, |
| 673 | onError: (error) => |
| 674 | notifyRowWriteError(error, () => router.push(buildUpgradeHref(workspaceId, 'tables'))), |
| 675 | onSettled: () => { |
| 676 | // `reconcileCreatedRow` (onSuccess) is the source of truth for the rows |
| 677 | // cache + its `totalCount`; only refresh the count surfaces here so a late |
| 678 | // offset refetch can't clobber freshly-inserted rows (insert-flicker). |
| 679 | invalidateRowCountSurfaces(queryClient, tableId) |
| 680 | }, |
| 681 | }) |
| 682 | } |
| 683 |
no test coverage detected