@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 140 lines 4.7 KB
Raw
1 import {
2 isManagedSettingPath,
3 isNullableProjectSettingsOverride,
4 PROJECT_SCOPED_SERVER_SETTING_KEYS,
5 type EnvironmentId,
6 type ProjectId,
7 type ProjectScopedServerSettingKey,
8 type ServerSettings,
9 type ServerSettingsPatch,
10 } from "@t3tools/contracts";
11 import {
12 clearProjectSettingsOverrides,
13 resolveProjectSettings,
14 } from "@t3tools/shared/projectSettings";
15
16 import { msg } from "gt-react-native";
17
18 import { m, type MessageResolver } from "../../i18n/messages";
19 import type { SettingsTarget } from "./settings-environment-filter";
20
21 export interface ScopedMobileSettingsTarget {
22 readonly environment: SettingsTarget;
23 readonly projectId: ProjectId | null;
24 readonly settings: ServerSettings;
25 readonly sources: ReturnType<typeof resolveProjectSettings>["sources"];
26 }
27
28 /**
29 * "Managed by <organization>." when any target's policy locks `path`: the server refuses the
30 * whole write then, so the control is managed for the selection.
31 */
32 export function managedSettingReason(
33 targets: readonly ScopedMobileSettingsTarget[],
34 paths: readonly string[],
35 resolve: MessageResolver = m,
36 ): string | undefined {
37 const policy = targets
38 .map((target) => target.environment.serverConfig.managedPolicy)
39 .find((candidate) => paths.some((path) => isManagedSettingPath(candidate, path)));
40 return policy === undefined
41 ? undefined
42 : resolve(msg("Managed by {organization}.", { organization: policy.organization }));
43 }
44
45 export function resolveMobileSettingsTargets(
46 environments: readonly SettingsTarget[],
47 members: readonly { readonly environmentId: EnvironmentId; readonly id: ProjectId }[] | null,
48 ): readonly ScopedMobileSettingsTarget[] {
49 if (members === null) {
50 return environments.map((environment) => ({
51 environment,
52 projectId: null,
53 ...resolveProjectSettings(environment.serverConfig.settings, null),
54 }));
55 }
56 const byId = new Map(environments.map((environment) => [environment.environmentId, environment]));
57 return members.flatMap((member) => {
58 const environment = byId.get(member.environmentId);
59 if (!environment) return [];
60 return [
61 {
62 environment,
63 projectId: member.id,
64 ...resolveProjectSettings(environment.serverConfig.settings, member.id),
65 },
66 ];
67 });
68 }
69
70 export function planMobileScopedSettingsPatch(
71 targets: readonly ScopedMobileSettingsTarget[],
72 projectSelected: boolean,
73 patch: ServerSettingsPatch,
74 ) {
75 if (!projectSelected) {
76 return targets.map((target) => ({ environmentId: target.environment.environmentId, patch }));
77 }
78 const keys = Object.keys(patch);
79 if (
80 keys.some(
81 (key) => !PROJECT_SCOPED_SERVER_SETTING_KEYS.includes(key as ProjectScopedServerSettingKey),
82 )
83 )
84 return [];
85 const writes = new Map<EnvironmentId, Record<string, unknown>>();
86 for (const target of targets) {
87 if (target.projectId === null) continue;
88 const current =
89 target.environment.serverConfig.settings.projectSettingsOverrides[target.projectId] ?? {};
90 const next: Record<string, unknown> = { ...current };
91 for (const [key, value] of Object.entries(patch)) {
92 if (
93 value === null &&
94 !isNullableProjectSettingsOverride(key as ProjectScopedServerSettingKey)
95 ) {
96 delete next[key];
97 } else {
98 next[key] = value;
99 }
100 }
101 const overrides = writes.get(target.environment.environmentId) ?? {};
102 overrides[target.projectId] = next;
103 writes.set(target.environment.environmentId, overrides);
104 }
105 return [...writes].map(([environmentId, projectSettingsOverrides]) => ({
106 environmentId,
107 patch: { projectSettingsOverrides } as ServerSettingsPatch,
108 }));
109 }
110
111 export function uniformMobileSetting<K extends keyof ServerSettings>(
112 targets: readonly Pick<ScopedMobileSettingsTarget, "settings">[],
113 key: K,
114 ): ServerSettings[K] | null {
115 const reference = targets[0];
116 if (!reference) return null;
117 const value = reference.settings[key];
118 return targets.every((target) => target.settings[key] === value) ? value : null;
119 }
120
121 export function planMobileScopedSettingsClear(
122 targets: readonly ScopedMobileSettingsTarget[],
123 keys: readonly ProjectScopedServerSettingKey[],
124 ) {
125 const writes = new Map<EnvironmentId, Record<string, unknown>>();
126 for (const target of targets) {
127 if (target.projectId === null) continue;
128 const overrides = writes.get(target.environment.environmentId) ?? {};
129 overrides[target.projectId] = clearProjectSettingsOverrides(
130 target.environment.serverConfig.settings,
131 target.projectId,
132 keys,
133 );
134 writes.set(target.environment.environmentId, overrides);
135 }
136 return [...writes].map(([environmentId, projectSettingsOverrides]) => ({
137 environmentId,
138 patch: { projectSettingsOverrides } as ServerSettingsPatch,
139 }));
140 }