({ workspaceId, tableId }: RowMutationContext)
| 1183 | * Update a column (rename, type change, or constraint update). |
| 1184 | */ |
| 1185 | export function useUpdateColumn({ workspaceId, tableId }: RowMutationContext) { |
| 1186 | const queryClient = useQueryClient() |
| 1187 | |
| 1188 | return useMutation({ |
| 1189 | mutationFn: async ({ columnName, updates }: UpdateColumnParams) => { |
| 1190 | return requestJson(updateTableColumnContract, { |
| 1191 | params: { tableId }, |
| 1192 | body: { workspaceId, columnName, updates }, |
| 1193 | }) |
| 1194 | }, |
| 1195 | onMutate: async ({ columnName, updates }) => { |
| 1196 | await queryClient.cancelQueries({ queryKey: tableKeys.detail(tableId) }) |
| 1197 | const previousDetail = queryClient.getQueryData<TableDefinition>(tableKeys.detail(tableId)) |
| 1198 | if (previousDetail) { |
| 1199 | // `columnName` is the column id (first-party) or name (legacy); match |
| 1200 | // either. A rename is metadata-only and never moves id-keyed row data, |
| 1201 | // so we only patch the schema column's name — never `row.data` keys. |
| 1202 | // Stamp the current storage id so `getColumnId` stays stable as the |
| 1203 | // display name changes (mirrors the server's metadata-only rename). |
| 1204 | const lower = columnName.toLowerCase() |
| 1205 | const isRename = typeof (updates as { name?: string }).name === 'string' |
| 1206 | const nextColumns = previousDetail.schema.columns.map((c) => { |
| 1207 | if (getColumnId(c) !== columnName && c.name.toLowerCase() !== lower) return c |
| 1208 | const next = { ...c, ...updates } |
| 1209 | if (isRename && next.id === undefined) next.id = getColumnId(c) |
| 1210 | return next |
| 1211 | }) |
| 1212 | queryClient.setQueryData<TableDefinition>(tableKeys.detail(tableId), { |
| 1213 | ...previousDetail, |
| 1214 | schema: { ...previousDetail.schema, columns: nextColumns }, |
| 1215 | }) |
| 1216 | } |
| 1217 | |
| 1218 | return { previousDetail } |
| 1219 | }, |
| 1220 | onError: (error, _vars, context) => { |
| 1221 | if (context?.previousDetail) { |
| 1222 | queryClient.setQueryData(tableKeys.detail(tableId), context.previousDetail) |
| 1223 | } |
| 1224 | if (isValidationError(error)) return |
| 1225 | toast.error(error.message, { duration: 5000 }) |
| 1226 | }, |
| 1227 | onSettled: () => { |
| 1228 | invalidateTableSchema(queryClient, tableId) |
| 1229 | }, |
| 1230 | }) |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * Update a table's UI metadata (e.g. column widths). |
no test coverage detected