Files
simstudioai--sim/apps/sim/hooks/use-inline-rename.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

110 lines
3.8 KiB
TypeScript

import { useCallback, useRef, useState } from 'react'
import { createLogger } from '@sim/logger'
const logger = createLogger('useInlineRename')
interface UseInlineRenameProps {
/**
* Persists the new name. Return the mutation promise (e.g. React Query's
* `mutateAsync(...)`) — NOT a fire-and-forget `mutate(...)` — so `isSaving`
* spans the in-flight request and a rejection can revive the edit session.
*/
onSave: (id: string, newName: string) => undefined | Promise<unknown>
}
/**
* Multiplexed (id-keyed) inline rename, used across resource rows (tables, files,
* knowledge) via {@link ResourceCell.editing}. This is the across-rows variant of
* the sidebar's single-item `useItemRename`; it mirrors that hook's save flow:
* `submitRename` is async and flips an `isSaving` flag during `await onSave`
* (the analogue of `useItemRename`'s `isRenaming`), while the `doneRef` guard keeps
* a blur racing an Enter/Escape commit a harmless no-op.
*
* TODO: the resource rename still intermittently unfocuses; the deeper re-render
* cause (parent remounting the editing cell) is tracked separately. This hook is
* aligned to the proven sidebar pattern as the first step.
*/
export function useInlineRename({ onSave }: UseInlineRenameProps) {
const onSaveRef = useRef(onSave)
onSaveRef.current = onSave
const originalNameRef = useRef('')
const doneRef = useRef(false)
const [editingId, setEditingId] = useState<string | null>(null)
const editingIdRef = useRef(editingId)
editingIdRef.current = editingId
const [editValue, setEditValue] = useState('')
const editValueRef = useRef(editValue)
editValueRef.current = editValue
const [isSaving, setIsSaving] = useState(false)
const startRename = useCallback((id: string, currentName: string) => {
doneRef.current = false
setEditingId(id)
/**
* Sync the ref eagerly (not just via the render-time assignment) so an
* in-flight save's `finally` that resolves before the next render still
* sees this id as the active edit and leaves the new session alone.
*/
editingIdRef.current = id
setEditValue(currentName)
originalNameRef.current = currentName
setIsSaving(false)
}, [])
const submitRename = useCallback(async () => {
if (doneRef.current) return
doneRef.current = true
const id = editingIdRef.current
const trimmed = editValueRef.current.trim()
if (!id || !trimmed || trimmed === originalNameRef.current) {
setEditingId(null)
return
}
setIsSaving(true)
try {
await onSaveRef.current(id, trimmed)
/**
* Only clear editing state if this submit still owns the edit session.
* Without the guard, a slow save for row A would tear down a rename of
* row B started while A's save was in flight. A superseded submit's
* cleanup is a no-op — `startRename` already reset `isSaving` for the
* new session, and the new session's own submit handles its lifecycle.
*/
if (editingIdRef.current === id) {
setEditingId(null)
}
} catch (error) {
logger.error('Failed to rename item', { error, id, newName: trimmed })
/**
* Mirror `useItemRename`'s failure path: stay in edit mode with the
* original name restored (no silent data loss, no unhandled rejection)
* and re-arm `doneRef` so the revived session can submit or cancel again.
*/
if (editingIdRef.current === id) {
setEditValue(originalNameRef.current)
doneRef.current = false
}
} finally {
if (editingIdRef.current === id) {
setIsSaving(false)
}
}
}, [])
const cancelRename = useCallback(() => {
doneRef.current = true
setEditingId(null)
}, [])
return {
editingId,
editValue,
isSaving,
setEditValue,
startRename,
submitRename,
cancelRename,
}
}