hedgedoc/frontend/src/components/common/note-loading-boundary/hooks/use-load-note-from-server.ts
Tilman Vatteroth caa53e3556 feat: add patch to add generic types to eventemitter2
EventEmitter2 has types, but they're very basic and not very type safe.
I created this patch, because my improved types haven't been merged into the official package.

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2023-02-09 21:58:41 +01:00

28 lines
1.1 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { getNote } from '../../../../api/notes'
import { useSingleStringUrlParameter } from '../../../../hooks/common/use-single-string-url-parameter'
import { setNoteDataFromServer } from '../../../../redux/note-details/methods'
import { useAsyncFn } from 'react-use'
import type { AsyncState } from 'react-use/lib/useAsyncFn'
/**
* Reads the note id from the current URL, requests the note from the backend and writes it into the global application state.
*
* @return An {@link AsyncState async state} that represents the current state of the loading process.
*/
export const useLoadNoteFromServer = (): [AsyncState<boolean>, () => void] => {
const id = useSingleStringUrlParameter('noteId', undefined)
return useAsyncFn(async (): Promise<boolean> => {
if (id === undefined) {
throw new Error('Invalid id')
}
const noteFromServer = await getNote(id)
setNoteDataFromServer(noteFromServer)
return true
}, [id])
}