mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-23 11:37:02 -04:00
fix(frontend): make note details in redux optional
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
118f158ad1
commit
6698450461
50 changed files with 278 additions and 135 deletions
|
@ -4,7 +4,6 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useApplicationState } from './use-application-state'
|
||||
import type { NotePermissions } from '@hedgedoc/commons'
|
||||
import { userIsOwner } from '@hedgedoc/commons'
|
||||
import { useMemo } from 'react'
|
||||
|
||||
|
@ -15,7 +14,7 @@ import { useMemo } from 'react'
|
|||
*/
|
||||
export const useIsOwner = (): boolean => {
|
||||
const me: string | undefined = useApplicationState((state) => state.user?.username)
|
||||
const permissions: NotePermissions = useApplicationState((state) => state.noteDetails.permissions)
|
||||
const permissions = useApplicationState((state) => state.noteDetails?.permissions)
|
||||
|
||||
return useMemo(() => userIsOwner(permissions, me), [permissions, me])
|
||||
return useMemo(() => (permissions === undefined ? false : userIsOwner(permissions, me)), [permissions, me])
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useApplicationState } from './use-application-state'
|
||||
import type { NotePermissions } from '@hedgedoc/commons'
|
||||
import { userCanEdit } from '@hedgedoc/commons'
|
||||
import { useMemo } from 'react'
|
||||
|
||||
|
@ -15,7 +14,7 @@ import { useMemo } from 'react'
|
|||
*/
|
||||
export const useMayEdit = (): boolean => {
|
||||
const me: string | undefined = useApplicationState((state) => state.user?.username)
|
||||
const permissions: NotePermissions = useApplicationState((state) => state.noteDetails.permissions)
|
||||
const permissions = useApplicationState((state) => state.noteDetails?.permissions)
|
||||
|
||||
return useMemo(() => userCanEdit(permissions, me), [permissions, me])
|
||||
return useMemo(() => (!permissions ? false : userCanEdit(permissions, me)), [permissions, me])
|
||||
}
|
||||
|
|
|
@ -11,5 +11,5 @@ import { useApplicationState } from './use-application-state'
|
|||
* @return The markdown content of the note
|
||||
*/
|
||||
export const useNoteMarkdownContent = (): string => {
|
||||
return useApplicationState((state) => state.noteDetails.markdownContent.plain)
|
||||
return useApplicationState((state) => state.noteDetails?.markdownContent.plain ?? '')
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import { useMemo } from 'react'
|
|||
*/
|
||||
export const useNoteTitle = (): string => {
|
||||
const untitledNote = useTranslatedText('editor.untitledNote')
|
||||
const noteTitle = useApplicationState((state) => state.noteDetails.title)
|
||||
const noteTitle = useApplicationState((state) => state.noteDetails?.title)
|
||||
|
||||
return useMemo(() => (noteTitle === '' ? untitledNote : noteTitle), [noteTitle, untitledNote])
|
||||
return useMemo(() => (!noteTitle ? untitledNote : noteTitle), [noteTitle, untitledNote])
|
||||
}
|
||||
|
|
|
@ -14,13 +14,23 @@ import { useMemo } from 'react'
|
|||
*/
|
||||
export const useTrimmedNoteMarkdownContentWithoutFrontmatter = (): string[] => {
|
||||
const maxLength = useFrontendConfig().maxDocumentLength
|
||||
const markdownContent = useApplicationState((state) => ({
|
||||
lines: state.noteDetails.markdownContent.lines,
|
||||
content: state.noteDetails.markdownContent.plain
|
||||
}))
|
||||
const lineOffset = useApplicationState((state) => state.noteDetails.startOfContentLineOffset)
|
||||
const markdownContent = useApplicationState((state) => {
|
||||
const noteDetails = state.noteDetails
|
||||
if (!noteDetails) {
|
||||
return undefined
|
||||
} else {
|
||||
return {
|
||||
lines: noteDetails.markdownContent.lines,
|
||||
content: noteDetails.markdownContent.plain
|
||||
}
|
||||
}
|
||||
})
|
||||
const lineOffset = useApplicationState((state) => state.noteDetails?.startOfContentLineOffset)
|
||||
|
||||
const trimmedLines = useMemo(() => {
|
||||
if (!markdownContent) {
|
||||
return undefined
|
||||
}
|
||||
if (markdownContent.content.length > maxLength) {
|
||||
return markdownContent.content.slice(0, maxLength).split('\n')
|
||||
} else {
|
||||
|
@ -29,6 +39,6 @@ export const useTrimmedNoteMarkdownContentWithoutFrontmatter = (): string[] => {
|
|||
}, [markdownContent, maxLength])
|
||||
|
||||
return useMemo(() => {
|
||||
return trimmedLines.slice(lineOffset)
|
||||
return trimmedLines === undefined || lineOffset === undefined ? [] : trimmedLines.slice(lineOffset)
|
||||
}, [lineOffset, trimmedLines])
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue