The History PR: III - Editor integration (#1158)

* Update history on editor load and title change

Signed-off-by: Erik Michelson <github@erik.michelson.eu>

* Add pinning to history from sidebar

Signed-off-by: Erik Michelson <github@erik.michelson.eu>

* Add CHANGELOG entry

Signed-off-by: Erik Michelson <github@erik.michelson.eu>

* Only update local history entries

Signed-off-by: Erik Michelson <github@erik.michelson.eu>

* Update property names to match backend ones

Signed-off-by: Erik Michelson <github@erik.michelson.eu>

* Show error notification on failure

Signed-off-by: Erik Michelson <github@erik.michelson.eu>

* Apply requested changes from review

- rename use hook to reflect the situation that only local entries will be updated
- extract the update ready check from the hook
- rename vars to make comparison more clear

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2021-05-04 20:35:08 +02:00 committed by GitHub
parent 726b084509
commit 3887de4309
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 6 deletions

View file

@ -0,0 +1,51 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import equal from 'fast-deep-equal'
import { useEffect, useRef } from 'react'
import { useSelector } from 'react-redux'
import { ApplicationState, store } from '../../../redux'
import { useParams } from 'react-router-dom'
import { EditorPagePathParams } from '../editor-page'
import { HistoryEntry, HistoryEntryOrigin } from '../../../redux/history/types'
import { updateLocalHistoryEntry } from '../../../redux/history/methods'
export const useUpdateLocalHistoryEntry = (updateReady: boolean): void => {
const { id } = useParams<EditorPagePathParams>()
const userExists = useSelector((state: ApplicationState) => !!state.user)
const currentNoteTitle = useSelector((state: ApplicationState) => state.noteDetails.noteTitle)
const currentNoteTags = useSelector((state: ApplicationState) => state.noteDetails.frontmatter.tags)
const lastNoteTitle = useRef('')
const lastNoteTags = useRef<string[]>([])
useEffect(() => {
if (!updateReady || userExists) {
return
}
if (currentNoteTitle === lastNoteTitle.current && equal(currentNoteTags, lastNoteTags.current)) {
return
}
const history = store.getState().history
const entry: HistoryEntry = history.find(entry => entry.identifier === id) ?? {
identifier: id,
title: '',
pinStatus: false,
lastVisited: '',
tags: [],
origin: HistoryEntryOrigin.LOCAL
}
if (entry.origin === HistoryEntryOrigin.REMOTE) {
return
}
entry.title = currentNoteTitle
entry.tags = currentNoteTags
entry.lastVisited = new Date().toISOString()
updateLocalHistoryEntry(id, entry)
lastNoteTitle.current = currentNoteTitle
lastNoteTags.current = currentNoteTags
}, [updateReady, id, userExists, currentNoteTitle, currentNoteTags])
}