@@ -85,6 +85,7 @@ export const tableKeys = {
8585 rowsRoot : ( tableId : string ) => [ ...tableKeys . detail ( tableId ) , 'rows' ] as const ,
8686 infiniteRows : ( tableId : string , paramsKey : string ) =>
8787 [ ...tableKeys . rowsRoot ( tableId ) , 'infinite' , paramsKey ] as const ,
88+ rowWrites : ( tableId : string ) => [ ...tableKeys . rowsRoot ( tableId ) , 'write' ] as const ,
8889}
8990
9091type TableRowsParams = Omit < TableRowsQueryInput , 'filter' | 'sort' > &
@@ -543,14 +544,15 @@ export function useUpdateTableRow({ workspaceId, tableId }: RowMutationContext)
543544 const queryClient = useQueryClient ( )
544545
545546 return useMutation ( {
547+ mutationKey : tableKeys . rowWrites ( tableId ) ,
546548 mutationFn : async ( { rowId, data } : UpdateTableRowParams ) => {
547549 return requestJson ( updateTableRowContract , {
548550 params : { tableId, rowId } ,
549551 body : { workspaceId, data : data as RowData } ,
550552 } )
551553 } ,
552- onMutate : ( { rowId, data } ) => {
553- void queryClient . cancelQueries ( { queryKey : tableKeys . rowsRoot ( tableId ) } )
554+ onMutate : async ( { rowId, data } ) => {
555+ await queryClient . cancelQueries ( { queryKey : tableKeys . rowsRoot ( tableId ) } )
554556
555557 const previousQueries = queryClient . getQueriesData < InfiniteData < TableRowsResponse , number > > ( {
556558 queryKey : tableKeys . rowsRoot ( tableId ) ,
@@ -573,18 +575,31 @@ export function useUpdateTableRow({ workspaceId, tableId }: RowMutationContext)
573575
574576 return { previousQueries }
575577 } ,
578+ onSuccess : ( response , { rowId } ) => {
579+ const serverRow = response . data . row
580+ patchCachedRows ( queryClient , tableId , ( row ) => {
581+ if ( row . id !== rowId ) return row
582+ return {
583+ ...row ,
584+ data : serverRow . data ,
585+ position : serverRow . position ,
586+ createdAt : serverRow . createdAt ,
587+ updatedAt : serverRow . updatedAt ,
588+ }
589+ } )
590+ } ,
576591 onError : ( error , _vars , context ) => {
577592 if ( context ?. previousQueries ) {
578593 for ( const [ queryKey , data ] of context . previousQueries ) {
579594 queryClient . setQueryData ( queryKey , data )
580595 }
581596 }
597+ if ( queryClient . isMutating ( { mutationKey : tableKeys . rowWrites ( tableId ) } ) === 1 ) {
598+ invalidateRowData ( queryClient , tableId )
599+ }
582600 if ( isValidationError ( error ) ) return
583601 toast . error ( error . message , { duration : 5000 } )
584602 } ,
585- onSettled : ( ) => {
586- invalidateRowData ( queryClient , tableId )
587- } ,
588603 } )
589604}
590605
@@ -599,6 +614,7 @@ export function useBatchUpdateTableRows({ workspaceId, tableId }: RowMutationCon
599614 const queryClient = useQueryClient ( )
600615
601616 return useMutation ( {
617+ mutationKey : tableKeys . rowWrites ( tableId ) ,
602618 mutationFn : async ( { updates } : BatchUpdateTableRowsParams ) => {
603619 return requestJson ( batchUpdateTableRowsContract , {
604620 params : { tableId } ,
@@ -608,8 +624,8 @@ export function useBatchUpdateTableRows({ workspaceId, tableId }: RowMutationCon
608624 } ,
609625 } )
610626 } ,
611- onMutate : ( { updates } ) => {
612- void queryClient . cancelQueries ( { queryKey : tableKeys . rowsRoot ( tableId ) } )
627+ onMutate : async ( { updates } ) => {
628+ await queryClient . cancelQueries ( { queryKey : tableKeys . rowsRoot ( tableId ) } )
613629
614630 const previousQueries = queryClient . getQueriesData < InfiniteData < TableRowsResponse , number > > ( {
615631 queryKey : tableKeys . rowsRoot ( tableId ) ,
@@ -644,7 +660,9 @@ export function useBatchUpdateTableRows({ workspaceId, tableId }: RowMutationCon
644660 toast . error ( error . message , { duration : 5000 } )
645661 } ,
646662 onSettled : ( ) => {
647- invalidateRowData ( queryClient , tableId )
663+ if ( queryClient . isMutating ( { mutationKey : tableKeys . rowWrites ( tableId ) } ) === 1 ) {
664+ invalidateRowData ( queryClient , tableId )
665+ }
648666 } ,
649667 } )
650668}
0 commit comments