mirror of
https://github.com/Athou/commafeed.git
synced 2026-11-20 10:43:37 +00:00
only allow one entry context menu to be open at a time (#2393)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Trans } from "@lingui/react/macro"
|
import { Trans } from "@lingui/react/macro"
|
||||||
import { Box } from "@mantine/core"
|
import { Box } from "@mantine/core"
|
||||||
import { openModal } from "@mantine/modals"
|
import { openModal } from "@mantine/modals"
|
||||||
import { useEffect } from "react"
|
import { useEffect, useState } from "react"
|
||||||
import InfiniteScroll from "react-infinite-scroller"
|
import InfiniteScroll from "react-infinite-scroller"
|
||||||
import { throttle } from "throttle-debounce"
|
import { throttle } from "throttle-debounce"
|
||||||
import { Constants } from "@/app/constants"
|
import { Constants } from "@/app/constants"
|
||||||
@@ -78,6 +78,23 @@ export function FeedEntries() {
|
|||||||
|
|
||||||
const swipedLeft = async (entry: ExpendableEntry) => await dispatch(markEntry({ entry, read: !entry.read }))
|
const swipedLeft = async (entry: ExpendableEntry) => await dispatch(markEntry({ entry, read: !entry.read }))
|
||||||
|
|
||||||
|
// only one context menu can be opened at a time
|
||||||
|
const [contextMenuEntryId, setContextMenuEntryId] = useState<string>()
|
||||||
|
const contextMenuChanged = (entry: ExpendableEntry, opened: boolean) =>
|
||||||
|
setContextMenuEntryId(current => {
|
||||||
|
if (opened) return entry.id
|
||||||
|
return current === entry.id ? undefined : current
|
||||||
|
})
|
||||||
|
|
||||||
|
// close context menu on scroll
|
||||||
|
useEffect(() => {
|
||||||
|
if (!contextMenuEntryId) return
|
||||||
|
|
||||||
|
const listener = throttle(100, () => setContextMenuEntryId(undefined))
|
||||||
|
window.addEventListener("scroll", listener)
|
||||||
|
return () => window.removeEventListener("scroll", listener)
|
||||||
|
}, [contextMenuEntryId])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const listener = throttle(100, () => {
|
const listener = throttle(100, () => {
|
||||||
if (viewMode !== "expanded") return
|
if (viewMode !== "expanded") return
|
||||||
@@ -287,6 +304,8 @@ export function FeedEntries() {
|
|||||||
onHeaderClick={event => headerClicked(entry, event)}
|
onHeaderClick={event => headerClicked(entry, event)}
|
||||||
onBodyClick={() => bodyClicked(entry)}
|
onBodyClick={() => bodyClicked(entry)}
|
||||||
onSwipedLeft={async () => await swipedLeft(entry)}
|
onSwipedLeft={async () => await swipedLeft(entry)}
|
||||||
|
contextMenuOpened={entry.id === contextMenuEntryId}
|
||||||
|
onContextMenuChange={opened => contextMenuChanged(entry, opened)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</InfiniteScroll>
|
</InfiniteScroll>
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ interface FeedEntryProps {
|
|||||||
onHeaderClick: (e: React.MouseEvent) => void
|
onHeaderClick: (e: React.MouseEvent) => void
|
||||||
onBodyClick: (e: React.MouseEvent) => void
|
onBodyClick: (e: React.MouseEvent) => void
|
||||||
onSwipedLeft: () => void
|
onSwipedLeft: () => void
|
||||||
|
contextMenuOpened: boolean
|
||||||
|
onContextMenuChange: (opened: boolean) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const useStyles = tss
|
const useStyles = tss
|
||||||
@@ -170,7 +172,11 @@ export function FeedEntry(props: Readonly<FeedEntryProps>) {
|
|||||||
"show-selection-indicator": props.showSelectionIndicator,
|
"show-selection-indicator": props.showSelectionIndicator,
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<FeedEntryContextMenu entry={props.entry}>
|
<FeedEntryContextMenu
|
||||||
|
entry={props.entry}
|
||||||
|
opened={props.contextMenuOpened}
|
||||||
|
onChange={props.onContextMenuChange}
|
||||||
|
>
|
||||||
<a
|
<a
|
||||||
className={classes.headerLink}
|
className={classes.headerLink}
|
||||||
href={props.entry.url}
|
href={props.entry.url}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { Trans } from "@lingui/react/macro"
|
import { Trans } from "@lingui/react/macro"
|
||||||
import { Menu } from "@mantine/core"
|
import { Menu } from "@mantine/core"
|
||||||
import { type ReactElement, useEffect, useState } from "react"
|
import type { ReactElement } from "react"
|
||||||
import { TbArrowBarToDown, TbExternalLink, TbMail, TbMailOpened, TbRss, TbStar, TbStarOff } from "react-icons/tb"
|
import { TbArrowBarToDown, TbExternalLink, TbMail, TbMailOpened, TbRss, TbStar, TbStarOff } from "react-icons/tb"
|
||||||
import { throttle } from "throttle-debounce"
|
|
||||||
import { markEntriesUpToEntry, markEntry, starEntry } from "@/app/entries/thunks"
|
import { markEntriesUpToEntry, markEntry, starEntry } from "@/app/entries/thunks"
|
||||||
import { redirectToFeed } from "@/app/redirect/thunks"
|
import { redirectToFeed } from "@/app/redirect/thunks"
|
||||||
import { useAppDispatch, useAppSelector } from "@/app/store"
|
import { useAppDispatch, useAppSelector } from "@/app/store"
|
||||||
@@ -12,6 +11,8 @@ import { useBrowserExtension } from "@/hooks/useBrowserExtension"
|
|||||||
|
|
||||||
interface FeedEntryContextMenuProps {
|
interface FeedEntryContextMenuProps {
|
||||||
entry: Entry
|
entry: Entry
|
||||||
|
opened: boolean
|
||||||
|
onChange: (opened: boolean) => void
|
||||||
// element that opens the menu when right-clicked
|
// element that opens the menu when right-clicked
|
||||||
children: ReactElement
|
children: ReactElement
|
||||||
}
|
}
|
||||||
@@ -19,23 +20,13 @@ interface FeedEntryContextMenuProps {
|
|||||||
const iconSize = 16
|
const iconSize = 16
|
||||||
|
|
||||||
export function FeedEntryContextMenu(props: Readonly<FeedEntryContextMenuProps>) {
|
export function FeedEntryContextMenu(props: Readonly<FeedEntryContextMenuProps>) {
|
||||||
const [opened, setOpened] = useState(false)
|
|
||||||
const customContextMenu = useAppSelector(state => state.user.settings?.customContextMenu)
|
const customContextMenu = useAppSelector(state => state.user.settings?.customContextMenu)
|
||||||
const sourceType = useAppSelector(state => state.entries.source.type)
|
const sourceType = useAppSelector(state => state.entries.source.type)
|
||||||
const dispatch = useAppDispatch()
|
const dispatch = useAppDispatch()
|
||||||
const { openLinkInBackgroundTab } = useBrowserExtension()
|
const { openLinkInBackgroundTab } = useBrowserExtension()
|
||||||
|
|
||||||
// close context menu on scroll
|
|
||||||
useEffect(() => {
|
|
||||||
if (!opened) return
|
|
||||||
|
|
||||||
const listener = throttle(100, () => setOpened(false))
|
|
||||||
window.addEventListener("scroll", listener)
|
|
||||||
return () => window.removeEventListener("scroll", listener)
|
|
||||||
}, [opened])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Menu position="bottom-start" opened={opened} onChange={setOpened}>
|
<Menu position="bottom-start" opened={props.opened} onChange={props.onChange}>
|
||||||
<Menu.ContextMenu disabled={!customContextMenu}>{props.children}</Menu.ContextMenu>
|
<Menu.ContextMenu disabled={!customContextMenu}>{props.children}</Menu.ContextMenu>
|
||||||
<Menu.Dropdown>
|
<Menu.Dropdown>
|
||||||
<Menu.Item
|
<Menu.Item
|
||||||
|
|||||||
Reference in New Issue
Block a user