chore: include follow and state

This commit is contained in:
userquin 2023-01-07 18:30:18 +01:00
parent 2a30510127
commit b6eed6b46b

View file

@ -2,22 +2,34 @@ import type { Status } from 'masto'
import { STORAGE_KEY_LAST_SCROLL_POSITION } from '~/constants' import { STORAGE_KEY_LAST_SCROLL_POSITION } from '~/constants'
interface RestoreScroll {
id: string
type: 'status' | 'follow'
}
export default defineNuxtPlugin(() => { export default defineNuxtPlugin(() => {
const lastStatus = useSessionStorage<Record<string, string>>(STORAGE_KEY_LAST_SCROLL_POSITION, {}) const lastStatus = useSessionStorage<Record<string, RestoreScroll>>(STORAGE_KEY_LAST_SCROLL_POSITION, {})
return { return {
provide: { provide: {
preventScrollToTop: (path: string) => {
return !!lastStatus.value[path]
},
restoreScrollPosition: () => { restoreScrollPosition: () => {
const statusId = lastStatus.value[useRoute().fullPath] const restore = lastStatus.value[useRoute().fullPath]
if (statusId) { if (restore) {
const el = document.getElementById(`status-${statusId}`) const el = restore.type === 'status'
? document.getElementById(`status-${restore.id}`)
: document.querySelector(`a[href="${restore.id}"]`)
if (el) if (el)
nextTick().then(() => el?.scrollIntoView()) nextTick().then(() => el?.scrollIntoView())
else else
delete lastStatus.value[useRoute().fullPath] delete lastStatus.value[useRoute().fullPath]
} }
}, },
rememberScrollPosition: (status: Status) => { rememberAccountPosition: (account: string) => {
lastStatus.value[useRoute().fullPath] = status.id lastStatus.value[useRoute().fullPath] = { id: account, type: 'follow' }
},
rememberStatusPosition: (status: Status) => {
lastStatus.value[useRoute().fullPath] = { id: status.id, type: 'status' }
}, },
}, },
} }