Files
simstudioai--sim/apps/sim/hooks/queries/utils/folder-tree.ts
T
wehub-resource-sync d25d482dc2
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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

112 lines
3.6 KiB
TypeScript

import type { WorkflowFolder } from '@/stores/folders/types'
/**
* Returns true when the folder or one of its ancestors is locked. Used to
* mirror server-side cascading folder lock policy on the client without an
* extra round-trip.
*/
export function isFolderOrAncestorLocked(
folderId: string | null | undefined,
folders: Record<string, WorkflowFolder>
): boolean {
const visited = new Set<string>()
let currentFolderId = folderId ?? null
while (currentFolderId) {
if (visited.has(currentFolderId)) return false
visited.add(currentFolderId)
const folder = folders[currentFolderId]
if (!folder) return false
if (folder.locked) return true
currentFolderId = folder.parentId
}
return false
}
/**
* Returns the human-readable path for a folder, e.g. `'Engineering / Backend'`.
* Returns `null` when the folder is at workspace root or unknown. Cycles or
* missing ancestors short-circuit by returning the segments resolved so far.
*/
export function getFolderPath(
folderId: string | null | undefined,
folders: Record<string, WorkflowFolder>,
separator = ' / '
): string | null {
if (!folderId) return null
const segments: string[] = []
const visited = new Set<string>()
let currentFolderId: string | null | undefined = folderId
while (currentFolderId) {
if (visited.has(currentFolderId)) break
visited.add(currentFolderId)
const folder: WorkflowFolder | undefined = folders[currentFolderId]
if (!folder) break
segments.unshift(folder.name)
currentFolderId = folder.parentId
}
return segments.length > 0 ? segments.join(separator) : null
}
/**
* Returns the closest locked ancestor folder for the given folderId, or `null`
* when neither the folder nor any of its ancestors are locked. Cycles or
* missing ancestors short-circuit and return `null` rather than looping.
*/
export function findLockedAncestorFolder(
folderId: string | null | undefined,
folders: Record<string, WorkflowFolder>
): WorkflowFolder | null {
if (!folderId) return null
const visited = new Set<string>()
let currentFolderId: string | null | undefined = folderId
while (currentFolderId) {
if (visited.has(currentFolderId)) return null
visited.add(currentFolderId)
const folder: WorkflowFolder | undefined = folders[currentFolderId]
if (!folder) return null
if (folder.locked) return folder
currentFolderId = folder.parentId
}
return null
}
/**
* Effective lock state for a workflow as visible to the client. Mirrors
* the server's `getWorkflowLockStatus(workflowId)` (in `@sim/platform-authz/workflow`)
* but reads from cached folder data instead of issuing DB walks. Treats an
* undefined workflow as unlocked so callers don't need to early-return.
*/
export function isWorkflowEffectivelyLocked(
workflow: { locked?: boolean | null; folderId?: string | null } | null | undefined,
folders: Record<string, WorkflowFolder>
): boolean {
if (!workflow) return false
if (workflow.locked) return true
return isFolderOrAncestorLocked(workflow.folderId, folders)
}
/**
* Effective lock state for a folder as visible to the client. Mirrors the
* server's `getFolderLockStatus(folderId)` (in `@sim/platform-authz/workflow`) but
* reads from cached folder data instead of issuing DB walks. Treats an
* undefined folder as unlocked so callers don't need to early-return.
*/
export function isFolderEffectivelyLocked(
folder: { locked?: boolean | null; parentId?: string | null } | null | undefined,
folders: Record<string, WorkflowFolder>
): boolean {
if (!folder) return false
if (folder.locked) return true
return isFolderOrAncestorLocked(folder.parentId, folders)
}