refactor: replace TimelineSelectorProvider with TimelineHeader in entry list and feed screens; enhance useSelectedFeed for better selector support
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -3,7 +3,7 @@ import { useMemo } from "react"
|
||||
|
||||
import { useSelectedFeed, useSelectedView } from "@/src/modules/screen/atoms"
|
||||
import { PagerList } from "@/src/modules/screen/PageList"
|
||||
import { TimelineSelectorProvider } from "@/src/modules/screen/TimelineSelectorProvider"
|
||||
import { TimelineHeader } from "@/src/modules/screen/TimelineSelectorProvider"
|
||||
import {
|
||||
useEntryIdsByCategory,
|
||||
useEntryIdsByFeedId,
|
||||
@@ -45,11 +45,17 @@ export function EntryList() {
|
||||
}, [selectedFeed])
|
||||
if (!Content) return null
|
||||
|
||||
return <TimelineSelectorProvider>{Content}</TimelineSelectorProvider>
|
||||
return (
|
||||
<>
|
||||
<TimelineHeader />
|
||||
{Content}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function ViewEntryList({ viewId, active }: { viewId: FeedViewType; active: boolean }) {
|
||||
const entryIds = useEntryIdsByView(viewId)
|
||||
|
||||
return <EntryListSelector entryIds={entryIds} viewId={viewId} active={active} />
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import type { FeedViewType } from "@follow/constants"
|
||||
import { EventBus } from "@follow/utils/src/event-bus"
|
||||
import * as Haptics from "expo-haptics"
|
||||
import { useEffect, useMemo } from "react"
|
||||
import { useCallback, useEffect, useMemo, useRef } from "react"
|
||||
import type { StyleProp, ViewStyle } from "react-native"
|
||||
import { Animated, StyleSheet } from "react-native"
|
||||
import PagerView from "react-native-pager-view"
|
||||
import { useSharedValue } from "react-native-reanimated"
|
||||
|
||||
import { selectTimeline, useSelectedFeed } from "@/src/modules/screen/atoms"
|
||||
import { useViewWithSubscription } from "@/src/store/subscription/hooks"
|
||||
|
||||
import { setHorizontalScrolling } from "./atoms"
|
||||
import { usePagerView } from "./usePagerView"
|
||||
|
||||
const AnimatedPagerView = Animated.createAnimatedComponent<typeof PagerView>(PagerView)
|
||||
|
||||
@@ -26,14 +26,12 @@ export function PagerList({
|
||||
|
||||
const activeViews = useViewWithSubscription()
|
||||
|
||||
const viewIdIndex = activeViews.findIndex((view) => view.view === viewId)
|
||||
const { page, pagerRef, ...rest } = usePagerView({
|
||||
initialPage: viewIdIndex,
|
||||
onIndexChange: (index) => {
|
||||
if (index === undefined) return
|
||||
selectTimeline({ type: "view", viewId: activeViews[index]!.view }, false)
|
||||
},
|
||||
})
|
||||
const activeViewIndex = useMemo(
|
||||
() => activeViews.findIndex((view) => view.view === viewId),
|
||||
[activeViews, viewId],
|
||||
)
|
||||
|
||||
const pagerRef = useRef<PagerView>(null)
|
||||
|
||||
useEffect(() => {
|
||||
return EventBus.subscribe("SELECT_TIMELINE", (data) => {
|
||||
@@ -42,19 +40,54 @@ export function PagerList({
|
||||
}
|
||||
})
|
||||
}, [activeViews, pagerRef])
|
||||
const userInitiatedDragRef = useSharedValue(false)
|
||||
|
||||
const pageScrollHandler = useCallback(
|
||||
(e: {
|
||||
nativeEvent: {
|
||||
position: number
|
||||
offset: number
|
||||
}
|
||||
}) => {
|
||||
const { position, offset } = e.nativeEvent
|
||||
|
||||
if (!userInitiatedDragRef.value) {
|
||||
return
|
||||
}
|
||||
|
||||
let targetIndex: number
|
||||
|
||||
if (offset > 0.6 && position < activeViews.length - 1) {
|
||||
targetIndex = position + 1
|
||||
} else if (offset < 0.4 && position === activeViewIndex - 1) {
|
||||
targetIndex = position
|
||||
} else if (offset === 0 && position === activeViewIndex) {
|
||||
targetIndex = activeViewIndex
|
||||
} else {
|
||||
targetIndex = activeViewIndex
|
||||
}
|
||||
|
||||
if (targetIndex !== activeViewIndex) {
|
||||
selectTimeline({ type: "view", viewId: activeViews[targetIndex]!.view }, false)
|
||||
userInitiatedDragRef.value = false
|
||||
}
|
||||
},
|
||||
[activeViewIndex, activeViews, userInitiatedDragRef],
|
||||
)
|
||||
|
||||
return (
|
||||
<AnimatedPagerView
|
||||
testID="pager-view"
|
||||
ref={pagerRef}
|
||||
style={[styles.PagerView, style]}
|
||||
initialPage={page}
|
||||
initialPage={activeViewIndex}
|
||||
layoutDirection="ltr"
|
||||
overdrag
|
||||
onPageScroll={rest.onPageScroll}
|
||||
onPageSelected={rest.onPageSelected}
|
||||
onPageScroll={pageScrollHandler}
|
||||
onPageScrollStateChanged={(e) => {
|
||||
rest.onPageScrollStateChanged(e)
|
||||
if (e.nativeEvent.pageScrollState === "dragging") {
|
||||
userInitiatedDragRef.value = true
|
||||
}
|
||||
setHorizontalScrolling(e.nativeEvent.pageScrollState !== "idle")
|
||||
if (e.nativeEvent.pageScrollState === "settling") {
|
||||
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
|
||||
@@ -64,8 +97,8 @@ export function PagerList({
|
||||
orientation="horizontal"
|
||||
>
|
||||
{useMemo(
|
||||
() => activeViews.map((view, index) => renderItem(view.view, page === index)),
|
||||
[activeViews, page, renderItem],
|
||||
() => activeViews.map((view, index) => renderItem(view.view, index === activeViewIndex)),
|
||||
[activeViews, activeViewIndex, renderItem],
|
||||
)}
|
||||
</AnimatedPagerView>
|
||||
)
|
||||
|
||||
@@ -15,13 +15,7 @@ import { TimelineViewSelector } from "@/src/modules/screen/TimelineViewSelector"
|
||||
|
||||
import { useEntryListContext, useFetchEntriesControls, useSelectedFeedTitle } from "./atoms"
|
||||
|
||||
export function TimelineSelectorProvider({
|
||||
children,
|
||||
feedId,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
feedId?: string
|
||||
}) {
|
||||
export function TimelineHeader({ feedId }: { feedId?: string }) {
|
||||
const viewTitle = useSelectedFeedTitle()
|
||||
const screenType = useEntryListContext().type
|
||||
|
||||
@@ -32,43 +26,40 @@ export function TimelineSelectorProvider({
|
||||
const { isFetching } = useFetchEntriesControls()
|
||||
|
||||
return (
|
||||
<>
|
||||
<NavigationBlurEffectHeader
|
||||
title={viewTitle}
|
||||
isLoading={(isFeed || isTimeline) && isFetching}
|
||||
headerLeft={useMemo(
|
||||
() =>
|
||||
isTimeline || isSubscriptions
|
||||
? () => <HomeLeftAction />
|
||||
: () => <DefaultHeaderBackButton canDismiss={false} canGoBack={true} />,
|
||||
[isTimeline, isSubscriptions],
|
||||
)}
|
||||
headerRight={useMemo(() => {
|
||||
const Component = (() => {
|
||||
if (isTimeline || isFeed)
|
||||
return () => (
|
||||
<ActionGroup>
|
||||
<UnreadOnlyActionButton />
|
||||
<MarkAllAsReadActionButton />
|
||||
<FeedShareActionButton feedId={feedId} />
|
||||
</ActionGroup>
|
||||
)
|
||||
if (isSubscriptions)
|
||||
return () => (
|
||||
<ActionGroup>
|
||||
<MarkAllAsReadActionButton />
|
||||
</ActionGroup>
|
||||
)
|
||||
})()
|
||||
<NavigationBlurEffectHeader
|
||||
title={viewTitle}
|
||||
isLoading={(isFeed || isTimeline) && isFetching}
|
||||
headerLeft={useMemo(
|
||||
() =>
|
||||
isTimeline || isSubscriptions
|
||||
? () => <HomeLeftAction />
|
||||
: () => <DefaultHeaderBackButton canDismiss={false} canGoBack={true} />,
|
||||
[isTimeline, isSubscriptions],
|
||||
)}
|
||||
headerRight={useMemo(() => {
|
||||
const Component = (() => {
|
||||
if (isTimeline || isFeed)
|
||||
return () => (
|
||||
<ActionGroup>
|
||||
<UnreadOnlyActionButton />
|
||||
<MarkAllAsReadActionButton />
|
||||
<FeedShareActionButton feedId={feedId} />
|
||||
</ActionGroup>
|
||||
)
|
||||
if (isSubscriptions)
|
||||
return () => (
|
||||
<ActionGroup>
|
||||
<MarkAllAsReadActionButton />
|
||||
</ActionGroup>
|
||||
)
|
||||
})()
|
||||
|
||||
if (Component)
|
||||
return () => <View className="flex-row items-center justify-end">{Component()}</View>
|
||||
return
|
||||
}, [isFeed, isTimeline, isSubscriptions, feedId])}
|
||||
headerHideableBottom={isTimeline || isSubscriptions ? TimelineViewSelector : undefined}
|
||||
headerHideableBottomHeight={TIMELINE_VIEW_SELECTOR_HEIGHT}
|
||||
/>
|
||||
{children}
|
||||
</>
|
||||
if (Component)
|
||||
return () => <View className="flex-row items-center justify-end">{Component()}</View>
|
||||
return
|
||||
}, [isFeed, isTimeline, isSubscriptions, feedId])}
|
||||
headerHideableBottom={isTimeline || isSubscriptions ? TimelineViewSelector : undefined}
|
||||
headerHideableBottomHeight={TIMELINE_VIEW_SELECTOR_HEIGHT}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { FeedViewType } from "@follow/constants"
|
||||
import { jotaiStore } from "@follow/utils"
|
||||
import { EventBus } from "@follow/utils/src/event-bus"
|
||||
import { atom, useAtom, useAtomValue, useSetAtom } from "jotai"
|
||||
import { createContext, useCallback, useContext, useMemo } from "react"
|
||||
import { createContext, useCallback, useContext, useMemo, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { views } from "@/src/constants/views"
|
||||
@@ -124,12 +124,31 @@ export function useSelectedView() {
|
||||
}
|
||||
}
|
||||
|
||||
export function useSelectedFeed() {
|
||||
export function useSelectedFeed(): SelectedTimeline | SelectedFeed
|
||||
export function useSelectedFeed<T>(
|
||||
selector?: (selectedFeed: SelectedTimeline | SelectedFeed) => T,
|
||||
): T | null
|
||||
export function useSelectedFeed<T>(
|
||||
selector?: (selectedFeed: SelectedTimeline | SelectedFeed) => T,
|
||||
) {
|
||||
const entryListContext = useEntryListContext()
|
||||
|
||||
const selectedTimeline = useAtomValue(selectedTimelineAtom)
|
||||
const selectedFeed = useAtomValue(selectedFeedAtom)
|
||||
return entryListContext.type === "feed" ? selectedFeed : selectedTimeline
|
||||
const [stableSelector] = useState(() => selector)
|
||||
return useAtomValue(
|
||||
useMemo(
|
||||
() =>
|
||||
atom((get) => {
|
||||
const selectedTimeline = get(selectedTimelineAtom)
|
||||
const selectedFeed = get(selectedFeedAtom)
|
||||
const result = entryListContext.type === "feed" ? selectedFeed : selectedTimeline
|
||||
if (stableSelector) {
|
||||
return stableSelector(result)
|
||||
}
|
||||
return result
|
||||
}),
|
||||
[entryListContext, stableSelector],
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
export function useFetchEntriesControls() {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { FeedViewType } from "@follow/constants"
|
||||
import type { FlashList } from "@shopify/flash-list"
|
||||
import type { ParseKeys } from "i18next"
|
||||
import { useMemo, useState } from "react"
|
||||
import { memo, useMemo, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Text, View } from "react-native"
|
||||
import { useEventCallback } from "usehooks-ts"
|
||||
@@ -46,7 +46,7 @@ const keyExtractor = (item: string | { category: string; subscriptionIds: string
|
||||
return item.category
|
||||
}
|
||||
|
||||
export const SubscriptionList = ({
|
||||
const SubscriptionListImpl = ({
|
||||
view,
|
||||
active = true,
|
||||
}: {
|
||||
@@ -252,3 +252,5 @@ const StarItem = () => {
|
||||
</GroupedInsetListCard>
|
||||
)
|
||||
}
|
||||
|
||||
export const SubscriptionList = memo(SubscriptionListImpl)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { FeedViewType } from "@follow/constants"
|
||||
import { cn } from "@follow/utils"
|
||||
import { memo, useContext } from "react"
|
||||
import { Text, View } from "react-native"
|
||||
@@ -37,8 +38,9 @@ export const SubscriptionItem = memo(
|
||||
const inGrouped = !!useContext(GroupedContext)
|
||||
const { isLoading } = usePrefetchFeed(id, { enabled: !subscription && !feed })
|
||||
|
||||
const selectedFeed = useSelectedFeed()
|
||||
const view = selectedFeed?.type === "view" ? selectedFeed.viewId : undefined
|
||||
const view = useSelectedFeed((r) => (r && r.type === "feed" ? r.feedId : undefined)) as
|
||||
| FeedViewType
|
||||
| undefined
|
||||
|
||||
const navigation = useNavigation()
|
||||
if (isLoading) {
|
||||
|
||||
@@ -11,7 +11,7 @@ import { BlackBoard2CuteReIcon } from "@/src/icons/black_board_2_cute_re"
|
||||
import type { TabScreenComponent } from "@/src/lib/navigation/bottom-tab/types"
|
||||
import { EntryListContext } from "@/src/modules/screen/atoms"
|
||||
import { PagerList } from "@/src/modules/screen/PageList"
|
||||
import { TimelineSelectorProvider } from "@/src/modules/screen/TimelineSelectorProvider"
|
||||
import { TimelineHeader } from "@/src/modules/screen/TimelineSelectorProvider"
|
||||
import { SubscriptionList } from "@/src/modules/subscription/SubscriptionLists"
|
||||
import { useWhoami } from "@/src/store/user/hooks"
|
||||
|
||||
@@ -22,18 +22,17 @@ export default function Subscriptions() {
|
||||
return (
|
||||
<EntryListContext.Provider value={useMemo(() => ({ type: "subscriptions" }), [])}>
|
||||
<RootSiblingParent>
|
||||
<TimelineSelectorProvider>
|
||||
{whoami ? (
|
||||
<PagerList
|
||||
renderItem={renderItem}
|
||||
style={{
|
||||
backgroundColor: systemGroupedBackground,
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<NoLoginInfo target="subscriptions" />
|
||||
)}
|
||||
</TimelineSelectorProvider>
|
||||
<TimelineHeader />
|
||||
{whoami ? (
|
||||
<PagerList
|
||||
renderItem={renderItem}
|
||||
style={{
|
||||
backgroundColor: systemGroupedBackground,
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<NoLoginInfo target="subscriptions" />
|
||||
)}
|
||||
</RootSiblingParent>
|
||||
</EntryListContext.Provider>
|
||||
)
|
||||
|
||||
@@ -7,7 +7,7 @@ import { BottomTabBarHeightContext } from "@/src/components/layouts/tabbar/conte
|
||||
import type { NavigationControllerView } from "@/src/lib/navigation/types"
|
||||
import { EntryListSelector } from "@/src/modules/entry-list/EntryListSelector"
|
||||
import { EntryListContext, useSelectedView } from "@/src/modules/screen/atoms"
|
||||
import { TimelineSelectorProvider } from "@/src/modules/screen/TimelineSelectorProvider"
|
||||
import { TimelineHeader } from "@/src/modules/screen/TimelineSelectorProvider"
|
||||
import { useCollectionEntryList } from "@/src/store/collection/hooks"
|
||||
import {
|
||||
useEntryIdsByCategory,
|
||||
@@ -46,9 +46,9 @@ export const FeedScreen: NavigationControllerView<{
|
||||
<EntryListContext.Provider value={useMemo(() => ({ type: "feed" }), [])}>
|
||||
<RootSiblingParent>
|
||||
<BottomTabBarHeightContext.Provider value={insets.bottom}>
|
||||
<TimelineSelectorProvider feedId={feed?.id}>
|
||||
<EntryListSelector entryIds={entryIds} viewId={view} />
|
||||
</TimelineSelectorProvider>
|
||||
<TimelineHeader feedId={feed?.id} />
|
||||
|
||||
<EntryListSelector entryIds={entryIds} viewId={view} />
|
||||
</BottomTabBarHeightContext.Provider>
|
||||
</RootSiblingParent>
|
||||
</EntryListContext.Provider>
|
||||
|
||||
Reference in New Issue
Block a user