From 47dc05d28506821afcf52f629616dcceb3365ce1 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 5 Dec 2023 05:06:02 +0100 Subject: [PATCH] Use list view to render accounts --- components/header.js | 6 +- components/switch-account.js | 111 +++++++++++++---------------------- components/user-list.js | 42 +++++++------ 3 files changed, 69 insertions(+), 90 deletions(-) diff --git a/components/header.js b/components/header.js index a6460bdf..2aeb7adb 100644 --- a/components/header.js +++ b/components/header.js @@ -28,7 +28,7 @@ import { clearNotifications } from '../lib/badge' import { useServiceWorker } from './serviceworker' import SubSelect from './sub-select' import { useShowModal } from './modal' -import SwitchAccountDialog, { useAccounts } from './switch-account' +import SwitchAccountList, { useAccounts } from './switch-account' function WalletSummary ({ me }) { if (!me) return null @@ -128,7 +128,7 @@ function NavProfileMenu ({ me, dropNavKey }) { - showModal(onClose => )}>switch account + showModal(onClose => )}>switch account { const status = await multiAuthSignout() @@ -209,7 +209,7 @@ function LurkerCorner ({ path }) { - showModal(onClose => )}>switch account + showModal(onClose => )}>switch account diff --git a/components/switch-account.js b/components/switch-account.js index eb2afbe9..af8808f4 100644 --- a/components/switch-account.js +++ b/components/switch-account.js @@ -1,13 +1,11 @@ import { createContext, useCallback, useContext, useEffect, useState } from 'react' -import AnonIcon from '../svgs/spy-fill.svg' import { useRouter } from 'next/router' import cookie from 'cookie' import { useMe } from './me' -import Image from 'react-bootstrap/Image' -import Link from 'next/link' -import { SSR } from '../lib/constants' +import { ANON_USER_ID, SSR } from '../lib/constants' import { USER } from '../fragments/users' import { useQuery } from '@apollo/client' +import { UserListRow } from './user-list' const AccountContext = createContext() @@ -62,92 +60,67 @@ export const AccountProvider = ({ children }) => { export const useAccounts = () => useContext(AccountContext) -const AnonAccount = ({ selected, onClick }) => { +const AccountListRow = ({ account, ...props }) => { const { isAnon, setIsAnon } = useAccounts() - const { refreshMe } = useMe() - return ( -
- { - document.cookie = 'multi_auth.user-id=anonymous; Path=/; Secure' - // order is important to prevent flashes of no session - setIsAnon(true) - await refreshMe() - }} - /> -
anonymous
- {isAnon &&
selected
} -
- ) -} + const { me, refreshMe } = useMe() + const anonRow = account.id === ANON_USER_ID + const selected = (isAnon && anonRow) || Number(me?.id) === Number(account.id) -const Account = ({ account, className }) => { - const { me } = useMe() + // fetch updated names and photo ids since they might have changed since we were issued the JWTs const [name, setName] = useState(account.name) - const [src, setSrc] = useState(account.photoId || '/dorian400.jpg') - const { refreshMe } = useMe() - const { setIsAnon } = useAccounts() + const [photoId, setPhotoId] = useState(account.photoId) useQuery(USER, { variables: { id: account.id }, onCompleted ({ user: { name, photoId } }) { - if (photoId) { - const src = `https://${process.env.NEXT_PUBLIC_MEDIA_DOMAIN}/${photoId}` - setSrc(src) - } - setName(name) + if (photoId) setPhotoId(photoId) + if (name) setName(name) } } ) + + const onClick = async () => { + document.cookie = `multi_auth.user-id=${anonRow ? 'anonymous' : account.id}; Path=/; Secure` + if (anonRow) { + // order is important to prevent flashes of no session + setIsAnon(true) + await refreshMe() + } else { + await refreshMe() + // order is important to prevent flashes of inconsistent data in switch account dialog + setIsAnon(account.id === ANON_USER_ID) + } + } + // can't show hat since we don't have access to the streak from the data available in the cookies return ( -
- { - document.cookie = `multi_auth.user-id=${account.id}; Path=/; Secure` - await refreshMe() - // order is important to prevent flashes of inconsistent data in switch account dialog - setIsAnon(false) - }} - /> - @{name} - {Number(me?.id) === Number(account.id) &&
selected
} +
+ +
switch
+ {selected &&
selected
}
) } -const AddAccount = () => { - const router = useRouter() - return ( -
- { - router.push({ - pathname: '/login', - query: { callbackUrl: window.location.origin + router.asPath, multiAuth: true } - }) - }} - /> -
+ add account
-
- ) -} - -export default function SwitchAccountDialog () { +export default function SwitchAccountList () { const { accounts } = useAccounts() + const router = useRouter() + const addAccount = () => { + router.push({ + pathname: '/login', + query: { callbackUrl: window.location.origin + router.asPath, multiAuth: true } + }) + } return ( <>
-
- +
+ + { - accounts.map((account) => ) + + accounts.map((account) => ) } - +
+ add account
diff --git a/components/user-list.js b/components/user-list.js index 5cdf7992..a2dc44f9 100644 --- a/components/user-list.js +++ b/components/user-list.js @@ -36,6 +36,29 @@ function seperate (arr, seperator) { return arr.flatMap((x, i) => i < arr.length - 1 ? [x, seperator] : [x]) } +export const UserListRow = ({ user, stats, className, showHat = true }) => { + return ( +
+ + + +
+ + @{user.name}{showHat && } + + {stats && ( +
+ {stats.map((Comp, i) => )} +
+ )} +
+
+ ) +} + export default function UserList ({ ssrData, query, variables, destructureData }) { const { data, fetchMore } = useQuery(query, { variables }) const dat = useData(data, ssrData) @@ -62,24 +85,7 @@ export default function UserList ({ ssrData, query, variables, destructureData } return ( <> - {users?.map(user => ( -
- - - -
- - @{user.name} - -
- {statComps.map((Comp, i) => )} -
-
-
- ))} + {users?.map(user => )} )