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
108 lines
3.7 KiB
TypeScript
108 lines
3.7 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { requestJson } from '@/lib/api/client/request'
|
|
import {
|
|
type CustomBlock,
|
|
type CustomBlockUsageCounts,
|
|
deleteCustomBlockContract,
|
|
getCustomBlockUsageCountsContract,
|
|
listCustomBlocksContract,
|
|
type PublishCustomBlockBody,
|
|
publishCustomBlockContract,
|
|
type UpdateCustomBlockBody,
|
|
updateCustomBlockContract,
|
|
} from '@/lib/api/contracts/custom-blocks'
|
|
|
|
export const CUSTOM_BLOCK_LIST_STALE_TIME = 60 * 1000
|
|
/** Short — the usage count is a pre-delete safety check and must stay fresh. */
|
|
export const CUSTOM_BLOCK_USAGES_STALE_TIME = 30 * 1000
|
|
|
|
export const customBlockKeys = {
|
|
all: ['custom-blocks'] as const,
|
|
lists: () => [...customBlockKeys.all, 'list'] as const,
|
|
list: (workspaceId?: string) => [...customBlockKeys.lists(), workspaceId ?? ''] as const,
|
|
usages: (id?: string) => [...customBlockKeys.all, 'usages', id ?? ''] as const,
|
|
}
|
|
|
|
interface CustomBlocksResult {
|
|
enabled: boolean
|
|
customBlocks: CustomBlock[]
|
|
}
|
|
|
|
async function fetchCustomBlocks(
|
|
workspaceId: string,
|
|
signal?: AbortSignal
|
|
): Promise<CustomBlocksResult> {
|
|
return requestJson(listCustomBlocksContract, { query: { workspaceId }, signal })
|
|
}
|
|
|
|
function useCustomBlocksQuery<T>(
|
|
workspaceId: string | undefined,
|
|
select: (r: CustomBlocksResult) => T
|
|
) {
|
|
return useQuery({
|
|
queryKey: customBlockKeys.list(workspaceId),
|
|
queryFn: ({ signal }) => fetchCustomBlocks(workspaceId as string, signal),
|
|
enabled: Boolean(workspaceId),
|
|
staleTime: CUSTOM_BLOCK_LIST_STALE_TIME,
|
|
select,
|
|
})
|
|
}
|
|
|
|
/** Org custom blocks (with live-derived input fields) available in this workspace. */
|
|
export function useCustomBlocks(workspaceId?: string) {
|
|
return useCustomBlocksQuery(workspaceId, (r) => r.customBlocks)
|
|
}
|
|
|
|
/** Whether this workspace may publish/use custom blocks (feature flag + enterprise plan). */
|
|
export function useCanPublishCustomBlock(workspaceId?: string) {
|
|
return useCustomBlocksQuery(workspaceId, (r) => r.enabled)
|
|
}
|
|
|
|
function fetchCustomBlockUsageCounts(
|
|
id: string,
|
|
signal?: AbortSignal
|
|
): Promise<CustomBlockUsageCounts> {
|
|
return requestJson(getCustomBlockUsageCountsContract, { params: { id }, signal })
|
|
}
|
|
|
|
/** How many workflows across the org place this block (live editor state and/or active deployment). */
|
|
export function useCustomBlockUsageCounts(blockId?: string, options?: { enabled?: boolean }) {
|
|
return useQuery({
|
|
queryKey: customBlockKeys.usages(blockId),
|
|
queryFn: ({ signal }) => fetchCustomBlockUsageCounts(blockId as string, signal),
|
|
enabled: Boolean(blockId) && (options?.enabled ?? true),
|
|
staleTime: CUSTOM_BLOCK_USAGES_STALE_TIME,
|
|
})
|
|
}
|
|
|
|
export function usePublishCustomBlock(workspaceId?: string) {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (body: PublishCustomBlockBody) => requestJson(publishCustomBlockContract, { body }),
|
|
onSettled: () => {
|
|
queryClient.invalidateQueries({ queryKey: customBlockKeys.lists() })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useUpdateCustomBlock(workspaceId?: string) {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ id, ...body }: UpdateCustomBlockBody & { id: string }) =>
|
|
requestJson(updateCustomBlockContract, { params: { id }, body }),
|
|
onSettled: () => {
|
|
queryClient.invalidateQueries({ queryKey: customBlockKeys.lists() })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteCustomBlock(workspaceId?: string) {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => requestJson(deleteCustomBlockContract, { params: { id } }),
|
|
onSettled: () => {
|
|
queryClient.invalidateQueries({ queryKey: customBlockKeys.lists() })
|
|
},
|
|
})
|
|
}
|