diff --git a/packages/app-root/src/components/PageFooter.js b/packages/app-root/src/components/PageFooter.js index 84982f21a32..6f47dec0d49 100644 --- a/packages/app-root/src/components/PageFooter.js +++ b/packages/app-root/src/components/PageFooter.js @@ -1,13 +1,16 @@ 'use client' +import AdminCheckbox from '@zooniverse/react-components/AdminCheckbox' import ZooFooter from '@zooniverse/react-components/ZooFooter' -import { usePanoptesUser } from '../hooks' +import { useAdminMode, usePanoptesUser } from '../hooks' export default function PageFooter() { - // we'll need the user in order to detect admin mode. - const { data: user } = usePanoptesUser() + const { data: user, isLoading } = usePanoptesUser() + const { adminMode, toggleAdmin } = useAdminMode(user) return ( - + : null} + /> ) } \ No newline at end of file diff --git a/packages/app-root/src/components/PageHeader.js b/packages/app-root/src/components/PageHeader.js index 2ffc97a7d4a..7fbf01ffbf8 100644 --- a/packages/app-root/src/components/PageHeader.js +++ b/packages/app-root/src/components/PageHeader.js @@ -2,6 +2,7 @@ import ZooHeader from '@zooniverse/react-components/ZooHeader' import { + useAdminMode, usePanoptesUser, useUnreadMessages, useUnreadNotifications @@ -11,9 +12,11 @@ export default function PageHeader() { const { data: user } = usePanoptesUser() const { data: unreadMessages }= useUnreadMessages(user) const { data: unreadNotifications }= useUnreadNotifications(user) + const { adminMode, toggleAdmin } = useAdminMode(user) return ( { + document.body.style.border = '' + document.body.style.borderImage = '' + } + }, [adminMode]) + + function toggleAdmin() { + let newAdminState = !adminState + setAdminState(newAdminState) + if (newAdminState) { + localStorage?.setItem('adminFlag', true) + } else { + localStorage?.removeItem('adminFlag') + } + } + + return { adminMode, toggleAdmin } +} \ No newline at end of file diff --git a/packages/app-root/src/hooks/usePanoptesUser.js b/packages/app-root/src/hooks/usePanoptesUser.js index d2998ba4ad4..fb550c2595a 100644 --- a/packages/app-root/src/hooks/usePanoptesUser.js +++ b/packages/app-root/src/hooks/usePanoptesUser.js @@ -29,7 +29,13 @@ async function fetchPanoptesUser() { return await auth.checkCurrent() } -let user +const isBrowser = typeof window !== 'undefined' +const localStorage = isBrowser ? window.localStorage : null +const storedUserJSON = localStorage?.getItem('panoptes-user') +let user = storedUserJSON && JSON.parse(storedUserJSON) +if (user === null) { + user = undefined +} export default function usePanoptesUser() { const [error, setError] = useState(null) @@ -39,19 +45,14 @@ export default function usePanoptesUser() { async function checkUserSession() { setLoading(true) try { - const { - admin, - avatar_src, - display_name, - id, - login, - } = await fetchPanoptesUser() - user = { - admin, - avatar_src, - display_name, - id, - login, + const panoptesUser = await fetchPanoptesUser() + if (panoptesUser) { + const { admin, avatar_src, display_name, id, login } = panoptesUser + user = { admin, avatar_src, display_name, id, login } + localStorage?.setItem('panoptes-user', JSON.stringify(user)) + } else { + user = undefined + localStorage?.removeItem('panoptes-user') } } catch (error) { setError(error)