Open History to see the last commit
1import type {
2 AtomCommand,
3 AtomCommandOptions,
4 AtomCommandResult,
5} from "@t3tools/client-runtime/state/runtime";
6import {
7 AuthOrchestrationOperateScope,
8 EnvironmentAuthorizationError,
9 type EnvironmentId,
10} from "@t3tools/contracts";
11import * as Cause from "effect/Cause";
12import { AsyncResult } from "effect/reactivity";
13
14import { readEnvironmentScope } from "./session";
15import { useAtomCommand } from "./use-atom-command";
16
17export const ORCHESTRATION_OPERATE_DENIED_MESSAGE =
18 "This connection cannot change threads or projects.";
19
20export function useOrchestrationCommand<W extends { readonly environmentId: EnvironmentId }, A, E>(
21 command: AtomCommand<W, A, E>,
22 options?: string | AtomCommandOptions,
23): (value: W) => Promise<AtomCommandResult<A, E | EnvironmentAuthorizationError>> {
24 const run = useAtomCommand(command, options);
25 return async (value: W): Promise<AtomCommandResult<A, E | EnvironmentAuthorizationError>> => {
26 if (!readEnvironmentScope(value.environmentId, AuthOrchestrationOperateScope)) {
27 return AsyncResult.failure(
28 Cause.fail(
29 new EnvironmentAuthorizationError({
30 requiredScope: AuthOrchestrationOperateScope,
31 message: ORCHESTRATION_OPERATE_DENIED_MESSAGE,
32 }),
33 ),
34 );
35 }
36 return run(value);
37 };
38}