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";
13import { msg } from "gt-react";
14
15import { m } from "../i18n/messages";
16
17import { readEnvironmentScope } from "./session";
18import { useAtomCommand } from "./use-atom-command";
19
20const ORCHESTRATION_OPERATE_DENIED_MESSAGE = msg(
21 "This connection cannot change threads or projects.",
22);
23
24export function useOrchestrationCommand<W extends { readonly environmentId: EnvironmentId }, A, E>(
25 command: AtomCommand<W, A, E>,
26 options?: string | AtomCommandOptions,
27): (value: W) => Promise<AtomCommandResult<A, E | EnvironmentAuthorizationError>> {
28 const run = useAtomCommand(command, options);
29 return async (value: W): Promise<AtomCommandResult<A, E | EnvironmentAuthorizationError>> => {
30 if (!readEnvironmentScope(value.environmentId, AuthOrchestrationOperateScope)) {
31 return AsyncResult.failure(
32 Cause.fail(
33 new EnvironmentAuthorizationError({
34 requiredScope: AuthOrchestrationOperateScope,
35 message: m(ORCHESTRATION_OPERATE_DENIED_MESSAGE),
36 }),
37 ),
38 );
39 }
40 return run(value);
41 };
42}