mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-06-08 10:22:47 -04:00
nullable note details
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
4fbe813af0
commit
a05b387ee1
58 changed files with 194 additions and 125 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useApplicationState } from './use-application-state'
|
||||
import { useNoteDetails } from './use-note-details'
|
||||
import type { NotePermissions } from '@hedgedoc/commons'
|
||||
import { userIsOwner } from '@hedgedoc/commons'
|
||||
import { useMemo } from 'react'
|
||||
|
@ -15,7 +16,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: NotePermissions = useNoteDetails().permissions
|
||||
|
||||
return useMemo(() => userIsOwner(permissions, me), [permissions, me])
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useApplicationState } from './use-application-state'
|
||||
import { useNoteDetails } from './use-note-details'
|
||||
import type { NotePermissions } from '@hedgedoc/commons'
|
||||
import { userCanEdit } from '@hedgedoc/commons'
|
||||
import { useMemo } from 'react'
|
||||
|
@ -15,7 +16,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: NotePermissions = useNoteDetails().permissions
|
||||
|
||||
return useMemo(() => userCanEdit(permissions, me), [permissions, me])
|
||||
}
|
||||
|
|
17
frontend/src/hooks/common/use-note-details.ts
Normal file
17
frontend/src/hooks/common/use-note-details.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import type { NoteDetails } from '../../redux/note-details/types/note-details'
|
||||
import { useApplicationState } from './use-application-state'
|
||||
|
||||
export const useNoteDetails = (): NoteDetails => {
|
||||
const noteDetails = useApplicationState((state) => state.noteDetails)
|
||||
|
||||
if (noteDetails === null) {
|
||||
throw new Error('No note details in global application state!')
|
||||
}
|
||||
|
||||
return noteDetails
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useApplicationState } from './use-application-state'
|
||||
import { useNoteDetails } from './use-note-details'
|
||||
|
||||
/**
|
||||
* Extracts the markdown content of the current note from the global application state.
|
||||
|
@ -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 useNoteDetails().markdownContent.plain
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useApplicationState } from './use-application-state'
|
||||
import { useNoteDetails } from './use-note-details'
|
||||
import { useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
|
@ -15,7 +15,7 @@ import { useTranslation } from 'react-i18next'
|
|||
export const useNoteTitle = (): string => {
|
||||
const { t } = useTranslation()
|
||||
const untitledNote = useMemo(() => t('editor.untitledNote'), [t])
|
||||
const noteTitle = useApplicationState((state) => state.noteDetails.title)
|
||||
const noteTitle = useNoteDetails().title
|
||||
|
||||
return useMemo(() => (noteTitle === '' ? untitledNote : noteTitle), [noteTitle, untitledNote])
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useFrontendConfig } from '../../components/common/frontend-config-context/use-frontend-config'
|
||||
import { useApplicationState } from './use-application-state'
|
||||
import { useNoteDetails } from './use-note-details'
|
||||
import { useMemo } from 'react'
|
||||
|
||||
/**
|
||||
|
@ -14,19 +14,17 @@ 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.frontmatterRendererInfo.lineOffset)
|
||||
const lines = useNoteDetails().markdownContent.lines
|
||||
const content = useNoteDetails().markdownContent.plain
|
||||
const lineOffset = useNoteDetails().frontmatterRendererInfo.lineOffset
|
||||
|
||||
const trimmedLines = useMemo(() => {
|
||||
if (markdownContent.content.length > maxLength) {
|
||||
return markdownContent.content.slice(0, maxLength).split('\n')
|
||||
if (content.length > maxLength) {
|
||||
return content.slice(0, maxLength).split('\n')
|
||||
} else {
|
||||
return markdownContent.lines
|
||||
return lines
|
||||
}
|
||||
}, [markdownContent, maxLength])
|
||||
}, [content, lines, maxLength])
|
||||
|
||||
return useMemo(() => {
|
||||
return trimmedLines.slice(lineOffset)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue