From 4d666e92690409ae143966084256521d4cc9a12e Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 22 Mar 2025 20:02:27 -0500 Subject: [PATCH 1/2] Simplify state of selected account --- components/account.js | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/components/account.js b/components/account.js index 3a9422454..86482c623 100644 --- a/components/account.js +++ b/components/account.js @@ -1,7 +1,6 @@ import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react' import { useRouter } from 'next/router' import * as cookie from 'cookie' -import { useMe } from '@/components/me' import { USER_ID, SSR } from '@/lib/constants' import { USER } from '@/fragments/users' import { useQuery } from '@apollo/client' @@ -19,8 +18,8 @@ const b64Decode = str => Buffer.from(str, 'base64').toString('utf-8') export const AccountProvider = ({ children }) => { const [accounts, setAccounts] = useState([]) - const [meAnon, setMeAnon] = useState(true) const [errors, setErrors] = useState([]) + const [selected, setSelected] = useState(null) const updateAccountsFromCookie = useCallback(() => { const { [MULTI_AUTH_LIST]: listCookie } = cookie.parse(document.cookie) @@ -59,7 +58,7 @@ export const AccountProvider = ({ children }) => { updateAccountsFromCookie() const { [MULTI_AUTH_POINTER]: pointerCookie } = cookie.parse(document.cookie) - setMeAnon(pointerCookie === 'anonymous') + setSelected(pointerCookie === MULTI_AUTH_ANON ? USER_ID.anon : Number(pointerCookie)) const interval = setInterval(checkErrors, CHECK_ERRORS_INTERVAL_MS) return () => clearInterval(interval) @@ -68,22 +67,18 @@ export const AccountProvider = ({ children }) => { const value = useMemo( () => ({ accounts, - meAnon, - setMeAnon, + selected, nextAccount, multiAuthErrors: errors }), - [accounts, meAnon, setMeAnon, nextAccount]) + [accounts, selected, nextAccount]) return {children} } export const useAccounts = () => useContext(AccountContext) const AccountListRow = ({ account, ...props }) => { - const { meAnon, setMeAnon } = useAccounts() - const { me, refreshMe } = useMe() - const anonRow = account.id === USER_ID.anon - const selected = (meAnon && anonRow) || Number(me?.id) === Number(account.id) + const { selected } = useAccounts() const router = useRouter() // fetch updated names and photo ids since they might have changed since we were issued the JWTs @@ -103,18 +98,8 @@ const AccountListRow = ({ account, ...props }) => { // update pointer cookie const options = cookieOptions({ httpOnly: false }) - document.cookie = cookie.serialize(MULTI_AUTH_POINTER, anonRow ? MULTI_AUTH_ANON : account.id, options) - - // update state - if (anonRow) { - // order is important to prevent flashes of no session - setMeAnon(true) - await refreshMe() - } else { - await refreshMe() - // order is important to prevent flashes of inconsistent data in switch account dialog - setMeAnon(account.id === USER_ID.anon) - } + const anon = account.id === USER_ID.anon + document.cookie = cookie.serialize(MULTI_AUTH_POINTER, anon ? MULTI_AUTH_ANON : account.id, options) // reload whatever page we're on to avoid any bugs due to missing authorization etc. router.reload() @@ -127,7 +112,7 @@ const AccountListRow = ({ account, ...props }) => { className='d-flex align-items-center me-2' {...props} onNymClick={onClick} - selected={selected} + selected={selected === account.id} /> ) From f999ad3fb15d0d5b5f47e8d5d14a5b3e5da3466d Mon Sep 17 00:00:00 2001 From: ekzyis Date: Mon, 24 Mar 2025 13:37:26 -0500 Subject: [PATCH 2/2] Remove multi auth error checking --- components/account.js | 42 +++--------------------------------------- components/banners.js | 22 ---------------------- 2 files changed, 3 insertions(+), 61 deletions(-) diff --git a/components/account.js b/components/account.js index 86482c623..ae319f0ac 100644 --- a/components/account.js +++ b/components/account.js @@ -7,18 +7,14 @@ import { useQuery } from '@apollo/client' import { UserListRow } from '@/components/user-list' import Link from 'next/link' import AddIcon from '@/svgs/add-fill.svg' -import { MultiAuthErrorBanner } from '@/components/banners' import { cookieOptions, MULTI_AUTH_ANON, MULTI_AUTH_LIST, MULTI_AUTH_POINTER } from '@/lib/auth' const AccountContext = createContext() -const CHECK_ERRORS_INTERVAL_MS = 5_000 - const b64Decode = str => Buffer.from(str, 'base64').toString('utf-8') export const AccountProvider = ({ children }) => { const [accounts, setAccounts] = useState([]) - const [errors, setErrors] = useState([]) const [selected, setSelected] = useState(null) const updateAccountsFromCookie = useCallback(() => { @@ -38,20 +34,6 @@ export const AccountProvider = ({ children }) => { return switchSuccess }, [updateAccountsFromCookie]) - const checkErrors = useCallback(() => { - const { - [MULTI_AUTH_LIST]: listCookie, - [MULTI_AUTH_POINTER]: pointerCookie - } = cookie.parse(document.cookie) - - const errors = [] - - if (!listCookie) errors.push(`${MULTI_AUTH_LIST} cookie not found`) - if (!pointerCookie) errors.push(`${MULTI_AUTH_POINTER} cookie not found`) - - setErrors(errors) - }, []) - useEffect(() => { if (SSR) return @@ -59,17 +41,13 @@ export const AccountProvider = ({ children }) => { const { [MULTI_AUTH_POINTER]: pointerCookie } = cookie.parse(document.cookie) setSelected(pointerCookie === MULTI_AUTH_ANON ? USER_ID.anon : Number(pointerCookie)) - - const interval = setInterval(checkErrors, CHECK_ERRORS_INTERVAL_MS) - return () => clearInterval(interval) - }, [updateAccountsFromCookie, checkErrors]) + }, [updateAccountsFromCookie]) const value = useMemo( () => ({ accounts, selected, - nextAccount, - multiAuthErrors: errors + nextAccount }), [accounts, selected, nextAccount]) return {children} @@ -119,23 +97,9 @@ const AccountListRow = ({ account, ...props }) => { } export default function SwitchAccountList () { - const { accounts, multiAuthErrors } = useAccounts() + const { accounts } = useAccounts() const router = useRouter() - const hasError = multiAuthErrors.length > 0 - - if (hasError) { - return ( - <> -
-
- -
-
- - ) - } - // can't show hat since the streak is not included in the JWT payload return ( <> diff --git a/components/banners.js b/components/banners.js index c938baada..c4edbf0f2 100644 --- a/components/banners.js +++ b/components/banners.js @@ -6,7 +6,6 @@ import { useMutation } from '@apollo/client' import { WELCOME_BANNER_MUTATION } from '@/fragments/users' import { useToast } from '@/components/toast' import Link from 'next/link' -import AccordianItem from '@/components/accordian-item' export function WelcomeBanner ({ Banner }) { const { me } = useMe() @@ -124,24 +123,3 @@ export function AuthBanner () { ) } - -export function MultiAuthErrorBanner ({ errors }) { - return ( - -
Account switching is currently unavailable
- - {errors.map((err, i) => ( -
  • {err}
  • - ))} - - } - /> -
    To resolve these issues, please sign out and sign in again.
    -
    - ) -}