Skip to content

Commit ed45177

Browse files
committed
better UX (valiate on save)
1 parent bd3e917 commit ed45177

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

src/settings/DeveloperSettingsTab.tsx

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
InlineField,
2929
Label,
3030
RadioControl,
31+
Text,
3132
} from "@vector-im/compound-web";
3233

3334
import { FieldRow, InputField } from "../input/Input";
@@ -41,11 +42,11 @@ import {
4142
matrixRTCMode as matrixRTCModeSetting,
4243
customLivekitUrl as customLivekitUrlSetting,
4344
MatrixRTCMode,
44-
useSettingWithLastUpdateReason,
4545
} from "./settings";
4646
import type { Room as LivekitRoom } from "livekit-client";
4747
import styles from "./DeveloperSettingsTab.module.css";
4848
import { useUrlParams } from "../UrlParams";
49+
import { getSFUConfigWithOpenID } from "../livekit/openIDSFU";
4950

5051
interface Props {
5152
client: MatrixClient;
@@ -93,8 +94,11 @@ export const DeveloperSettingsTab: FC<Props> = ({
9394
alwaysShowIphoneEarpieceSetting,
9495
);
9596

96-
const [customLivekitUrl, setCustomLivekitUrl, customLivekitUrlUpdateReason] =
97-
useSettingWithLastUpdateReason(customLivekitUrlSetting);
97+
const [customLivekitUrlUpdateError, setCustomLivekitUrlUpdateError] =
98+
useState<string | null>(null);
99+
const [customLivekitUrl, setCustomLivekitUrl] = useSetting(
100+
customLivekitUrlSetting,
101+
);
98102
const [customLivekitUrlTextBuffer, setCustomLivekitUrlTextBuffer] =
99103
useState(customLivekitUrl);
100104
useEffect(() => {
@@ -217,11 +221,11 @@ export const DeveloperSettingsTab: FC<Props> = ({
217221
/>{" "}
218222
</FieldRow>
219223
<EditInPlace
224+
serverInvalid={customLivekitUrlUpdateError !== null}
220225
onSubmit={(e) => e.preventDefault()}
221226
helpLabel={
222227
customLivekitUrl === null
223-
? t("developer_mode.custom_livekit_url.from_config") +
224-
(customLivekitUrlUpdateReason ?? "")
228+
? t("developer_mode.custom_livekit_url.from_config")
225229
: t("developer_mode.custom_livekit_url.current_url") +
226230
customLivekitUrl
227231
}
@@ -230,14 +234,36 @@ export const DeveloperSettingsTab: FC<Props> = ({
230234
savingLabel={t("developer_mode.custom_livekit_url.saving")}
231235
cancelButtonLabel={t("developer_mode.custom_livekit_url.reset")}
232236
onSave={useCallback(
233-
(e: React.FormEvent<HTMLFormElement>) => {
234-
setCustomLivekitUrl(
235-
customLivekitUrlTextBuffer === ""
236-
? null
237-
: customLivekitUrlTextBuffer,
238-
);
237+
async (e: React.FormEvent<HTMLFormElement>): Promise<void> => {
238+
if (
239+
customLivekitUrlTextBuffer === "" ||
240+
customLivekitUrlTextBuffer === null
241+
) {
242+
setCustomLivekitUrl(null);
243+
return Promise.resolve();
244+
}
245+
246+
try {
247+
logger.debug("try setting");
248+
await getSFUConfigWithOpenID(
249+
client,
250+
customLivekitUrlTextBuffer,
251+
"Test-room-alias-" + Date.now().toString() + client.getUserId(),
252+
);
253+
logger.debug("done setting! Success");
254+
setCustomLivekitUrlUpdateError(null);
255+
setCustomLivekitUrl(customLivekitUrlTextBuffer);
256+
} catch (e) {
257+
logger.error("failed setting", e);
258+
setCustomLivekitUrlUpdateError("invalid URL (did not update)");
259+
// automatically unset the error after 4 seconds (2 seconds will be for the save label)
260+
setTimeout(() => {
261+
logger.debug("unsetting error");
262+
setCustomLivekitUrlUpdateError(null);
263+
}, 2000);
264+
}
239265
},
240-
[setCustomLivekitUrl, customLivekitUrlTextBuffer],
266+
[customLivekitUrlTextBuffer, setCustomLivekitUrl, client],
241267
)}
242268
value={customLivekitUrlTextBuffer ?? ""}
243269
onChange={useCallback(
@@ -252,7 +278,13 @@ export const DeveloperSettingsTab: FC<Props> = ({
252278
},
253279
[setCustomLivekitUrl],
254280
)}
255-
/>
281+
>
282+
{customLivekitUrlUpdateError !== null && (
283+
<Text size="sm" priority="low">
284+
{customLivekitUrlUpdateError}
285+
</Text>
286+
)}
287+
</EditInPlace>
256288
<Heading as="h3" type="body" weight="semibold" size="lg">
257289
{t("developer_mode.matrixRTCMode.title")}
258290
</Heading>

src/settings/settings.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,6 @@ export function useSetting<T>(setting: Setting<T>): [T, (value: T) => void] {
6262
return [useBehavior(setting.value$), setting.setValue];
6363
}
6464

65-
/**
66-
* React hook that returns a settings's current value and a setter.
67-
*/
68-
export function useSettingWithLastUpdateReason<T>(
69-
setting: Setting<T>,
70-
): [T, (value: T) => void, string | null] {
71-
return [
72-
useBehavior(setting.value$),
73-
setting.setValue,
74-
useBehavior(setting.lastUpdateReason$),
75-
];
76-
}
77-
7865
// null = undecided
7966
export const optInAnalytics = new Setting<boolean | null>(
8067
"opt-in-analytics",

0 commit comments

Comments
 (0)