fix: show new note button only dependent on guest access

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2023-10-07 12:01:31 +02:00 committed by Philip Molares
parent 170977baa9
commit 8e10fb6804
4 changed files with 32 additions and 5 deletions

View file

@ -555,7 +555,8 @@
"signOut": "Sign Out", "signOut": "Sign Out",
"logoutFailed": "There was an error logging you out.\nClose your browser window to ensure session data is removed.", "logoutFailed": "There was an error logging you out.\nClose your browser window to ensure session data is removed.",
"guest": { "guest": {
"title": "Continue as guest" "title": "Continue as guest",
"noteCreationDisabled": "Guest note creation is disabled on this instance. Please log in to create a note."
}, },
"auth": { "auth": {
"email": "Email", "email": "Email",

View file

@ -11,6 +11,9 @@ import { useRouter } from 'next/navigation'
import React, { useCallback } from 'react' import React, { useCallback } from 'react'
import { FileEarmarkPlus as IconPlus } from 'react-bootstrap-icons' import { FileEarmarkPlus as IconPlus } from 'react-bootstrap-icons'
import { Trans } from 'react-i18next' import { Trans } from 'react-i18next'
import { useFrontendConfig } from '../frontend-config-context/use-frontend-config'
import { GuestAccessLevel } from '../../../api/config/types'
import { useIsLoggedIn } from '../../../hooks/common/use-is-logged-in'
/** /**
* Links to the "new note" endpoint * Links to the "new note" endpoint
@ -18,6 +21,9 @@ import { Trans } from 'react-i18next'
export const NewNoteButton: React.FC = () => { export const NewNoteButton: React.FC = () => {
const { showErrorNotification } = useUiNotifications() const { showErrorNotification } = useUiNotifications()
const router = useRouter() const router = useRouter()
const guestAccessLevel = useFrontendConfig().guestAccess
const isLoggedIn = useIsLoggedIn()
const createNewNoteAndRedirect = useCallback((): void => { const createNewNoteAndRedirect = useCallback((): void => {
createNote('') createNote('')
.then((note) => { .then((note) => {
@ -28,6 +34,10 @@ export const NewNoteButton: React.FC = () => {
}) })
}, [router, showErrorNotification]) }, [router, showErrorNotification])
if (!isLoggedIn && guestAccessLevel !== GuestAccessLevel.CREATE) {
return null
}
return ( return (
<IconButton <IconButton
{...cypressId('new-note-button')} {...cypressId('new-note-button')}

View file

@ -11,6 +11,7 @@ import { HistoryButton } from '../../layout/app-bar/app-bar-elements/help-dropdo
import { useFrontendConfig } from '../../common/frontend-config-context/use-frontend-config' import { useFrontendConfig } from '../../common/frontend-config-context/use-frontend-config'
import { Trans, useTranslation } from 'react-i18next' import { Trans, useTranslation } from 'react-i18next'
import { GuestAccessLevel } from '../../../api/config/types' import { GuestAccessLevel } from '../../../api/config/types'
import { ShowIf } from '../../common/show-if/show-if'
/** /**
* Renders the card with the options for not logged-in users. * Renders the card with the options for not logged-in users.
@ -20,10 +21,6 @@ export const GuestCard: React.FC = () => {
useTranslation() useTranslation()
if (guestAccessLevel === GuestAccessLevel.DENY) {
return null
}
return ( return (
<Card> <Card>
<Card.Body> <Card.Body>
@ -34,6 +31,11 @@ export const GuestCard: React.FC = () => {
<NewNoteButton /> <NewNoteButton />
<HistoryButton /> <HistoryButton />
</div> </div>
<ShowIf condition={guestAccessLevel !== GuestAccessLevel.CREATE}>
<div className={'text-muted mt-2 small'}>
<Trans i18nKey={'login.guest.noteCreationDisabled'} />
</div>
</ShowIf>
</Card.Body> </Card.Body>
</Card> </Card>
) )

View file

@ -0,0 +1,14 @@
/*
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useApplicationState } from './use-application-state'
/**
* Hook to check if currently a user is logged in.
* @return True, if a user is logged in. False otherwise.
*/
export const useIsLoggedIn = () => {
return useApplicationState((state) => !!state.user)
}