Files
Folo/apps/renderer/src/modules/entry-column/EntryColumnShortcutHandler.tsx
T
Innei d98ebaa4f7 refactor: vlist and keep scroll position (#1931)
* init

Signed-off-by: Innei <tukon479@gmail.com>

* feat: grid

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: mark read logci

Signed-off-by: Innei <tukon479@gmail.com>

* chore: remove react-virtuoso

Signed-off-by: Innei <tukon479@gmail.com>

* fix: video

Signed-off-by: Innei <tukon479@gmail.com>

* transition

Signed-off-by: Innei <tukon479@gmail.com>

* fix: image memo loaded status

Signed-off-by: Innei <tukon479@gmail.com>

* fix: sort button

Signed-off-by: Innei <tukon479@gmail.com>

* feat: cjk truncate

Signed-off-by: Innei <tukon479@gmail.com>

---------

Signed-off-by: Innei <tukon479@gmail.com>
2024-11-29 22:51:37 +08:00

89 lines
2.6 KiB
TypeScript

import { useRefValue } from "@follow/hooks"
import type { FC } from "react"
import { memo, useLayoutEffect, useState } from "react"
import { useHotkeys } from "react-hotkeys-hook"
import { useMainContainerElement } from "~/atoms/dom"
import { HotKeyScopeMap } from "~/constants"
import { shortcuts } from "~/constants/shortcuts"
import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
import { useRouteEntryId } from "~/hooks/biz/useRouteParams"
export const EntryColumnShortcutHandler: FC<{
refetch: () => void
data: readonly string[]
handleScrollTo: (index: number) => void
}> = memo(({ data, refetch, handleScrollTo }) => {
const dataRef = useRefValue(data!)
useHotkeys(
shortcuts.entries.refetch.key,
() => {
refetch()
},
{ scopes: HotKeyScopeMap.Home },
)
const currentEntryIdRef = useRefValue(useRouteEntryId())
const navigate = useNavigateEntry()
const $mainContainer = useMainContainerElement()
const [enabledArrowKey, setEnabledArrowKey] = useState(false)
// Enable arrow key navigation shortcuts only when focus is on entryContent or entryList,
// entryList shortcuts should not be triggered in the feed col
useLayoutEffect(() => {
if (!$mainContainer) return
const handler = () => {
const target = document.activeElement
const isFocusIn = $mainContainer.contains(target) || $mainContainer === target
setEnabledArrowKey(isFocusIn)
}
handler()
// NOTE: focusin event will bubble to the document
document.addEventListener("focusin", handler)
return () => {
document.removeEventListener("focusin", handler)
}
}, [$mainContainer])
useHotkeys(
shortcuts.entries.next.key,
() => {
const data = dataRef.current
const currentActiveEntryIndex = data.indexOf(currentEntryIdRef.current || "")
const nextIndex = Math.min(currentActiveEntryIndex + 1, data.length - 1)
handleScrollTo(nextIndex)
const nextId = data![nextIndex]
navigate({
entryId: nextId,
})
},
{ scopes: HotKeyScopeMap.Home, enabled: enabledArrowKey, preventDefault: true },
)
useHotkeys(
shortcuts.entries.previous.key,
() => {
const data = dataRef.current
const currentActiveEntryIndex = data.indexOf(currentEntryIdRef.current || "")
const nextIndex =
currentActiveEntryIndex === -1 ? data.length - 1 : Math.max(0, currentActiveEntryIndex - 1)
handleScrollTo(nextIndex)
const nextId = data![nextIndex]
navigate({
entryId: nextId,
})
},
{ scopes: HotKeyScopeMap.Home, enabled: enabledArrowKey, preventDefault: true },
)
return null
})