mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 17:41:25 -08:00
Remove account switcher tab
This commit is contained in:
1 parent
fc5252fa16
commit
8fc750447a
2 files changed
+1
-424
No files matched your search
@@ -238,7 +238,7 @@ export function UserHero({
|
||||
WebkitBoxOrient: 'vertical',
|
||||
overflow: 'hidden',
|
||||
fontStyle: allowEditing && !status ? 'italic' : 'normal',
|
||||
opacity: allowEditing && !status ? config.opacity.Placeholder : 1
|
||||
opacity: allowEditing && !status ? config.opacity.Placeholder : 1,
|
||||
}}
|
||||
>
|
||||
{status || (allowEditing && "What's on your mind?")}
|
||||
|
||||
@@ -1,423 +0,0 @@
|
||||
import type { MouseEvent, MouseEventHandler } from 'react';
|
||||
import { useCallback, useState } from 'react';
|
||||
import type { RectCords } from 'folds';
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
Dialog,
|
||||
Header,
|
||||
Menu,
|
||||
MenuItem,
|
||||
PopOut,
|
||||
Text,
|
||||
config,
|
||||
toRem,
|
||||
Chip,
|
||||
Spinner,
|
||||
Overlay,
|
||||
OverlayBackdrop,
|
||||
OverlayCenter,
|
||||
Line,
|
||||
} from 'folds';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import type { Session } from '$state/sessions';
|
||||
import { sessionsAtom, activeSessionIdAtom, backgroundUnreadCountsAtom } from '$state/sessions';
|
||||
import {
|
||||
SidebarItemTooltip,
|
||||
SidebarAvatar,
|
||||
SidebarUnreadBadge,
|
||||
SidebarItem,
|
||||
} from '$components/sidebar';
|
||||
import { UserAvatar } from '$components/user-avatar';
|
||||
import { nameInitials } from '$utils/common';
|
||||
import { getMxIdLocalPart, mxcUrlToHttp } from '$utils/matrix';
|
||||
import { stopPropagation } from '$utils/keyboard';
|
||||
import { getHomePath, getLoginPath, withSearchParam } from '$pages/pathUtils';
|
||||
import { logoutClient, initClient, stopClient } from '$client/initMatrix';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { useUserProfile } from '$hooks/useUserProfile';
|
||||
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
|
||||
import { useSessionProfiles } from '$hooks/useSessionProfiles';
|
||||
import { useOpenSettings } from '$features/settings';
|
||||
import { createLogger } from '$utils/debug';
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
import { UnreadBadge, UnreadBadgeCenter } from '$components/unread-badge';
|
||||
import { Check, chipIcon, GearSix, menuIcon, Plus } from '$components/icons/phosphor';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
import { usePathWithOrigin } from '$hooks/usePathWithOrigin';
|
||||
|
||||
const log = createLogger('AccountSwitcherTab');
|
||||
|
||||
function AccountRow({
|
||||
session,
|
||||
isActive,
|
||||
displayName,
|
||||
avatarUrl,
|
||||
isBusy,
|
||||
unread,
|
||||
onSwitch,
|
||||
onSignOut,
|
||||
}: {
|
||||
session: Session;
|
||||
isActive: boolean;
|
||||
displayName?: string;
|
||||
avatarUrl?: string;
|
||||
isBusy?: boolean;
|
||||
unread?: { total: number; highlight: number };
|
||||
onSwitch: (session: Session) => void;
|
||||
onSignOut: (session: Session) => void;
|
||||
}) {
|
||||
const localPart = getMxIdLocalPart(session.userId) ?? session.userId;
|
||||
const server = session.userId.split(':')[1] ?? session.baseUrl;
|
||||
const label = displayName ?? localPart;
|
||||
|
||||
return (
|
||||
<MenuItem
|
||||
size="400"
|
||||
radii="300"
|
||||
style={{
|
||||
opacity: isBusy ? 0.6 : undefined,
|
||||
height: 'auto',
|
||||
}}
|
||||
before={
|
||||
<SidebarAvatar size="200" style={{ width: toRem(28), height: toRem(28) }}>
|
||||
<UserAvatar
|
||||
userId={session.userId}
|
||||
src={avatarUrl}
|
||||
alt={label}
|
||||
renderFallback={() => <Text size="H6">{nameInitials(label)}</Text>}
|
||||
/>
|
||||
</SidebarAvatar>
|
||||
}
|
||||
after={
|
||||
<Box gap="200" alignItems="Center" shrink="No">
|
||||
{!isActive && unread && unread.total > 0 && (
|
||||
<UnreadBadgeCenter>
|
||||
<UnreadBadge highlight={unread.highlight > 0} count={unread.total} />
|
||||
</UnreadBadgeCenter>
|
||||
)}
|
||||
{isActive && chipIcon(Check, { style: { color: 'var(--mx-c-success)' } })}
|
||||
{isBusy ? (
|
||||
<Spinner size="200" variant="Secondary" />
|
||||
) : (
|
||||
<Chip
|
||||
variant="Critical"
|
||||
fill="None"
|
||||
size="400"
|
||||
radii="300"
|
||||
onClick={(e: MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
onSignOut(session);
|
||||
}}
|
||||
>
|
||||
<Text size="T200">Sign out</Text>
|
||||
</Chip>
|
||||
)}
|
||||
</Box>
|
||||
}
|
||||
onClick={() => !isActive && !isBusy && onSwitch(session)}
|
||||
>
|
||||
<Box
|
||||
direction="Column"
|
||||
grow="Yes"
|
||||
style={{
|
||||
paddingTop: config.space.S100,
|
||||
paddingBottom: config.space.S100,
|
||||
justifyContent: 'Center',
|
||||
}}
|
||||
>
|
||||
<Text size="T300" truncate>
|
||||
{label}
|
||||
</Text>
|
||||
<Text size="T200" priority="300" truncate>
|
||||
{isActive ? session.userId : server}
|
||||
</Text>
|
||||
</Box>
|
||||
</MenuItem>
|
||||
);
|
||||
}
|
||||
|
||||
export function AccountSwitcherTab({ isBottom }: { isBottom?: boolean }) {
|
||||
const mx = useMatrixClient();
|
||||
const navigate = useNavigate();
|
||||
const sessions = useAtomValue(sessionsAtom);
|
||||
const [activeSessionId, setActiveSessionId] = useAtom(activeSessionIdAtom);
|
||||
const setSessions = useSetAtom(sessionsAtom);
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const backgroundUnreads = useAtomValue(backgroundUnreadCountsAtom);
|
||||
const setBackgroundUnreads = useSetAtom(backgroundUnreadCountsAtom);
|
||||
const openSettings = useOpenSettings();
|
||||
const [oldSidebar] = useSetting(settingsAtom, 'oldSidebar');
|
||||
|
||||
// Total unread count across all background sessions (for the sidebar badge).
|
||||
const totalBackgroundUnread = Object.entries(backgroundUnreads)
|
||||
.filter(([uid]) => uid !== (activeSessionId ?? sessions[0]?.userId))
|
||||
.reduce((acc, [, u]) => acc + u.total, 0);
|
||||
const totalBackgroundHighlight = Object.entries(backgroundUnreads)
|
||||
.filter(([uid]) => uid !== (activeSessionId ?? sessions[0]?.userId))
|
||||
.reduce((acc, [, u]) => acc + u.highlight, 0);
|
||||
const anyBackgroundHighlight = totalBackgroundHighlight > 0;
|
||||
|
||||
const [menuAnchor, setMenuAnchor] = useState<RectCords>();
|
||||
const [busyUserIds, setBusyUserIds] = useState(new Set());
|
||||
const [confirmSignOutSession, setConfirmSignOutSession] = useState<Session | undefined>(
|
||||
undefined
|
||||
);
|
||||
|
||||
const activeSession = sessions.find((s) => s.userId === activeSessionId) ?? sessions[0];
|
||||
|
||||
const myUserId = mx.getUserId() ?? '';
|
||||
const activeProfile = useUserProfile(myUserId);
|
||||
const activeAvatarUrl = activeProfile.avatarUrl
|
||||
? (mxcUrlToHttp(mx, activeProfile.avatarUrl, useAuthentication, 96, 96, 'crop') ?? undefined)
|
||||
: undefined;
|
||||
const activeDisplayName = activeProfile.displayName;
|
||||
|
||||
const sessionProfiles = useSessionProfiles(sessions);
|
||||
|
||||
const { disableAccountSwitcher } = useClientConfig();
|
||||
|
||||
const loginUrl = usePathWithOrigin(getLoginPath());
|
||||
|
||||
const handleToggle: MouseEventHandler<HTMLButtonElement> = (evt) => {
|
||||
if (disableAccountSwitcher) {
|
||||
openSettings();
|
||||
return;
|
||||
}
|
||||
const cords = evt.currentTarget.getBoundingClientRect();
|
||||
setMenuAnchor((cur) => (cur ? undefined : cords));
|
||||
};
|
||||
|
||||
const handleSwitch = useCallback(
|
||||
(session: Session) => {
|
||||
log.log('switching to account', session.userId);
|
||||
setMenuAnchor(undefined);
|
||||
navigate(getHomePath(), { replace: true });
|
||||
setActiveSessionId(session.userId);
|
||||
// Clear the unread badge for the account we're now switching into.
|
||||
setBackgroundUnreads((prev) => {
|
||||
const next = { ...prev };
|
||||
delete next[session.userId];
|
||||
return next;
|
||||
});
|
||||
},
|
||||
[navigate, setActiveSessionId, setBackgroundUnreads]
|
||||
);
|
||||
|
||||
const handleSignOut = useCallback(
|
||||
async (session: Session) => {
|
||||
log.log('signing out', session.userId);
|
||||
setMenuAnchor(undefined);
|
||||
setBusyUserIds((prev) => new Set(prev).add(session.userId));
|
||||
try {
|
||||
if (session.userId === mx.getUserId()) {
|
||||
await logoutClient(mx, session);
|
||||
setSessions({ type: 'DELETE', session });
|
||||
setActiveSessionId(
|
||||
sessions.find((s) => s.userId !== session.userId)?.userId ?? undefined
|
||||
);
|
||||
window.location.reload();
|
||||
} else {
|
||||
try {
|
||||
const tempMx = await initClient(session);
|
||||
await logoutClient(tempMx, session);
|
||||
} catch (err) {
|
||||
log.error('failed to logout background session, IndexedDB may remain', err);
|
||||
}
|
||||
setSessions({ type: 'DELETE', session });
|
||||
if (activeSessionId === session.userId) {
|
||||
setActiveSessionId(
|
||||
sessions.find((s) => s.userId !== session.userId)?.userId ?? undefined
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
log.error('Logout failed', err);
|
||||
} finally {
|
||||
setBusyUserIds((prev) => {
|
||||
const next = new Set(prev);
|
||||
next.delete(session.userId);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
},
|
||||
[mx, sessions, activeSessionId, setSessions, setActiveSessionId]
|
||||
);
|
||||
|
||||
const handleAddAccount = () => {
|
||||
const url = withSearchParam(loginUrl, { addAccount: '1' });
|
||||
setMenuAnchor(undefined);
|
||||
stopClient(mx);
|
||||
setTimeout(() => window.location.assign(url), 100);
|
||||
};
|
||||
|
||||
const handleOpenSettings = () => {
|
||||
setMenuAnchor(undefined);
|
||||
openSettings();
|
||||
};
|
||||
|
||||
const activeLocalPart =
|
||||
getMxIdLocalPart(activeSession?.userId ?? '') ?? activeSession?.userId ?? '';
|
||||
const label = activeDisplayName ?? activeLocalPart;
|
||||
|
||||
if (!activeSession) return null;
|
||||
|
||||
return (
|
||||
<SidebarItem active={!!menuAnchor} isBottom={isBottom}>
|
||||
<SidebarItemTooltip tooltip={label} position={isBottom ? 'Top' : 'Right'}>
|
||||
{(triggerRef) => (
|
||||
<SidebarAvatar
|
||||
as="button"
|
||||
ref={triggerRef}
|
||||
onClick={handleToggle}
|
||||
outlined={sessions.length > 1}
|
||||
>
|
||||
<UserAvatar
|
||||
userId={activeSession.userId}
|
||||
src={activeAvatarUrl}
|
||||
alt={label}
|
||||
renderFallback={() => <Text size="H4">{nameInitials(label)}</Text>}
|
||||
/>
|
||||
</SidebarAvatar>
|
||||
)}
|
||||
</SidebarItemTooltip>
|
||||
{(totalBackgroundUnread > 0 || anyBackgroundHighlight) && (
|
||||
<SidebarUnreadBadge
|
||||
highlight={anyBackgroundHighlight}
|
||||
count={anyBackgroundHighlight ? totalBackgroundHighlight : totalBackgroundUnread}
|
||||
/>
|
||||
)}
|
||||
|
||||
<PopOut
|
||||
anchor={menuAnchor}
|
||||
position={isBottom ? 'Top' : 'Right'}
|
||||
align="End"
|
||||
offset={6}
|
||||
content={
|
||||
<FocusTrap
|
||||
focusTrapOptions={{
|
||||
initialFocus: false,
|
||||
returnFocusOnDeactivate: false,
|
||||
onDeactivate: () => setMenuAnchor(undefined),
|
||||
clickOutsideDeactivates: true,
|
||||
isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown',
|
||||
isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp',
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<Menu style={{ minWidth: toRem(240) }}>
|
||||
<Box direction="Column" gap="100" style={{ padding: config.space.S100 }}>
|
||||
<Text size="L400" style={{ padding: `${config.space.S100} ${config.space.S200}` }}>
|
||||
Accounts
|
||||
</Text>
|
||||
{sessions.map((session) => {
|
||||
const isActive = session.userId === (activeSessionId ?? sessions[0]?.userId);
|
||||
let rowDisplayName: string | undefined;
|
||||
let rowAvatarUrl: string | undefined;
|
||||
if (isActive) {
|
||||
rowDisplayName = activeDisplayName;
|
||||
rowAvatarUrl = activeAvatarUrl;
|
||||
} else {
|
||||
const prof = sessionProfiles[session.userId];
|
||||
rowDisplayName = prof?.displayName;
|
||||
rowAvatarUrl = prof?.avatarHttpUrl;
|
||||
}
|
||||
return (
|
||||
<AccountRow
|
||||
key={session.userId}
|
||||
session={session}
|
||||
isActive={isActive}
|
||||
displayName={rowDisplayName}
|
||||
avatarUrl={rowAvatarUrl}
|
||||
isBusy={busyUserIds.has(session.userId)}
|
||||
unread={!isActive ? backgroundUnreads[session.userId] : undefined}
|
||||
onSwitch={handleSwitch}
|
||||
onSignOut={(pendingSession) => {
|
||||
setMenuAnchor(undefined);
|
||||
setConfirmSignOutSession(pendingSession);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
<MenuItem size="300" radii="300" before={chipIcon(Plus)} onClick={handleAddAccount}>
|
||||
<Text size="T300">Add Account</Text>
|
||||
</MenuItem>
|
||||
{/*This will defo need to be reverted w the new statuses, w the right changes in the SidebarNav to make the cog a permanent fixture but democracy wants old style*/}
|
||||
{oldSidebar && (
|
||||
<>
|
||||
<Line
|
||||
variant="Surface"
|
||||
size="300"
|
||||
style={{ margin: `${config.space.S100} 0` }}
|
||||
/>
|
||||
<MenuItem
|
||||
size="300"
|
||||
radii="300"
|
||||
before={menuIcon(GearSix)}
|
||||
onClick={handleOpenSettings}
|
||||
>
|
||||
<Text size="T300">Settings</Text>
|
||||
</MenuItem>
|
||||
</>
|
||||
)}
|
||||
</Box>
|
||||
</Menu>
|
||||
</FocusTrap>
|
||||
}
|
||||
/>
|
||||
|
||||
{confirmSignOutSession && (
|
||||
<Overlay open backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
<FocusTrap
|
||||
focusTrapOptions={{
|
||||
initialFocus: false,
|
||||
fallbackFocus: () => document.body,
|
||||
clickOutsideDeactivates: true,
|
||||
onDeactivate: () => setConfirmSignOutSession(undefined),
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<Dialog variant="Surface">
|
||||
<Header
|
||||
style={{
|
||||
padding: `0 ${config.space.S200} 0 ${config.space.S400}`,
|
||||
borderBottomWidth: config.borderWidth.B300,
|
||||
}}
|
||||
variant="Surface"
|
||||
size="500"
|
||||
>
|
||||
<Box grow="Yes">
|
||||
<Text size="H4">Sign out</Text>
|
||||
</Box>
|
||||
</Header>
|
||||
<Box style={{ padding: config.space.S400 }} direction="Column" gap="400">
|
||||
<Text priority="400">
|
||||
Are you sure you want to sign out of <b>{confirmSignOutSession.userId}</b>?
|
||||
</Text>
|
||||
<Box direction="Column" gap="200">
|
||||
<Button
|
||||
variant="Critical"
|
||||
onClick={() => {
|
||||
handleSignOut(confirmSignOutSession);
|
||||
setConfirmSignOutSession(undefined);
|
||||
}}
|
||||
>
|
||||
<Text size="B400">Sign out</Text>
|
||||
</Button>
|
||||
<Button variant="Secondary" onClick={() => setConfirmSignOutSession(undefined)}>
|
||||
<Text size="B400">Cancel</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
</Box>
|
||||
</Dialog>
|
||||
</FocusTrap>
|
||||
</OverlayCenter>
|
||||
</Overlay>
|
||||
)}
|
||||
</SidebarItem>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user