Files
Folo/apps/renderer/src/modules/entry-column/EntryColumnShortcutHandler.tsx
T
3a85febb6e refactor(monorepo): migrate to monorepo (#1059)
* init

Signed-off-by: Innei <i@innei.in>

* chore: utils import

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

* remove stable router

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

* refactor: components import

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

* chore: auto-fix linting and formatting issues

* minify

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

* update

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

* fix: test

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

* chore: ui component extract

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

* fix: import

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

* re-render

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

* refactor: remove hooks

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

* fix

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

* update

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

* setting

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

* update

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

* refactor: remove icons

Signed-off-by: Innei <i@innei.in>

* typings

Signed-off-by: Innei <i@innei.in>

* feat: add horizontal scroll functionality to TimelineTabs (#1081)

Co-authored-by: xhuajin <xhuajin@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>

* fix: Wrong initial position of tab animation (#1087)

Co-authored-by: hamster1963 <hamster1963@users.noreply.github.com>

* fix: disable auto toggle readability in web

* fix: margin condition in EntryListHeader (#1103)

* chore(i18n): add daily reward system translation (#1102)

Co-authored-by: lc4t <lc4t@users.noreply.github.com>

* fix: deps

Signed-off-by: Innei <i@innei.in>

* fix: type

Signed-off-by: Innei <i@innei.in>

* update

Signed-off-by: Innei <i@innei.in>

* fix

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
Signed-off-by: Innei <tukon479@gmail.com>
Co-authored-by: Innei <Innei@users.noreply.github.com>
Co-authored-by: Huajin <114134574+xhuajin@users.noreply.github.com>
Co-authored-by: xhuajin <xhuajin@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: 仓鼠 <71394853+hamster1963@users.noreply.github.com>
Co-authored-by: hamster1963 <hamster1963@users.noreply.github.com>
Co-authored-by: Konv Suu <2583695112@qq.com>
Co-authored-by: lc4t <11334449+lc4t@users.noreply.github.com>
Co-authored-by: lc4t <lc4t@users.noreply.github.com>
2024-10-24 14:37:43 +08:00

94 lines
2.7 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 type { VirtuosoHandle } from "react-virtuoso"
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[]
virtuosoRef: React.RefObject<VirtuosoHandle>
}> = memo(({ data, refetch, virtuosoRef }) => {
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)
virtuosoRef.current?.scrollIntoView?.({
index: nextIndex,
})
const nextId = data![nextIndex]
navigate({
entryId: nextId,
})
},
{ scopes: HotKeyScopeMap.Home, enabled: enabledArrowKey },
)
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)
virtuosoRef.current?.scrollIntoView?.({
index: nextIndex,
})
const nextId = data![nextIndex]
navigate({
entryId: nextId,
})
},
{ scopes: HotKeyScopeMap.Home, enabled: enabledArrowKey },
)
return null
})