Files
simstudioai--sim/apps/sim/lib/logs/folder-expansion.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

54 lines
1.8 KiB
TypeScript

import { db } from '@sim/db'
import { workflowFolder } from '@sim/db/schema'
import { and, eq, isNull } from 'drizzle-orm'
/**
* Expands a CSV of selected folder IDs to include every descendant folder in the
* workspace, so that filtering by a parent folder also matches workflows that
* live in nested subfolders.
*
* Returns the original CSV when there are no descendants (or when the input is
* empty / undefined). Unknown IDs are preserved so the caller's `inArray` check
* behaves the same as today (matches nothing).
*
* Server-only: pulls in the database client. Keep separate from `filters.ts`
* (imported by client hooks) to avoid leaking postgres into the browser bundle.
*/
export async function expandFolderIdsWithDescendants(
workspaceId: string,
folderIdsCsv: string | undefined
): Promise<string | undefined> {
if (!folderIdsCsv) return folderIdsCsv
const seedIds = folderIdsCsv.split(',').filter(Boolean)
if (seedIds.length === 0) return folderIdsCsv
const rows = await db
.select({ id: workflowFolder.id, parentId: workflowFolder.parentId })
.from(workflowFolder)
.where(and(eq(workflowFolder.workspaceId, workspaceId), isNull(workflowFolder.archivedAt)))
const childrenByParent = new Map<string, string[]>()
for (const row of rows) {
if (!row.parentId) continue
const list = childrenByParent.get(row.parentId)
if (list) list.push(row.id)
else childrenByParent.set(row.parentId, [row.id])
}
const expanded = new Set<string>(seedIds)
const queue = [...seedIds]
while (queue.length > 0) {
const current = queue.pop() as string
const children = childrenByParent.get(current)
if (!children) continue
for (const childId of children) {
if (!expanded.has(childId)) {
expanded.add(childId)
queue.push(childId)
}
}
}
return Array.from(expanded).join(',')
}