-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTeamPolicies.tsx
233 lines (211 loc) · 11.4 KB
/
TeamPolicies.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/
import { OrganizationSettings } from "@gitpod/public-api/lib/gitpod/v1/organization_pb";
import { FormEvent, useCallback, useEffect, useState } from "react";
import Alert from "../components/Alert";
import { CheckboxInputField } from "../components/forms/CheckboxInputField";
import { Heading2, Heading3, Subheading } from "../components/typography/headings";
import { useIsOwner } from "../data/organizations/members-query";
import { useOrgSettingsQuery } from "../data/organizations/org-settings-query";
import { useCurrentOrg } from "../data/organizations/orgs-query";
import { useUpdateOrgSettingsMutation } from "../data/organizations/update-org-settings-mutation";
import { OrgSettingsPage } from "./OrgSettingsPage";
import { ConfigurationSettingsField } from "../repositories/detail/ConfigurationSettingsField";
import { useDocumentTitle } from "../hooks/use-document-title";
import { useOrgBillingMode } from "../data/billing-mode/org-billing-mode-query";
import { converter } from "../service/public-api";
import { useToast } from "../components/toasts/Toasts";
import type { PlainMessage } from "@bufbuild/protobuf";
import { WorkspaceTimeoutDuration } from "@gitpod/gitpod-protocol";
import { Link } from "react-router-dom";
import { InputField } from "../components/forms/InputField";
import { TextInput } from "../components/forms/TextInputField";
import { LoadingButton } from "@podkit/buttons/LoadingButton";
import { MaxParallelWorkspaces } from "./policies/MaxParallelWorkspaces";
import { WorkspaceClassesEnterpriseCallout } from "./policies/WorkspaceClassesEnterpriseCallout";
import { EditorOptions } from "./policies/EditorOptions";
import { RolePermissionsRestrictions } from "./policies/RoleRestrictions";
import { OrgWorkspaceClassesOptions } from "./policies/OrgWorkspaceClassesOptions";
import { useDefaultOrgTimeoutQuery } from "../data/organizations/default-org-timeout-query";
import { useInstallationConfiguration } from "../data/installation/installation-config-query";
export default function TeamPoliciesPage() {
useDocumentTitle("Organization Settings - Policies");
const { toast } = useToast();
const org = useCurrentOrg().data;
const isOwner = useIsOwner();
const { data: settings, isLoading } = useOrgSettingsQuery();
const updateTeamSettings = useUpdateOrgSettingsMutation();
const { data: installationConfig } = useInstallationConfiguration();
const isDedicatedInstallation = installationConfig?.isDedicatedInstallation ?? true; // we bias towards being on dedicated so the callout doesn't show when we're not sure
const billingMode = useOrgBillingMode();
const [workspaceTimeout, setWorkspaceTimeout] = useState<string | undefined>(undefined);
const [allowTimeoutChangeByMembers, setAllowTimeoutChangeByMembers] = useState<boolean | undefined>(undefined);
const [workspaceTimeoutSettingError, setWorkspaceTimeoutSettingError] = useState<string | undefined>(undefined);
const defaultOrgTimeout = useDefaultOrgTimeoutQuery();
const handleUpdateTeamSettings = useCallback(
async (newSettings: Partial<PlainMessage<OrganizationSettings>>, options?: { throwMutateError?: boolean }) => {
if (!org?.id) {
throw new Error("no organization selected");
}
if (!isOwner) {
throw new Error("no organization settings change permission");
}
try {
await updateTeamSettings.mutateAsync(newSettings);
setWorkspaceTimeoutSettingError(undefined);
toast("Organization settings updated");
} catch (error) {
if (options?.throwMutateError) {
throw error;
}
toast(`Failed to update organization settings: ${error.message}`);
console.error(error);
}
},
[updateTeamSettings, org?.id, isOwner, toast],
);
useEffect(() => {
setWorkspaceTimeout(
settings?.timeoutSettings?.inactivity
? converter.toDurationString(settings.timeoutSettings.inactivity)
: undefined,
);
setAllowTimeoutChangeByMembers(!settings?.timeoutSettings?.denyUserTimeouts);
}, [settings?.timeoutSettings]);
const handleUpdateOrganizationTimeoutSettings = useCallback(
(e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
if (workspaceTimeout) {
WorkspaceTimeoutDuration.validate(workspaceTimeout);
}
} catch (error) {
setWorkspaceTimeoutSettingError(error.message);
return;
}
// Nothing has changed
if (workspaceTimeout === undefined && allowTimeoutChangeByMembers === undefined) {
return;
}
handleUpdateTeamSettings({
timeoutSettings: {
inactivity: converter.toDurationOpt(workspaceTimeout),
denyUserTimeouts: !allowTimeoutChangeByMembers,
},
});
},
[workspaceTimeout, allowTimeoutChangeByMembers, handleUpdateTeamSettings],
);
const isPaidOrDedicated =
billingMode.data?.mode === "none" || (billingMode.data?.mode === "usage-based" && billingMode.data?.paid);
return (
<>
<OrgSettingsPage>
<div className="space-y-8">
<div>
<Heading2>Policies</Heading2>
<Subheading>
Restrict workspace classes, editors and sharing across your organization.
</Subheading>
</div>
<ConfigurationSettingsField>
<Heading3>Collaboration and sharing</Heading3>
{updateTeamSettings.isError && (
<Alert type="error" closable={true} className="mb-2 max-w-xl rounded-md">
<span>Failed to update organization settings: </span>
<span>{updateTeamSettings.error.message || "unknown error"}</span>
</Alert>
)}
<CheckboxInputField
label="Workspace Sharing"
hint="Allow workspaces created within an Organization to share the workspace with any authenticated user."
checked={!settings?.workspaceSharingDisabled}
onChange={(checked) => handleUpdateTeamSettings({ workspaceSharingDisabled: !checked })}
disabled={isLoading || !isOwner}
/>
</ConfigurationSettingsField>
<ConfigurationSettingsField>
<Heading3>Workspace timeouts</Heading3>
{!isPaidOrDedicated && (
<Alert type="info" className="my-3">
Setting Workspace timeouts is only available for organizations on a paid plan. Visit{" "}
<Link to={"/billing"} className="gp-link">
Billing
</Link>{" "}
to upgrade your plan.
</Alert>
)}
<form onSubmit={handleUpdateOrganizationTimeoutSettings}>
<InputField
label="Default workspace timeout"
error={workspaceTimeoutSettingError}
hint={
<span>
Use minutes or hours, like <span className="font-semibold">30m</span> or{" "}
<span className="font-semibold">2h</span>. If not set, your organization's
default of <span className="font-semibold">{defaultOrgTimeout}</span> will be
used.
</span>
}
>
<TextInput
value={workspaceTimeout ?? ""}
placeholder="e.g. 30m"
onChange={setWorkspaceTimeout}
disabled={updateTeamSettings.isLoading || !isOwner || !isPaidOrDedicated}
/>
</InputField>
<CheckboxInputField
label="Allow members to change workspace timeouts"
hint="Allow users to change the timeout duration for their workspaces as well as setting a default one in their user settings."
checked={!!allowTimeoutChangeByMembers}
containerClassName="my-4"
onChange={setAllowTimeoutChangeByMembers}
disabled={updateTeamSettings.isLoading || !isOwner || !isPaidOrDedicated}
/>
<LoadingButton
type="submit"
loading={updateTeamSettings.isLoading}
disabled={
!isOwner ||
!isPaidOrDedicated ||
(workspaceTimeout ===
converter.toDurationStringOpt(settings?.timeoutSettings?.inactivity) &&
allowTimeoutChangeByMembers === !settings?.timeoutSettings?.denyUserTimeouts)
}
>
Save
</LoadingButton>
</form>
</ConfigurationSettingsField>
<MaxParallelWorkspaces
isOwner={isOwner}
isLoading={updateTeamSettings.isLoading}
settings={settings}
handleUpdateTeamSettings={handleUpdateTeamSettings}
isPaidOrDedicated={isPaidOrDedicated}
/>
<OrgWorkspaceClassesOptions
isOwner={isOwner}
settings={settings}
handleUpdateTeamSettings={handleUpdateTeamSettings}
/>
{!isDedicatedInstallation && <WorkspaceClassesEnterpriseCallout />}
<EditorOptions
isOwner={isOwner}
settings={settings}
handleUpdateTeamSettings={handleUpdateTeamSettings}
/>
<RolePermissionsRestrictions
settings={settings}
isOwner={isOwner}
handleUpdateTeamSettings={handleUpdateTeamSettings}
/>
</div>
</OrgSettingsPage>
</>
);
}