Open History to see the last commit
1import { createAttachmentEnvironmentAtoms } from "@t3tools/client-runtime/state/attachments";
2import type { AtomCommand } from "@t3tools/client-runtime/state/runtime";
3import {
4 AuthOrchestrationOperateScope,
5 EnvironmentAuthorizationError,
6 type EnvironmentId,
7} from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9import { msg } from "gt-react-native";
10import { m } from "../i18n/messages";
11
12import { connectionAtomRuntime } from "../connection/runtime";
13import { readEnvironmentScope } from "./session";
14
15function requireAttachmentWriteAccess<W extends { readonly environmentId: EnvironmentId }, A, E>(
16 command: AtomCommand<W, A, E>,
17): AtomCommand<W, A, E | EnvironmentAuthorizationError> {
18 return {
19 ...command,
20 run: (registry, input) =>
21 Effect.suspend((): Effect.Effect<A, E | EnvironmentAuthorizationError> =>
22 readEnvironmentScope(input.environmentId, AuthOrchestrationOperateScope)
23 ? command.run(registry, input)
24 : Effect.fail(
25 new EnvironmentAuthorizationError({
26 message: m(msg("This connection cannot change attachments.")),
27 requiredScope: AuthOrchestrationOperateScope,
28 }),
29 ),
30 ),
31 };
32}
33
34const commands = createAttachmentEnvironmentAtoms(connectionAtomRuntime);
35export const attachmentEnvironment = {
36 createUploadUrl: requireAttachmentWriteAccess(commands.createUploadUrl),
37 remove: requireAttachmentWriteAccess(commands.remove),
38};