Skip to content

Commit

Permalink
refactor: separate EntryList component
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Jan 23, 2025
1 parent d6b3a8c commit a084c2b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 74 deletions.
77 changes: 4 additions & 73 deletions apps/mobile/src/modules/entry-list/entry-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { FeedViewType } from "@follow/constants"
import { useTypeScriptHappyCallback } from "@follow/hooks"
import { useBottomTabBarHeight } from "@react-navigation/bottom-tabs"
import { useHeaderHeight } from "@react-navigation/elements"
import { useIsFocused } from "@react-navigation/native"
import { FlashList } from "@shopify/flash-list"
import { Image } from "expo-image"
import { router } from "expo-router"
import { useCallback, useContext, useEffect, useMemo } from "react"
import { useCallback, useContext, useMemo } from "react"
import { StyleSheet, Text, useAnimatedValue, View } from "react-native"
import { useSafeAreaInsets } from "react-native-safe-area-context"

Expand All @@ -15,82 +14,14 @@ import {
NavigationContext,
} from "@/src/components/common/SafeNavigationScrollView"
import { ItemPressable } from "@/src/components/ui/pressable/item-pressable"
import { useSelectedFeed, useSetDrawerSwipeDisabled } from "@/src/modules/feed-drawer/atoms"
import {
useEntry,
useEntryIdsByCategory,
useEntryIdsByFeedId,
useEntryIdsByInboxId,
useEntryIdsByView,
} from "@/src/store/entry/hooks"
import { useList } from "@/src/store/list/hooks"
import { useSelectedFeed } from "@/src/modules/feed-drawer/atoms"
import { useEntry } from "@/src/store/entry/hooks"

import { ViewSelector } from "../feed-drawer/view-selector"
import { LeftAction, RightAction } from "./action"
import { EntryListContentGrid } from "./entry-list-gird"

export function EntryList() {
const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled()
const isFocused = useIsFocused()
useEffect(() => {
if (isFocused) {
setDrawerSwipeDisabled(false)
} else {
setDrawerSwipeDisabled(true)
}
}, [setDrawerSwipeDisabled, isFocused])

const selectedFeed = useSelectedFeed()

switch (selectedFeed.type) {
case "view": {
return <ViewEntryList viewId={selectedFeed.viewId} />
}
case "feed": {
return <FeedEntryList feedId={selectedFeed.feedId} />
}
case "category": {
return <CategoryEntryList categoryName={selectedFeed.categoryName} />
}
case "list": {
return <ListEntryList listId={selectedFeed.listId} />
}
case "inbox": {
return <InboxEntryList inboxId={selectedFeed.inboxId} />
}
// No default
}
}

function ViewEntryList({ viewId }: { viewId: FeedViewType }) {
const entryIds = useEntryIdsByView(viewId)

return <EntryListScreen entryIds={entryIds} />
}

function FeedEntryList({ feedId }: { feedId: string }) {
const entryIds = useEntryIdsByFeedId(feedId)
return <EntryListScreen entryIds={entryIds} />
}

function CategoryEntryList({ categoryName }: { categoryName: string }) {
const entryIds = useEntryIdsByCategory(categoryName)
return <EntryListScreen entryIds={entryIds} />
}

function ListEntryList({ listId }: { listId: string }) {
const list = useList(listId)
if (!list) return null

return <EntryListScreen entryIds={list.entryIds ?? []} />
}

function InboxEntryList({ inboxId }: { inboxId: string }) {
const entryIds = useEntryIdsByInboxId(inboxId)
return <EntryListScreen entryIds={entryIds} />
}

function EntryListScreen({ entryIds }: { entryIds: string[] }) {
export function EntryListScreen({ entryIds }: { entryIds: string[] }) {
const scrollY = useAnimatedValue(0)
const selectedFeed = useSelectedFeed()
const view = selectedFeed.type === "view" ? selectedFeed.viewId : null
Expand Down
74 changes: 74 additions & 0 deletions apps/mobile/src/modules/entry-list/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { FeedViewType } from "@follow/constants"
import { useIsFocused } from "@react-navigation/native"
import { useEffect } from "react"

import { useSelectedFeed, useSetDrawerSwipeDisabled } from "@/src/modules/feed-drawer/atoms"
import {
useEntryIdsByCategory,
useEntryIdsByFeedId,
useEntryIdsByInboxId,
useEntryIdsByView,
} from "@/src/store/entry/hooks"
import { useList } from "@/src/store/list/hooks"

import { EntryListScreen } from "./entry-list"

export function EntryList() {
const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled()
const isFocused = useIsFocused()
useEffect(() => {
if (isFocused) {
setDrawerSwipeDisabled(false)
} else {
setDrawerSwipeDisabled(true)
}
}, [setDrawerSwipeDisabled, isFocused])

const selectedFeed = useSelectedFeed()

switch (selectedFeed.type) {
case "view": {
return <ViewEntryList viewId={selectedFeed.viewId} />
}
case "feed": {
return <FeedEntryList feedId={selectedFeed.feedId} />
}
case "category": {
return <CategoryEntryList categoryName={selectedFeed.categoryName} />
}
case "list": {
return <ListEntryList listId={selectedFeed.listId} />
}
case "inbox": {
return <InboxEntryList inboxId={selectedFeed.inboxId} />
}
// No default
}
}

function ViewEntryList({ viewId }: { viewId: FeedViewType }) {
const entryIds = useEntryIdsByView(viewId)
return <EntryListScreen entryIds={entryIds} />
}

function FeedEntryList({ feedId }: { feedId: string }) {
const entryIds = useEntryIdsByFeedId(feedId)
return <EntryListScreen entryIds={entryIds} />
}

function CategoryEntryList({ categoryName }: { categoryName: string }) {
const entryIds = useEntryIdsByCategory(categoryName)
return <EntryListScreen entryIds={entryIds} />
}

function ListEntryList({ listId }: { listId: string }) {
const list = useList(listId)
if (!list) return null

return <EntryListScreen entryIds={list.entryIds ?? []} />
}

function InboxEntryList({ inboxId }: { inboxId: string }) {
const entryIds = useEntryIdsByInboxId(inboxId)
return <EntryListScreen entryIds={entryIds} />
}
2 changes: 1 addition & 1 deletion apps/mobile/src/screens/(stack)/(tabs)/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntryList } from "@/src/modules/entry-list/entry-list"
import { EntryList } from "@/src/modules/entry-list"

export default function Index() {
return <EntryList />
Expand Down

0 comments on commit a084c2b

Please sign in to comment.