chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* Zustand store for table undo/redo stacks.
|
||||
* Ephemeral — no persistence. Stacks are keyed by tableId.
|
||||
*/
|
||||
|
||||
import { generateShortId } from '@sim/utils/id'
|
||||
import { create } from 'zustand'
|
||||
import { devtools } from 'zustand/middleware'
|
||||
import type { TableUndoAction, TableUndoStacks, TableUndoState, UndoEntry } from './types'
|
||||
|
||||
const STACK_CAPACITY = 100
|
||||
const EMPTY_STACKS: TableUndoStacks = { undo: [], redo: [] }
|
||||
|
||||
let undoRedoInProgress = false
|
||||
|
||||
function patchRowIdInEntry(entry: UndoEntry, oldRowId: string, newRowId: string): UndoEntry {
|
||||
const { action } = entry
|
||||
switch (action.type) {
|
||||
case 'update-cell':
|
||||
if (action.rowId === oldRowId) {
|
||||
return { ...entry, action: { ...action, rowId: newRowId } }
|
||||
}
|
||||
break
|
||||
case 'clear-cells': {
|
||||
const hasMatch = action.cells.some((c) => c.rowId === oldRowId)
|
||||
if (hasMatch) {
|
||||
const patched = action.cells.map((c) =>
|
||||
c.rowId === oldRowId ? { ...c, rowId: newRowId } : c
|
||||
)
|
||||
return { ...entry, action: { ...action, cells: patched } }
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'update-cells': {
|
||||
const hasMatch = action.cells.some((c) => c.rowId === oldRowId)
|
||||
if (hasMatch) {
|
||||
const patched = action.cells.map((c) =>
|
||||
c.rowId === oldRowId ? { ...c, rowId: newRowId } : c
|
||||
)
|
||||
return { ...entry, action: { ...action, cells: patched } }
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'create-row':
|
||||
if (action.rowId === oldRowId) {
|
||||
return { ...entry, action: { ...action, rowId: newRowId } }
|
||||
}
|
||||
break
|
||||
case 'create-rows': {
|
||||
const hasMatch = action.rows.some((r) => r.rowId === oldRowId)
|
||||
if (hasMatch) {
|
||||
const patched = action.rows.map((r) =>
|
||||
r.rowId === oldRowId ? { ...r, rowId: newRowId } : r
|
||||
)
|
||||
return { ...entry, action: { ...action, rows: patched } }
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'delete-rows': {
|
||||
const hasMatch = action.rows.some((r) => r.rowId === oldRowId)
|
||||
if (hasMatch) {
|
||||
const patched = action.rows.map((r) =>
|
||||
r.rowId === oldRowId ? { ...r, rowId: newRowId } : r
|
||||
)
|
||||
return { ...entry, action: { ...action, rows: patched } }
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'delete-column': {
|
||||
const hasMatch = action.cellData.some((c) => c.rowId === oldRowId)
|
||||
if (hasMatch) {
|
||||
const patched = action.cellData.map((c) =>
|
||||
c.rowId === oldRowId ? { ...c, rowId: newRowId } : c
|
||||
)
|
||||
return { ...entry, action: { ...action, cellData: patched } }
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a function without recording undo entries. Supports async functions —
|
||||
* `undoRedoInProgress` stays true until the returned Promise settles, so
|
||||
* mutations inside `executeAction` don't accidentally push new undo entries.
|
||||
*/
|
||||
export async function runWithoutRecording<T>(fn: () => T | Promise<T>): Promise<T> {
|
||||
undoRedoInProgress = true
|
||||
try {
|
||||
return await fn()
|
||||
} finally {
|
||||
undoRedoInProgress = false
|
||||
}
|
||||
}
|
||||
|
||||
export const useTableUndoStore = create<TableUndoState>()(
|
||||
devtools(
|
||||
(set, get) => ({
|
||||
stacks: {},
|
||||
|
||||
push: (tableId: string, action: TableUndoAction) => {
|
||||
if (undoRedoInProgress) return
|
||||
|
||||
const entry: UndoEntry = { id: generateShortId(), action, timestamp: Date.now() }
|
||||
|
||||
set((state) => {
|
||||
const current = state.stacks[tableId] ?? EMPTY_STACKS
|
||||
const undoStack = [entry, ...current.undo].slice(0, STACK_CAPACITY)
|
||||
return {
|
||||
stacks: {
|
||||
...state.stacks,
|
||||
[tableId]: { undo: undoStack, redo: [] },
|
||||
},
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
popUndo: (tableId: string) => {
|
||||
const current = get().stacks[tableId] ?? EMPTY_STACKS
|
||||
if (current.undo.length === 0) return null
|
||||
|
||||
const [entry, ...rest] = current.undo
|
||||
set((state) => ({
|
||||
stacks: {
|
||||
...state.stacks,
|
||||
[tableId]: {
|
||||
undo: rest,
|
||||
redo: [entry, ...current.redo],
|
||||
},
|
||||
},
|
||||
}))
|
||||
return entry
|
||||
},
|
||||
|
||||
popRedo: (tableId: string) => {
|
||||
const current = get().stacks[tableId] ?? EMPTY_STACKS
|
||||
if (current.redo.length === 0) return null
|
||||
|
||||
const [entry, ...rest] = current.redo
|
||||
set((state) => ({
|
||||
stacks: {
|
||||
...state.stacks,
|
||||
[tableId]: {
|
||||
undo: [entry, ...current.undo],
|
||||
redo: rest,
|
||||
},
|
||||
},
|
||||
}))
|
||||
return entry
|
||||
},
|
||||
|
||||
patchRedoRowId: (tableId: string, oldRowId: string, newRowId: string) => {
|
||||
set((state) => {
|
||||
const stacks = state.stacks[tableId]
|
||||
if (!stacks) return state
|
||||
const patchedRedo = stacks.redo.map((entry) =>
|
||||
patchRowIdInEntry(entry, oldRowId, newRowId)
|
||||
)
|
||||
return {
|
||||
stacks: {
|
||||
...state.stacks,
|
||||
[tableId]: { ...stacks, redo: patchedRedo },
|
||||
},
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
patchUndoRowId: (tableId: string, oldRowId: string, newRowId: string) => {
|
||||
set((state) => {
|
||||
const stacks = state.stacks[tableId]
|
||||
if (!stacks) return state
|
||||
const patchedUndo = stacks.undo.map((entry) =>
|
||||
patchRowIdInEntry(entry, oldRowId, newRowId)
|
||||
)
|
||||
return {
|
||||
stacks: {
|
||||
...state.stacks,
|
||||
[tableId]: { ...stacks, undo: patchedUndo },
|
||||
},
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
clear: (tableId: string) => {
|
||||
set((state) => {
|
||||
const { [tableId]: _, ...rest } = state.stacks
|
||||
return { stacks: rest }
|
||||
})
|
||||
},
|
||||
}),
|
||||
{ name: 'table-undo-store' }
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user