From 4bbfb16755d9520943efa83f2441a08738967c50 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 10 Feb 2025 19:10:28 +0800 Subject: [PATCH] feat: inbox switch button --- .../renderer/src/hooks/biz/useTimelineList.ts | 2 +- .../src/modules/feed-column/index.tsx | 190 ++-------------- .../src/modules/feed-column/list.desktop.tsx | 32 +-- .../src/modules/feed-column/timeline-list.tsx | 22 ++ .../feed-column/timeline-switch-button.tsx | 214 ++++++++++++++++++ 5 files changed, 259 insertions(+), 201 deletions(-) create mode 100644 apps/renderer/src/modules/feed-column/timeline-list.tsx create mode 100644 apps/renderer/src/modules/feed-column/timeline-switch-button.tsx diff --git a/apps/renderer/src/hooks/biz/useTimelineList.ts b/apps/renderer/src/hooks/biz/useTimelineList.ts index 02d6ec2d7..a55a4e940 100644 --- a/apps/renderer/src/hooks/biz/useTimelineList.ts +++ b/apps/renderer/src/hooks/biz/useTimelineList.ts @@ -6,7 +6,7 @@ export const useTimelineList = () => { return [ ...[0, 1, 2, 3].map((view) => `view-${view}`), - ...lists.map((list) => `list-${list.listId}`), ...inboxes.map((inbox) => `inbox-${inbox.inboxId}`), + ...lists.map((list) => `list-${list.listId}`), ] } diff --git a/apps/renderer/src/modules/feed-column/index.tsx b/apps/renderer/src/modules/feed-column/index.tsx index dc85333a0..06339f981 100644 --- a/apps/renderer/src/modules/feed-column/index.tsx +++ b/apps/renderer/src/modules/feed-column/index.tsx @@ -1,50 +1,33 @@ -import { useDroppable } from "@dnd-kit/core" import { ActionButton } from "@follow/components/ui/button/index.js" import { RootPortal } from "@follow/components/ui/portal/index.js" import { ScrollArea } from "@follow/components/ui/scroll-area/ScrollArea.js" -import type { FeedViewType } from "@follow/constants" -import { Routes, views } from "@follow/constants" +import { Routes } from "@follow/constants" import { useTypeScriptHappyCallback } from "@follow/hooks" import { useRegisterGlobalContext } from "@follow/shared/bridge" -import { nextFrame } from "@follow/utils/dom" import { clamp, cn } from "@follow/utils/utils" import { useWheel } from "@use-gesture/react" import { AnimatePresence, m } from "framer-motion" import { Lethargy } from "lethargy" import type { FC, PropsWithChildren } from "react" -import { startTransition, useCallback, useLayoutEffect, useRef, useState } from "react" +import { useCallback, useLayoutEffect, useRef, useState } from "react" import { isHotkeyPressed, useHotkeys } from "react-hotkeys-hook" -import { useTranslation } from "react-i18next" -import { useShowContextMenu } from "~/atoms/context-menu" -import { getMainContainerElement, useRootContainerElement } from "~/atoms/dom" +import { useRootContainerElement } from "~/atoms/dom" import { useUISettingKey } from "~/atoms/settings/ui" import { setFeedColumnShow, useFeedColumnShow } from "~/atoms/sidebar" -import { - HotKeyScopeMap, - isElectronBuild, - ROUTE_TIMELINE_OF_INBOX, - ROUTE_TIMELINE_OF_LIST, - ROUTE_TIMELINE_OF_VIEW, -} from "~/constants" +import { HotKeyScopeMap, isElectronBuild } from "~/constants" import { shortcuts } from "~/constants/shortcuts" -import { useListActions } from "~/hooks/biz/useFeedActions" import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry" import { useReduceMotion } from "~/hooks/biz/useReduceMotion" import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { useTimelineList } from "~/hooks/biz/useTimelineList" -import { useContextMenu } from "~/hooks/common" -import { useListById } from "~/store/list" -import { subscriptionActions } from "~/store/subscription" -import { useFeedUnreadStore } from "~/store/unread" -import { useUnreadByView } from "~/store/unread/hooks" import { WindowUnderBlur } from "../../components/ui/background" -import { FeedIcon } from "../feed/feed-icon" import { getSelectedFeedIds, resetSelectedFeedIds, setSelectedFeedIds } from "./atom" import { FeedColumnHeader } from "./header" import { useShouldFreeUpSpace } from "./hook" -import { FeedList } from "./list" +import TimelineList from "./timeline-list" +import TimelineSwitchButton from "./timeline-switch-button" const lethargy = new Lethargy() @@ -72,10 +55,7 @@ export function FeedColumn({ children, className }: PropsWithChildren<{ classNam view: s.view, listId: s.listId, })) - const { timelineId, listId } = routeParams - let { view } = routeParams - const list = useListById(listId) - view = list?.view ?? view ?? 0 + const { timelineId } = routeParams const navigateBackHome = useBackHome(timelineId) const setActive = useCallback( (args: string | ((prev: string | undefined, index: number) => string)) => { @@ -191,10 +171,10 @@ export function FeedColumn({ children, className }: PropsWithChildren<{ classNam } }, [])} > - - {views.map((item, index) => ( -
- + + {timelineList.map((timelineId) => ( +
+
))}
@@ -205,143 +185,13 @@ export function FeedColumn({ children, className }: PropsWithChildren<{ classNam ) } -const TimelineSwitchButton = ({ timelineId }: { timelineId: string }) => { - const activeTimelineId = useRouteParamsSelector((s) => s.timelineId) - const isActive = activeTimelineId === timelineId - const navigate = useNavigateEntry() - const setActive = useCallback(() => { - navigate({ - timelineId, - feedId: null, - entryId: null, - }) - resetSelectedFeedIds() - }, [navigate, timelineId]) - - if (timelineId.startsWith(ROUTE_TIMELINE_OF_VIEW)) { - const id = Number.parseInt(timelineId.slice(ROUTE_TIMELINE_OF_VIEW.length), 10) as FeedViewType - return - } else if (timelineId.startsWith(ROUTE_TIMELINE_OF_LIST)) { - const id = timelineId.slice(ROUTE_TIMELINE_OF_LIST.length) - return - } else if (timelineId.startsWith(ROUTE_TIMELINE_OF_INBOX)) { - return null // TODO - } -} - -const ViewSwitchButton: FC<{ - view: FeedViewType - isActive: boolean - setActive: () => void -}> = ({ view, isActive, setActive }) => { - const unreadByView = useUnreadByView() - const { t } = useTranslation() - const showSidebarUnreadCount = useUISettingKey("sidebarShowUnreadCount") - const item = views.find((item) => item.view === view)! - - const { isOver, setNodeRef } = useDroppable({ - id: `view-${item.name}`, - data: { - category: "", - view: item.view, - }, - }) - - return ( - { - startTransition(() => { - setActive() - }) - e.stopPropagation() - }} - > - {item.icon} - {showSidebarUnreadCount ? ( -
- {unreadByView[view]! > 99 ? 99+ : unreadByView[view]} -
- ) : ( - - )} -
- ) -} - -const ListSwitchButton: FC<{ - listId: string - isActive: boolean - setActive: () => void -}> = ({ listId, isActive, setActive }) => { - const list = useListById(listId) - const listUnread = useFeedUnreadStore((state) => state.data[listId] || 0) - - const handleNavigate = useCallback( - (e: React.MouseEvent) => { - e.stopPropagation() - - setActive() - subscriptionActions.markReadByFeedIds({ - listId, - }) - // focus to main container in order to let keyboard can navigate entry items by arrow keys - nextFrame(() => { - getMainContainerElement()?.focus() - }) - }, - [listId, setActive], - ) - - const items = useListActions({ listId }) - const showContextMenu = useShowContextMenu() - const contextMenuProps = useContextMenu({ - onContextMenu: async (e) => { - await showContextMenu(items, e) - }, - }) - - if (!list) return null - - return ( - - -
- -
-
- ) -} - const SwipeWrapper: FC<{ - active: number + active: string children: React.JSX.Element[] }> = ({ children, active }) => { const reduceMotion = useReduceMotion() + const timelineList = useTimelineList() + const index = timelineList.indexOf(active) const feedColumnWidth = useUISettingKey("feedColWidth") const containerRef = useRef(null) @@ -350,12 +200,12 @@ const SwipeWrapper: FC<{ const [isReady, setIsReady] = useState(false) const [direction, setDirection] = useState<"left" | "right">("right") - const [currentAnimtedActive, setCurrentAnimatedActive] = useState(active) + const [currentAnimtedActive, setCurrentAnimatedActive] = useState(index) useLayoutEffect(() => { const prevActiveIndex = prevActiveIndexRef.current - if (prevActiveIndex !== active) { - if (prevActiveIndex < active) { + if (prevActiveIndex !== index) { + if (prevActiveIndex < index) { setDirection("right") } else { setDirection("left") @@ -363,13 +213,13 @@ const SwipeWrapper: FC<{ } // eslint-disable-next-line @eslint-react/web-api/no-leaked-timeout setTimeout(() => { - setCurrentAnimatedActive(active) + setCurrentAnimatedActive(index) }, 0) if (prevActiveIndexRef.current !== -1) { setIsReady(true) } - prevActiveIndexRef.current = active - }, [active]) + prevActiveIndexRef.current = index + }, [index]) if (reduceMotion) { return
{children[currentAnimtedActive]}
diff --git a/apps/renderer/src/modules/feed-column/list.desktop.tsx b/apps/renderer/src/modules/feed-column/list.desktop.tsx index 55d060c83..3ca47c719 100644 --- a/apps/renderer/src/modules/feed-column/list.desktop.tsx +++ b/apps/renderer/src/modules/feed-column/list.desktop.tsx @@ -2,7 +2,6 @@ import { useDraggable } from "@dnd-kit/core" import { ScrollArea } from "@follow/components/ui/scroll-area/index.js" import { cn, isKeyForMultiSelectPressed } from "@follow/utils/utils" import { forwardRef, memo, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react" -import { useTranslation } from "react-i18next" import Selecto from "react-selecto" import { useEventListener } from "usehooks-ts" @@ -17,36 +16,19 @@ import { } from "./atom" import { DraggableContext } from "./context" import { useShouldFreeUpSpace } from "./hook" -import { - EmptyFeedList, - ListHeader, - StarredItem, - useFeedsGroupedData, - useInboxesGroupedData, - useListsGroupedData, -} from "./list.shared" +import { EmptyFeedList, ListHeader, StarredItem, useFeedsGroupedData } from "./list.shared" import { SortableFeedList } from "./sort-by" const FeedListImpl = forwardRef( ({ className, view }, ref) => { const feedsData = useFeedsGroupedData(view) - const listsData = useListsGroupedData(view) - const inboxesData = useInboxesGroupedData(view) const categoryOpenStateData = useCategoryOpenStateByView(view) - const hasData = - Object.keys(feedsData).length > 0 || - Object.keys(listsData).length > 0 || - Object.keys(inboxesData).length > 0 - - const { t } = useTranslation() + const hasData = Object.keys(feedsData).length > 0 // Data prefetch useAuthQuery(Queries.lists.list()) - const hasListData = Object.keys(listsData).length > 0 - const hasInboxData = Object.keys(inboxesData).length > 0 - const scrollerRef = useRef(null) const selectoRef = useRef(null) const [selectedFeedIds, setSelectedFeedIds] = useSelectedFeedIdsState() @@ -216,16 +198,6 @@ const FeedListImpl = forwardRef
- {(hasListData || hasInboxData) && ( -
- {t("words.feeds")} -
- )} {hasData ? ( + } else if (timelineId.startsWith(ROUTE_TIMELINE_OF_LIST)) { + // const id = timelineId.slice(ROUTE_TIMELINE_OF_LIST.length) + return null + } else if (timelineId.startsWith(ROUTE_TIMELINE_OF_INBOX)) { + // const id = timelineId.slice(ROUTE_TIMELINE_OF_INBOX.length) + return null + } +} diff --git a/apps/renderer/src/modules/feed-column/timeline-switch-button.tsx b/apps/renderer/src/modules/feed-column/timeline-switch-button.tsx new file mode 100644 index 000000000..130886a5d --- /dev/null +++ b/apps/renderer/src/modules/feed-column/timeline-switch-button.tsx @@ -0,0 +1,214 @@ +import { useDroppable } from "@dnd-kit/core" +import { ActionButton } from "@follow/components/ui/button/index.js" +import type { FeedViewType } from "@follow/constants" +import { views } from "@follow/constants" +import { nextFrame } from "@follow/utils/dom" +import { cn } from "@follow/utils/utils" +import type { FC } from "react" +import { startTransition, useCallback } from "react" +import { useTranslation } from "react-i18next" + +import { useShowContextMenu } from "~/atoms/context-menu" +import { getMainContainerElement } from "~/atoms/dom" +import { useUISettingKey } from "~/atoms/settings/ui" +import { + ROUTE_TIMELINE_OF_INBOX, + ROUTE_TIMELINE_OF_LIST, + ROUTE_TIMELINE_OF_VIEW, +} from "~/constants" +import { useListActions } from "~/hooks/biz/useFeedActions" +import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry" +import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" +import { useContextMenu } from "~/hooks/common" +import { useInboxById } from "~/store/inbox" +import { useListById } from "~/store/list" +import { subscriptionActions } from "~/store/subscription" +import { useFeedUnreadStore } from "~/store/unread" +import { useUnreadByView } from "~/store/unread/hooks" + +import { FeedIcon } from "../feed/feed-icon" +import { resetSelectedFeedIds } from "./atom" + +export default function TimelineSwitchButton({ timelineId }: { timelineId: string }) { + const activeTimelineId = useRouteParamsSelector((s) => s.timelineId) + const isActive = activeTimelineId === timelineId + const navigate = useNavigateEntry() + const setActive = useCallback(() => { + navigate({ + timelineId, + feedId: null, + entryId: null, + }) + resetSelectedFeedIds() + }, [navigate, timelineId]) + + if (timelineId.startsWith(ROUTE_TIMELINE_OF_VIEW)) { + const id = Number.parseInt(timelineId.slice(ROUTE_TIMELINE_OF_VIEW.length), 10) as FeedViewType + return + } else if (timelineId.startsWith(ROUTE_TIMELINE_OF_LIST)) { + const id = timelineId.slice(ROUTE_TIMELINE_OF_LIST.length) + return + } else if (timelineId.startsWith(ROUTE_TIMELINE_OF_INBOX)) { + const id = timelineId.slice(ROUTE_TIMELINE_OF_INBOX.length) + return + } +} + +const ViewSwitchButton: FC<{ + view: FeedViewType + isActive: boolean + setActive: () => void +}> = ({ view, isActive, setActive }) => { + const unreadByView = useUnreadByView() + const { t } = useTranslation() + const showSidebarUnreadCount = useUISettingKey("sidebarShowUnreadCount") + const item = views.find((item) => item.view === view)! + + const { isOver, setNodeRef } = useDroppable({ + id: `view-${item.name}`, + data: { + category: "", + view: item.view, + }, + }) + + return ( + { + startTransition(() => { + setActive() + }) + e.stopPropagation() + }} + > + {item.icon} + {showSidebarUnreadCount ? ( +
+ {unreadByView[view]! > 99 ? 99+ : unreadByView[view]} +
+ ) : ( + + )} +
+ ) +} + +const ListSwitchButton: FC<{ + listId: string + isActive: boolean + setActive: () => void +}> = ({ listId, isActive, setActive }) => { + const list = useListById(listId) + const listUnread = useFeedUnreadStore((state) => state.data[listId] || 0) + + const handleNavigate = useCallback( + (e: React.MouseEvent) => { + e.stopPropagation() + + setActive() + subscriptionActions.markReadByFeedIds({ + listId, + }) + // focus to main container in order to let keyboard can navigate entry items by arrow keys + nextFrame(() => { + getMainContainerElement()?.focus() + }) + }, + [listId, setActive], + ) + + const items = useListActions({ listId }) + const showContextMenu = useShowContextMenu() + const contextMenuProps = useContextMenu({ + onContextMenu: async (e) => { + await showContextMenu(items, e) + }, + }) + + if (!list) return null + + return ( + + +
+ +
+
+ ) +} + +const InboxSwitchButton: FC<{ + inboxId: string + isActive: boolean + setActive: () => void +}> = ({ inboxId, isActive, setActive }) => { + const inbox = useInboxById(inboxId) + const inboxUnread = useFeedUnreadStore((state) => state.data[inboxId] || 0) + const showSidebarUnreadCount = useUISettingKey("sidebarShowUnreadCount") + + const handleNavigate = useCallback( + (e: React.MouseEvent) => { + e.stopPropagation() + + setActive() + // focus to main container in order to let keyboard can navigate entry items by arrow keys + nextFrame(() => { + getMainContainerElement()?.focus() + }) + }, + [setActive], + ) + + if (!inbox) return null + + return ( + + + {showSidebarUnreadCount ? ( +
+ {inboxUnread > 99 ? 99+ : inboxUnread} +
+ ) : ( + + )} +
+ ) +}