refactor: simplify useSelectedView hook and update selectTimeline function to use target parameter for event dispatching
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { FeedViewType } from "@follow/constants"
|
||||
import { EventBus } from "@follow/utils/src/event-bus"
|
||||
import * as Haptics from "expo-haptics"
|
||||
import { useCallback, useEffect, useMemo, useRef } from "react"
|
||||
import { useCallback, useEffect, useId, useMemo, useRef } from "react"
|
||||
import type { StyleProp, ViewStyle } from "react-native"
|
||||
import { Animated, StyleSheet } from "react-native"
|
||||
import PagerView from "react-native-pager-view"
|
||||
@@ -33,13 +33,14 @@ export function PagerList({
|
||||
|
||||
const pagerRef = useRef<PagerView>(null)
|
||||
|
||||
const rid = useId()
|
||||
useEffect(() => {
|
||||
return EventBus.subscribe("SELECT_TIMELINE", (data) => {
|
||||
if (data.manual) {
|
||||
if (data.target !== rid) {
|
||||
pagerRef.current?.setPage(activeViews.findIndex((view) => view.view === data.view.viewId))
|
||||
}
|
||||
})
|
||||
}, [activeViews, pagerRef])
|
||||
}, [activeViews, pagerRef, rid])
|
||||
const userInitiatedDragRef = useSharedValue(false)
|
||||
|
||||
const pageScrollHandler = useCallback(
|
||||
@@ -68,11 +69,11 @@ export function PagerList({
|
||||
}
|
||||
|
||||
if (targetIndex !== activeViewIndex) {
|
||||
selectTimeline({ type: "view", viewId: activeViews[targetIndex]!.view }, false)
|
||||
selectTimeline({ type: "view", viewId: activeViews[targetIndex]!.view }, rid)
|
||||
userInitiatedDragRef.value = false
|
||||
}
|
||||
},
|
||||
[activeViewIndex, activeViews, userInitiatedDragRef],
|
||||
[activeViewIndex, activeViews, rid, userInitiatedDragRef],
|
||||
)
|
||||
|
||||
return (
|
||||
|
||||
@@ -2,6 +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 { selectAtom } from "jotai/utils"
|
||||
import { createContext, useCallback, useContext, useMemo, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
@@ -82,16 +83,7 @@ export const useEntryListContext = () => {
|
||||
}
|
||||
|
||||
export function useSelectedView() {
|
||||
return useAtomValue(
|
||||
useMemo(
|
||||
() =>
|
||||
atom((get) => {
|
||||
const selectedTimeline = get(selectedTimelineAtom)
|
||||
return selectedTimeline.viewId
|
||||
}),
|
||||
[],
|
||||
),
|
||||
)
|
||||
return useAtomValue(useMemo(() => selectAtom(selectedTimelineAtom, (state) => state.viewId), []))
|
||||
}
|
||||
|
||||
export function useSelectedFeed(): SelectedTimeline | SelectedFeed
|
||||
@@ -167,16 +159,16 @@ declare module "@follow/utils/src/event-bus" {
|
||||
export interface CustomEvent {
|
||||
SELECT_TIMELINE: {
|
||||
view: SelectedTimeline
|
||||
manual: boolean
|
||||
target: string | undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const selectTimeline = (state: SelectedTimeline, manual = true) => {
|
||||
export const selectTimeline = (state: SelectedTimeline, target?: string) => {
|
||||
jotaiStore.set(selectedTimelineAtom, state)
|
||||
EventBus.dispatch("SELECT_TIMELINE", {
|
||||
view: state,
|
||||
manual,
|
||||
target,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user