Files
simstudioai--sim/apps/sim/blocks/custom/client-overlay.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

62 lines
2.1 KiB
TypeScript

'use client'
import { useSyncExternalStore } from 'react'
import { registerBlockOverlayResolver } from '@/blocks/custom/overlay'
import type { BlockConfig } from '@/blocks/types'
/**
* Client-side custom-block overlay: a mutable Map, hydrated from
* `useCustomBlocks` by `CustomBlocksProvider`, that the `@/blocks/registry`
* accessors fall back to. Scoped to the active workspace's org; re-hydrated on
* workspace switch.
*
* Because many consumers snapshot `getAllBlocks()` (the cmd+K search, the Access
* Control block list), the overlay is also an external store: `version` bumps on
* every hydrate and listeners are notified, so those consumers can re-read via
* {@link useCustomBlockOverlayVersion} instead of going stale until a refresh.
*/
let map = new Map<string, BlockConfig>()
let version = 0
const listeners = new Set<() => void>()
registerBlockOverlayResolver({
get: (type) => map.get(type),
all: () => [...map.values()],
})
/**
* Bump the overlay version and notify subscribers that block-registry-derived
* data changed. Shared signal for BOTH custom-block hydration and the
* block-visibility hydrate path — subscribers treat the version as an opaque
* "re-read `getAllBlocks()`" token, never as "custom blocks specifically
* changed".
*/
export function notifyBlockOverlayChanged(): void {
version += 1
for (const listener of listeners) listener()
}
/** Replace the in-scope custom blocks and notify subscribers. */
export function hydrateClientCustomBlocks(configs: BlockConfig[]): void {
map = new Map(configs.map((config) => [config.type, config]))
notifyBlockOverlayChanged()
}
function subscribe(listener: () => void): () => void {
listeners.add(listener)
return () => listeners.delete(listener)
}
/**
* Subscribe a component to overlay changes. Returns a monotonic version that
* changes on every hydrate — include it in a `useMemo`/`useEffect` dep list to
* recompute anything derived from `getAllBlocks()` when custom blocks load.
*/
export function useCustomBlockOverlayVersion(): number {
return useSyncExternalStore(
subscribe,
() => version,
() => 0
)
}