mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
Add account switcher and logout button to homeserver offline screen (#1115)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Title #### Type of change - [x] Bug fix-ish (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
This commit is contained in:
2 files changed
+98
-7
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Add account switching and logout actions to the homeserver offline screen.
|
||||
@@ -1,10 +1,15 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { useCallback } from 'react';
|
||||
import { Box, Button, Dialog, config, Text } from 'folds';
|
||||
import { useCallback, useState } from 'react';
|
||||
import { Box, Button, Dialog, config, Spinner, Text, color } from 'folds';
|
||||
import { useAtomValue, useSetAtom } from 'jotai';
|
||||
import { SpecVersionsLoader } from '$components/SpecVersionsLoader';
|
||||
import { SpecVersionsProvider } from '$hooks/useSpecVersions';
|
||||
import { SplashScreen } from '$components/splash-screen';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { logoutClient } from '$client/initMatrix';
|
||||
import { activeSessionIdAtom, sessionsAtom, type Session } from '$state/sessions';
|
||||
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
import type { SpecVersions } from '../../cs-api';
|
||||
|
||||
const EMPTY_VERSIONS: SpecVersions = { versions: [] };
|
||||
@@ -14,6 +19,39 @@ type HomeserverOfflineErrorProps = {
|
||||
onRetry: () => void;
|
||||
};
|
||||
function HomeserverOfflineError({ baseUrl, onRetry }: HomeserverOfflineErrorProps) {
|
||||
const mx = useMatrixClient();
|
||||
const sessions = useAtomValue(sessionsAtom);
|
||||
const activeSessionId = useAtomValue(activeSessionIdAtom);
|
||||
const setActiveSessionId = useSetAtom(activeSessionIdAtom);
|
||||
const setSessions = useSetAtom(sessionsAtom);
|
||||
const { disableAccountSwitcher } = useClientConfig();
|
||||
const [showAccounts, setShowAccounts] = useState(false);
|
||||
|
||||
const activeSession =
|
||||
sessions.find((session) => session.userId === activeSessionId) ?? sessions[0];
|
||||
const otherSessions = disableAccountSwitcher
|
||||
? []
|
||||
: sessions.filter((session) => session.userId !== activeSession?.userId);
|
||||
|
||||
const handleSwitch = (session: Session) => {
|
||||
setActiveSessionId(session.userId);
|
||||
};
|
||||
|
||||
const [logoutState, logout] = useAsyncCallback<void, Error, []>(
|
||||
useCallback(async () => {
|
||||
if (!activeSession) return;
|
||||
|
||||
await logoutClient(mx, activeSession);
|
||||
setSessions({ type: 'DELETE', session: activeSession });
|
||||
setActiveSessionId(
|
||||
sessions.find((session) => session.userId !== activeSession.userId)?.userId
|
||||
);
|
||||
window.location.reload();
|
||||
}, [mx, activeSession, sessions, setSessions, setActiveSessionId])
|
||||
);
|
||||
|
||||
const loggingOut = logoutState.status === AsyncStatus.Loading;
|
||||
|
||||
return (
|
||||
<SplashScreen>
|
||||
<Box direction="Column" grow="Yes" alignItems="Center" justifyContent="Center" gap="400">
|
||||
@@ -26,11 +64,59 @@ function HomeserverOfflineError({ baseUrl, onRetry }: HomeserverOfflineErrorProp
|
||||
may have a connection issue. Please try again.
|
||||
</Text>
|
||||
</Box>
|
||||
<Button variant="Critical" onClick={onRetry} fill="Soft">
|
||||
<Text as="span" size="B400">
|
||||
Retry
|
||||
</Text>
|
||||
</Button>
|
||||
<Box direction="Column" gap="200">
|
||||
<Button variant="Critical" onClick={onRetry} fill="Soft" disabled={loggingOut}>
|
||||
<Text as="span" size="B400">
|
||||
Retry
|
||||
</Text>
|
||||
</Button>
|
||||
{otherSessions.length > 0 && (
|
||||
<>
|
||||
<Button
|
||||
variant="Secondary"
|
||||
fill="Soft"
|
||||
onClick={() => setShowAccounts((visible) => !visible)}
|
||||
aria-expanded={showAccounts}
|
||||
>
|
||||
<Text as="span" size="B400">
|
||||
Switch account
|
||||
</Text>
|
||||
</Button>
|
||||
{showAccounts && (
|
||||
<Box direction="Column" gap="100">
|
||||
{otherSessions.map((session) => (
|
||||
<Button
|
||||
key={session.userId}
|
||||
variant="Secondary"
|
||||
fill="None"
|
||||
onClick={() => handleSwitch(session)}
|
||||
>
|
||||
<Text as="span" size="B300" truncate>
|
||||
{session.userId}
|
||||
</Text>
|
||||
</Button>
|
||||
))}
|
||||
</Box>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
<Button
|
||||
variant="Critical"
|
||||
fill="None"
|
||||
onClick={logout}
|
||||
disabled={loggingOut}
|
||||
before={loggingOut && <Spinner variant="Critical" size="200" />}
|
||||
>
|
||||
<Text as="span" size="B400">
|
||||
Logout
|
||||
</Text>
|
||||
</Button>
|
||||
{logoutState.status === AsyncStatus.Error && (
|
||||
<Text size="T200" style={{ color: color.Critical.Main }}>
|
||||
Failed to logout. {logoutState.error.message}
|
||||
</Text>
|
||||
)}
|
||||
</Box>
|
||||
</Box>
|
||||
</Dialog>
|
||||
</Box>
|
||||
|
||||
Reference in new issue
Block a user