Skip to content

Commit

Permalink
Rename Panel into State
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Dec 13, 2024
1 parent 9ec7d93 commit 717bec3
Showing 1 changed file with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ import { SettingsSection } from "../../shared/SettingsSection";
import { SettingsSubheader } from "../../SettingsSubheader";

/**
* The panel to show in the encryption settings tab.
* The state in the encryption settings tab.
* - "loading": We are checking if the device is verified.
* - "main": The main panel with all the sections (Key storage, recovery, advanced).
* - "verification_required": The panel to show when the user needs to verify their session.
* - "change_recovery_key": The panel to show when the user is changing their recovery key.
* - "set_recovery_key": The panel to show when the user is setting up their recovery key.
*/
type Panel = "loading" | "main" | "verification_required" | "change_recovery_key" | "set_recovery_key";
type State = "loading" | "main" | "verification_required" | "change_recovery_key" | "set_recovery_key";

export function EncryptionUserSettingsTab(): JSX.Element {
const [panel, setPanel] = useState<Panel>("loading");
const checkVerificationRequired = useVerificationRequired(setPanel);
const [state, setState] = useState<State>("loading");
const checkVerificationRequired = useVerificationRequired(setState);

let content: JSX.Element;
switch (panel) {
switch (state) {
case "loading":
content = <InlineSpinner />;
break;
Expand All @@ -44,26 +44,18 @@ export function EncryptionUserSettingsTab(): JSX.Element {
case "main":
content = (
<RecoveryPanel
onChangingRecoveryKeyClick={() => setPanel("change_recovery_key")}
onSetUpRecoveryClick={() => setPanel("set_recovery_key")}
/>
);
break;
case "set_recovery_key":
content = (
<ChangeRecoveryKey
isSetupFlow={true}
onCancelClick={() => setPanel("main")}
onFinish={() => setPanel("main")}
onChangingRecoveryKeyClick={() => setState("change_recovery_key")}
onSetUpRecoveryClick={() => setState("set_recovery_key")}
/>
);
break;
case "change_recovery_key":
case "set_recovery_key":
content = (
<ChangeRecoveryKey
isSetupFlow={false}
onCancelClick={() => setPanel("main")}
onFinish={() => setPanel("main")}
isSetupFlow={state === "set_recovery_key"}
onCancelClick={() => setState("main")}
onFinish={() => setState("main")}
/>
);
break;
Expand All @@ -74,21 +66,21 @@ export function EncryptionUserSettingsTab(): JSX.Element {

/**
* Hook to check if the user needs to verify their session.
* If the user needs to verify their session, the panel will be set to "verification_required".
* If the user doesn't need to verify their session, the panel will be set to "main".
* @param setPanel
* If the user needs to verify their session, the state will be set to "verification_required".
* If the user doesn't need to verify their session, the state will be set to "main".
* @param setState
*/
function useVerificationRequired(setPanel: (panel: Panel) => void): () => Promise<void> {
function useVerificationRequired(setState: (state: State) => void): () => Promise<void> {
const matrixClient = useMatrixClientContext();

const checkVerificationRequired = useCallback(async () => {
const crypto = matrixClient.getCrypto();
if (!crypto) return;

const isCrossSigningReady = await crypto.isCrossSigningReady();
if (isCrossSigningReady) setPanel("main");
else setPanel("verification_required");
}, [matrixClient, setPanel]);
if (isCrossSigningReady) setState("main");
else setState("verification_required");
}, [matrixClient, setState]);

useEffect(() => {
checkVerificationRequired();
Expand Down

0 comments on commit 717bec3

Please sign in to comment.