feat(rn): implement search list
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { getBackgroundGradient, isCJKChar } from "@follow/utils"
|
||||
import { Image } from "expo-image"
|
||||
import { LinearGradient } from "expo-linear-gradient"
|
||||
import { useMemo } from "react"
|
||||
import { useMemo, useState } from "react"
|
||||
import type { StyleProp, ViewStyle } from "react-native"
|
||||
import { StyleSheet, Text } from "react-native"
|
||||
import { StyleSheet, Text, View } from "react-native"
|
||||
|
||||
export const FallbackIcon = ({
|
||||
title,
|
||||
@@ -39,6 +40,31 @@ export const FallbackIcon = ({
|
||||
)
|
||||
}
|
||||
|
||||
export const IconWithFallback = (props: {
|
||||
url?: string | undefined | null
|
||||
size: number
|
||||
title?: string
|
||||
className?: string
|
||||
style?: StyleProp<ViewStyle>
|
||||
}) => {
|
||||
const { url, size, title = "", className, style } = props
|
||||
const [hasError, setHasError] = useState(false)
|
||||
|
||||
if (!url || hasError) {
|
||||
return <FallbackIcon title={title} size={size} className={className} style={style} />
|
||||
}
|
||||
|
||||
return (
|
||||
<View className={className} style={style}>
|
||||
<Image
|
||||
source={{ uri: url }}
|
||||
style={[{ width: size, height: size }]}
|
||||
onError={() => setHasError(true)}
|
||||
/>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
display: "flex",
|
||||
|
||||
@@ -2,12 +2,10 @@ export enum SearchType {
|
||||
Feed = "feed",
|
||||
List = "list",
|
||||
User = "user",
|
||||
RSSHub = "rsshub",
|
||||
}
|
||||
|
||||
export const SearchTabs = [
|
||||
{ name: "Feed", value: SearchType.Feed },
|
||||
{ name: "List", value: SearchType.List },
|
||||
{ name: "User", value: SearchType.User },
|
||||
{ name: "RSSHub", value: SearchType.RSSHub },
|
||||
]
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
import { FeedViewType } from "@follow/constants"
|
||||
import { withOpacity } from "@follow/utils"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { Image } from "expo-image"
|
||||
import { router } from "expo-router"
|
||||
import { useAtomValue } from "jotai"
|
||||
import { memo } from "react"
|
||||
import type { ListRenderItem } from "react-native"
|
||||
import { Text, useWindowDimensions, View } from "react-native"
|
||||
import { ScrollView } from "react-native-gesture-handler"
|
||||
import Animated, { FadeInUp } from "react-native-reanimated"
|
||||
|
||||
import { FeedIcon } from "@/src/components/ui/icon/feed-icon"
|
||||
import { LoadingIndicator } from "@/src/components/ui/loading"
|
||||
import { ItemPressable } from "@/src/components/ui/pressable/item-pressable"
|
||||
import { SadCuteReIcon } from "@/src/icons/sad_cute_re"
|
||||
import { apiClient } from "@/src/lib/api-fetch"
|
||||
import { useSubscriptionByFeedId } from "@/src/store/subscription/hooks"
|
||||
import { useColor } from "@/src/theme/colors"
|
||||
|
||||
import { useSearchPageContext } from "../ctx"
|
||||
import { BaseSearchPageFlatList, BaseSearchPageRootView, BaseSearchPageScrollView } from "./__base"
|
||||
import { BaseSearchPageFlatList, ItemSeparator, RenderScrollComponent } from "./__base"
|
||||
import { useDataSkeleton } from "./hooks"
|
||||
|
||||
type SearchResultItem = Awaited<ReturnType<typeof apiClient.discover.$post>>["data"][number]
|
||||
|
||||
@@ -39,50 +36,34 @@ export const SearchFeed = () => {
|
||||
enabled: !!searchValue,
|
||||
})
|
||||
|
||||
const textColor = useColor("text")
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<BaseSearchPageRootView className="items-center justify-center">
|
||||
<View className="-mt-72" />
|
||||
<LoadingIndicator color={withOpacity(textColor, 0.7)} size={32} />
|
||||
</BaseSearchPageRootView>
|
||||
)
|
||||
}
|
||||
|
||||
if (data?.data.length === 0) {
|
||||
return (
|
||||
<BaseSearchPageRootView className="items-center justify-center">
|
||||
<View className="-mt-72" />
|
||||
<SadCuteReIcon height={32} width={32} color={withOpacity(textColor, 0.5)} />
|
||||
<Text className="text-text/50 mt-2">No results found</Text>
|
||||
</BaseSearchPageRootView>
|
||||
)
|
||||
}
|
||||
const skeleton = useDataSkeleton(isLoading, data)
|
||||
if (skeleton) return skeleton
|
||||
|
||||
return (
|
||||
<BaseSearchPageFlatList
|
||||
refreshing={isLoading}
|
||||
onRefresh={refetch}
|
||||
keyExtractor={keyExtractor}
|
||||
renderScrollComponent={(props) => <BaseSearchPageScrollView {...props} />}
|
||||
contentContainerClassName={"-mt-4"}
|
||||
renderScrollComponent={RenderScrollComponent}
|
||||
data={data?.data}
|
||||
renderItem={renderItem}
|
||||
ItemSeparatorComponent={ItemSeparator}
|
||||
/>
|
||||
)
|
||||
}
|
||||
const keyExtractor = (item: SearchResultItem) => item.feed?.id ?? Math.random().toString()
|
||||
|
||||
const renderItem = ({ item }: { item: SearchResultItem }) => (
|
||||
const renderItem: ListRenderItem<SearchResultItem> = ({ item }) => (
|
||||
<SearchFeedItem key={item.feed?.id} item={item} />
|
||||
)
|
||||
|
||||
const SearchFeedItem = memo(({ item }: { item: SearchResultItem }) => {
|
||||
const SearchFeedItem = ({ item }: { item: SearchResultItem }) => {
|
||||
const isSubscribed = useSubscriptionByFeedId(item.feed?.id ?? "")
|
||||
return (
|
||||
<Animated.View entering={FadeInUp}>
|
||||
<ItemPressable
|
||||
className="py-2"
|
||||
className="py-6"
|
||||
onPress={() => {
|
||||
if (item.feed?.id) {
|
||||
router.push(`/follow?id=${item.feed.id}`)
|
||||
@@ -146,7 +127,7 @@ const SearchFeedItem = memo(({ item }: { item: SearchResultItem }) => {
|
||||
</ItemPressable>
|
||||
</Animated.View>
|
||||
)
|
||||
})
|
||||
}
|
||||
const formatter = new Intl.DateTimeFormat("en-US", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
|
||||
@@ -1,16 +1,111 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { Image } from "expo-image"
|
||||
import { router } from "expo-router"
|
||||
import { useAtomValue } from "jotai"
|
||||
import { Text } from "react-native"
|
||||
import { memo } from "react"
|
||||
import { Text, View } from "react-native"
|
||||
import Animated, { FadeInUp } from "react-native-reanimated"
|
||||
|
||||
import { FallbackIcon } from "@/src/components/ui/icon/fallback-icon"
|
||||
import { ItemPressable } from "@/src/components/ui/pressable/item-pressable"
|
||||
import { apiClient } from "@/src/lib/api-fetch"
|
||||
import { useSubscriptionByListId } from "@/src/store/subscription/hooks"
|
||||
|
||||
import { useSearchPageContext } from "../ctx"
|
||||
import { BaseSearchPageScrollView } from "./__base"
|
||||
import { BaseSearchPageFlatList, ItemSeparator, RenderScrollComponent } from "./__base"
|
||||
import { useDataSkeleton } from "./hooks"
|
||||
|
||||
type SearchResultItem = Awaited<ReturnType<typeof apiClient.discover.$post>>["data"][number]
|
||||
|
||||
export const SearchList = () => {
|
||||
const { searchValueAtom } = useSearchPageContext()
|
||||
const searchValue = useAtomValue(searchValueAtom)
|
||||
|
||||
const { data, isLoading, refetch } = useQuery({
|
||||
queryKey: ["searchFeed", searchValue],
|
||||
queryFn: () => {
|
||||
return apiClient.discover.$post({
|
||||
json: {
|
||||
keyword: searchValue,
|
||||
target: "lists",
|
||||
},
|
||||
})
|
||||
},
|
||||
enabled: !!searchValue,
|
||||
})
|
||||
|
||||
const skeleton = useDataSkeleton(isLoading, data)
|
||||
if (skeleton) return skeleton
|
||||
|
||||
return (
|
||||
<BaseSearchPageScrollView>
|
||||
<Text className="text-text">{searchValue}</Text>
|
||||
</BaseSearchPageScrollView>
|
||||
<BaseSearchPageFlatList
|
||||
refreshing={isLoading}
|
||||
onRefresh={refetch}
|
||||
keyExtractor={keyExtractor}
|
||||
renderScrollComponent={RenderScrollComponent}
|
||||
data={data?.data}
|
||||
renderItem={renderItem}
|
||||
ItemSeparatorComponent={ItemSeparator}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const keyExtractor = (item: SearchResultItem) => item.list?.id ?? Math.random().toString()
|
||||
|
||||
const renderItem = ({ item }: { item: SearchResultItem }) => (
|
||||
<SearchListCard key={item.list?.id} item={item} />
|
||||
)
|
||||
|
||||
const SearchListCard = memo(({ item }: { item: SearchResultItem }) => {
|
||||
const isSubscribed = useSubscriptionByListId(item.list?.id ?? "")
|
||||
return (
|
||||
<Animated.View entering={FadeInUp}>
|
||||
<ItemPressable
|
||||
className="py-2"
|
||||
onPress={() => {
|
||||
if (item.list?.id) {
|
||||
router.push(`/follow?id=${item.list.id}&type=list`)
|
||||
}
|
||||
}}
|
||||
>
|
||||
{/* Headline */}
|
||||
<View className="flex-row items-center gap-2 pl-4 pr-2">
|
||||
<View className="size-[32px] overflow-hidden rounded-lg">
|
||||
{item.list?.image ? (
|
||||
<Image
|
||||
source={item.list.image}
|
||||
className="size-full"
|
||||
contentFit="cover"
|
||||
transition={1000}
|
||||
/>
|
||||
) : (
|
||||
!!item.list?.title && <FallbackIcon title={item.list.title} size={32} />
|
||||
)}
|
||||
</View>
|
||||
<View className="flex-1">
|
||||
<Text
|
||||
className="text-text text-lg font-semibold"
|
||||
ellipsizeMode="middle"
|
||||
numberOfLines={1}
|
||||
>
|
||||
{item.list?.title}
|
||||
</Text>
|
||||
{!!item.list?.description && (
|
||||
<Text className="text-text/60" ellipsizeMode="tail" numberOfLines={1}>
|
||||
{item.list?.description}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
{/* Subscribe */}
|
||||
{isSubscribed && (
|
||||
<View className="ml-auto">
|
||||
<View className="bg-gray-5/60 rounded-full px-2 py-1">
|
||||
<Text className="text-gray-2 text-sm font-medium">Subscribed</Text>
|
||||
</View>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
</ItemPressable>
|
||||
</Animated.View>
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Text } from "react-native"
|
||||
|
||||
import { BaseSearchPageScrollView } from "./__base"
|
||||
|
||||
export const SearchRSSHub = () => {
|
||||
return (
|
||||
<BaseSearchPageScrollView>
|
||||
<Text>RSSHub</Text>
|
||||
</BaseSearchPageScrollView>
|
||||
)
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { cn } from "@follow/utils/src/utils"
|
||||
import { forwardRef } from "react"
|
||||
import type { ScrollViewProps } from "react-native"
|
||||
import { RefreshControl, ScrollView, useWindowDimensions, View } from "react-native"
|
||||
import { RefreshControl, ScrollView, StyleSheet, useWindowDimensions, View } from "react-native"
|
||||
import type { FlatListPropsWithLayout } from "react-native-reanimated"
|
||||
import Animated, { LinearTransition } from "react-native-reanimated"
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context"
|
||||
@@ -70,10 +70,18 @@ export function BaseSearchPageFlatList<T>({
|
||||
<RefreshControl
|
||||
refreshing={refreshing}
|
||||
onRefresh={onRefresh}
|
||||
progressViewOffset={offsetTop}
|
||||
progressViewOffset={searchBarHeight + insets.top}
|
||||
/>
|
||||
}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
const itemSeparator = (
|
||||
<View style={{ height: StyleSheet.hairlineWidth }} className="bg-opaque-separator ml-16 flex-1" />
|
||||
)
|
||||
export const ItemSeparator = () => itemSeparator
|
||||
|
||||
export const RenderScrollComponent = (props: ScrollViewProps) => (
|
||||
<BaseSearchPageScrollView {...props} />
|
||||
)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { withOpacity } from "@follow/utils"
|
||||
import { useMemo } from "react"
|
||||
import { Text, View } from "react-native"
|
||||
|
||||
import { LoadingIndicator } from "@/src/components/ui/loading"
|
||||
import { SadCuteReIcon } from "@/src/icons/sad_cute_re"
|
||||
import { useColor } from "@/src/theme/colors"
|
||||
|
||||
import { BaseSearchPageRootView } from "./__base"
|
||||
|
||||
export const useDataSkeleton = (isLoading: boolean, data: any) => {
|
||||
const textColor = useColor("text")
|
||||
return useMemo(() => {
|
||||
if (isLoading) {
|
||||
return (
|
||||
<BaseSearchPageRootView className="items-center justify-center">
|
||||
<View className="-mt-72" />
|
||||
<LoadingIndicator color={withOpacity(textColor, 0.7)} size={32} />
|
||||
</BaseSearchPageRootView>
|
||||
)
|
||||
}
|
||||
|
||||
if (data?.data.length === 0) {
|
||||
return (
|
||||
<BaseSearchPageRootView className="items-center justify-center">
|
||||
<View className="-mt-72" />
|
||||
<SadCuteReIcon height={32} width={32} color={withOpacity(textColor, 0.5)} />
|
||||
<Text className="text-text/50 mt-2">No results found</Text>
|
||||
</BaseSearchPageRootView>
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}, [isLoading, data, textColor])
|
||||
}
|
||||
@@ -38,7 +38,7 @@ export const SearchHeader: FC<{
|
||||
className="relative"
|
||||
onLayout={onLayout}
|
||||
>
|
||||
{/* <BlurEffect /> */}
|
||||
<BlurEffect />
|
||||
<View style={styles.header}>
|
||||
<ComposeSearchBar />
|
||||
</View>
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
import { FeedViewType } from "@follow/constants"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { StackActions } from "@react-navigation/native"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { router, Stack, useLocalSearchParams, useNavigation } from "expo-router"
|
||||
import { useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { ScrollView, Text, View } from "react-native"
|
||||
import { z } from "zod"
|
||||
|
||||
import {
|
||||
ModalHeaderCloseButton,
|
||||
ModalHeaderShubmitButton,
|
||||
} from "@/src/components/common/ModalSharedComponents"
|
||||
import { FormProvider } from "@/src/components/ui/form/FormProvider"
|
||||
import { FormLabel } from "@/src/components/ui/form/Label"
|
||||
import { FormSwitch } from "@/src/components/ui/form/Switch"
|
||||
import { TextField } from "@/src/components/ui/form/TextField"
|
||||
import { FeedIcon } from "@/src/components/ui/icon/feed-icon"
|
||||
import { LoadingIndicator } from "@/src/components/ui/loading"
|
||||
import { useIsRouteOnlyOne } from "@/src/hooks/useIsRouteOnlyOne"
|
||||
import { FeedViewSelector } from "@/src/modules/feed/view-selector"
|
||||
import { useFeed } from "@/src/store/feed/hooks"
|
||||
import { feedSyncServices } from "@/src/store/feed/store"
|
||||
import { useSubscriptionByFeedId } from "@/src/store/subscription/hooks"
|
||||
import { subscriptionSyncService } from "@/src/store/subscription/store"
|
||||
import type { SubscriptionForm } from "@/src/store/subscription/types"
|
||||
|
||||
const formSchema = z.object({
|
||||
view: z.string(),
|
||||
category: z.string().nullable().optional(),
|
||||
isPrivate: z.boolean().optional(),
|
||||
title: z.string().optional(),
|
||||
})
|
||||
const defaultValues = { view: FeedViewType.Articles.toString() }
|
||||
export function FollowFeed(props: { id: string }) {
|
||||
const { id } = props
|
||||
const feed = useFeed(id as string)
|
||||
const { isLoading } = useQuery({
|
||||
queryKey: ["feed", id],
|
||||
queryFn: () => feedSyncServices.fetchFeedById({ id: id as string }),
|
||||
enabled: !feed,
|
||||
})
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<View className="mt-24 flex-1 flex-row items-start justify-center">
|
||||
<LoadingIndicator size={36} />
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
return <FollowImpl />
|
||||
}
|
||||
function FollowImpl() {
|
||||
const { id } = useLocalSearchParams()
|
||||
|
||||
const feed = useFeed(id as string)
|
||||
const isSubscribed = useSubscriptionByFeedId(feed?.id || "")
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues,
|
||||
})
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const routeOnlyOne = useIsRouteOnlyOne()
|
||||
const navigate = useNavigation()
|
||||
const parentRoute = navigate.getParent()
|
||||
const submit = async () => {
|
||||
setIsLoading(true)
|
||||
const values = form.getValues()
|
||||
const body: SubscriptionForm = {
|
||||
url: feed.url,
|
||||
view: Number.parseInt(values.view),
|
||||
category: values.category ?? "",
|
||||
isPrivate: values.isPrivate ?? false,
|
||||
title: values.title ?? "",
|
||||
feedId: feed.id,
|
||||
}
|
||||
|
||||
await subscriptionSyncService.subscribe(body).finally(() => {
|
||||
setIsLoading(false)
|
||||
})
|
||||
|
||||
if (router.canDismiss()) {
|
||||
router.dismissAll()
|
||||
|
||||
if (!routeOnlyOne) {
|
||||
parentRoute?.dispatch(StackActions.popToTop())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { isValid, isDirty } = form.formState
|
||||
|
||||
if (!feed?.id) {
|
||||
return <Text className="text-text">Feed ({id}) not found</Text>
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollView contentContainerClassName="px-2 pt-4 gap-y-4">
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: `${isSubscribed ? "Edit" : "Follow"} - ${feed?.title}`,
|
||||
headerLeft: ModalHeaderCloseButton,
|
||||
gestureEnabled: !isDirty,
|
||||
headerRight: () => (
|
||||
<ModalHeaderShubmitButton
|
||||
isValid={isValid}
|
||||
onPress={form.handleSubmit(submit)}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
),
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* Group 1 */}
|
||||
<View className="bg-secondary-system-grouped-background rounded-lg p-4">
|
||||
<View className="flex flex-row gap-4">
|
||||
<View className="size-[50px] overflow-hidden rounded-lg">
|
||||
<FeedIcon feed={feed} size={50} />
|
||||
</View>
|
||||
<View className="flex-1 flex-col gap-y-1">
|
||||
<Text className="text-text text-lg font-semibold">{feed?.title}</Text>
|
||||
<Text className="text-secondary-label text-sm">{feed?.description}</Text>
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
{/* Group 2 */}
|
||||
<View className="bg-secondary-system-grouped-background gap-y-4 rounded-lg p-4">
|
||||
<FormProvider form={form}>
|
||||
<View>
|
||||
<Controller
|
||||
name="title"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, ref, value } }) => (
|
||||
<TextField
|
||||
label="Title"
|
||||
description="Custom title for this Feed. Leave empty to use the default."
|
||||
onChangeText={onChange}
|
||||
value={value}
|
||||
ref={ref}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View>
|
||||
<Controller
|
||||
name="category"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, ref, value } }) => (
|
||||
<TextField
|
||||
label="Category"
|
||||
description="By default, your follows will be grouped by website."
|
||||
onChangeText={onChange}
|
||||
value={value || ""}
|
||||
ref={ref}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View>
|
||||
<Controller
|
||||
name="isPrivate"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, value } }) => (
|
||||
<FormSwitch
|
||||
value={value}
|
||||
label="Private"
|
||||
description="Private feeds are only visible to you."
|
||||
onValueChange={onChange}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View className="-mx-4">
|
||||
<FormLabel className="mb-4 pl-5" label="View" optional />
|
||||
|
||||
<Controller
|
||||
name="view"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, value } }) => (
|
||||
<FeedViewSelector value={value as any as FeedViewType} onChange={onChange} />
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
</FormProvider>
|
||||
</View>
|
||||
</ScrollView>
|
||||
)
|
||||
}
|
||||
@@ -4,23 +4,35 @@ import { Text, TouchableOpacity, View } from "react-native"
|
||||
|
||||
import { Grid } from "@/src/components/ui/grid"
|
||||
import { views } from "@/src/constants/views"
|
||||
import { useColor } from "@/src/theme/colors"
|
||||
|
||||
interface Props {
|
||||
value: FeedViewType
|
||||
onChange: (value: FeedViewType) => void
|
||||
onChange?: (value: FeedViewType) => void
|
||||
|
||||
className?: string
|
||||
readOnly?: boolean
|
||||
}
|
||||
|
||||
export const FeedViewSelector = ({ value, onChange, className }: Props) => {
|
||||
export const FeedViewSelector = ({ value, onChange, className, readOnly }: Props) => {
|
||||
const secondaryLabelColor = useColor("secondaryLabel")
|
||||
return (
|
||||
<Grid columns={views.length} gap={5} className={className}>
|
||||
{views.map((view) => {
|
||||
const isSelected = +value === +view.view
|
||||
return (
|
||||
<TouchableOpacity key={view.name} onPress={() => onChange(view.view)}>
|
||||
<TouchableOpacity
|
||||
key={view.name}
|
||||
onPress={() => onChange?.(view.view)}
|
||||
disabled={readOnly}
|
||||
className={readOnly ? "opacity-50" : undefined}
|
||||
>
|
||||
<View className="flex-1 items-center">
|
||||
<view.icon color={isSelected ? view.activeColor : "gray"} height={18} width={18} />
|
||||
<view.icon
|
||||
color={isSelected ? view.activeColor : secondaryLabelColor}
|
||||
height={18}
|
||||
width={18}
|
||||
/>
|
||||
<Text
|
||||
className={cn(
|
||||
"mt-1 whitespace-nowrap text-[8px] font-medium",
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
import { FeedViewType } from "@follow/constants"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { Stack } from "expo-router"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { Alert, ScrollView, Text, View } from "react-native"
|
||||
import { z } from "zod"
|
||||
|
||||
import {
|
||||
ModalHeaderCloseButton,
|
||||
ModalHeaderShubmitButton,
|
||||
} from "@/src/components/common/ModalSharedComponents"
|
||||
import { FormProvider } from "@/src/components/ui/form/FormProvider"
|
||||
import { FormLabel } from "@/src/components/ui/form/Label"
|
||||
import { FormSwitch } from "@/src/components/ui/form/Switch"
|
||||
import { TextField } from "@/src/components/ui/form/TextField"
|
||||
import { IconWithFallback } from "@/src/components/ui/icon/fallback-icon"
|
||||
import { LoadingIndicator } from "@/src/components/ui/loading"
|
||||
import { useList } from "@/src/store/list/hooks"
|
||||
import { listSyncServices } from "@/src/store/list/store"
|
||||
import { useSubscriptionByListId } from "@/src/store/subscription/hooks"
|
||||
|
||||
import { FeedViewSelector } from "../feed/view-selector"
|
||||
|
||||
export const FollowList = (props: { id: string }) => {
|
||||
const { id } = props
|
||||
const list = useList(id as string)
|
||||
const { isLoading } = useQuery({
|
||||
queryKey: ["list", id],
|
||||
queryFn: () => listSyncServices.fetchListById({ id: id as string }),
|
||||
enabled: !list,
|
||||
})
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<View className="mt-24 flex-1 flex-row items-start justify-center">
|
||||
<LoadingIndicator size={36} />
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
return <Impl id={id} />
|
||||
}
|
||||
|
||||
const formSchema = z.object({
|
||||
view: z.string(),
|
||||
isPrivate: z.boolean().optional(),
|
||||
title: z.string().optional(),
|
||||
})
|
||||
const defaultValues = { view: FeedViewType.Articles.toString() }
|
||||
|
||||
const Impl = (props: { id: string }) => {
|
||||
const { id } = props
|
||||
const list = useList(id as string)!
|
||||
|
||||
const isSubscribed = useSubscriptionByListId(id as string)
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues,
|
||||
})
|
||||
const { isValid, isDirty } = form.formState
|
||||
|
||||
const submit = async () => {
|
||||
const payload = form.getValues()
|
||||
// console.log("submit", payload)
|
||||
void payload
|
||||
|
||||
if (list.fee && !isSubscribed) {
|
||||
Alert.alert(
|
||||
`To follow this list, you must pay a fee to the list creator. Press OK to pay ${list.fee} power to follow this list.`,
|
||||
"OK",
|
||||
[
|
||||
{
|
||||
text: "Cancel",
|
||||
style: "cancel",
|
||||
},
|
||||
{
|
||||
text: "OK",
|
||||
onPress: () => {},
|
||||
},
|
||||
],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const isLoading = false
|
||||
|
||||
return (
|
||||
<ScrollView contentContainerClassName="px-2 pt-4 gap-y-4">
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: `${isSubscribed ? "Edit" : "Follow"} - ${list?.title}`,
|
||||
headerLeft: ModalHeaderCloseButton,
|
||||
gestureEnabled: !isDirty,
|
||||
headerRight: () => (
|
||||
<ModalHeaderShubmitButton
|
||||
isValid={isValid}
|
||||
onPress={form.handleSubmit(submit)}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
),
|
||||
}}
|
||||
/>
|
||||
|
||||
<View className="bg-secondary-system-grouped-background rounded-lg p-4">
|
||||
<View className="flex flex-row gap-4">
|
||||
<View className="size-[50px] overflow-hidden rounded-lg">
|
||||
<IconWithFallback url={list?.image} size={50} />
|
||||
</View>
|
||||
<View className="flex-1 flex-col gap-y-1">
|
||||
<Text className="text-text text-lg font-semibold">{list?.title}</Text>
|
||||
<Text className="text-secondary-label text-sm">{list?.description}</Text>
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
<View className="bg-secondary-system-grouped-background gap-y-4 rounded-lg p-4">
|
||||
<FormProvider form={form}>
|
||||
<View className="-mx-4">
|
||||
<FormLabel className="mb-4 pl-5" label="View" optional />
|
||||
|
||||
<FeedViewSelector value={list.view} />
|
||||
</View>
|
||||
|
||||
<View>
|
||||
<Controller
|
||||
name="title"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, ref, value } }) => (
|
||||
<TextField
|
||||
label="Title"
|
||||
description="Custom title for this Feed. Leave empty to use the default."
|
||||
onChangeText={onChange}
|
||||
value={value}
|
||||
ref={ref}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View>
|
||||
<Controller
|
||||
name="isPrivate"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, value } }) => (
|
||||
<FormSwitch
|
||||
value={value}
|
||||
label="Private"
|
||||
description="Private feeds are only visible to you."
|
||||
onValueChange={onChange}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
|
||||
{!!list.fee && (
|
||||
<View>
|
||||
<View className="flex-row">
|
||||
<FormLabel label="Follow fee" optional />
|
||||
<Text className="text-text text-lg font-semibold">{list.fee}</Text>
|
||||
</View>
|
||||
<Text className="text-secondary-label text-sm">
|
||||
To follow this list, you must pay a fee to the list creator.
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
</FormProvider>
|
||||
</View>
|
||||
</ScrollView>
|
||||
)
|
||||
}
|
||||
@@ -79,6 +79,20 @@ class Morph {
|
||||
}
|
||||
return { subscriptions, ...collections }
|
||||
}
|
||||
|
||||
toList({ list: data }: HonoApiClient.List_Get): ListModel {
|
||||
return {
|
||||
id: data.id,
|
||||
title: data.title!,
|
||||
userId: data.ownerUserId!,
|
||||
description: data.description!,
|
||||
view: data.view,
|
||||
image: data.image!,
|
||||
ownerUserId: data.ownerUserId!,
|
||||
feedIds: data.feedIds!,
|
||||
fee: data.fee!,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const honoMorph = new Morph()
|
||||
|
||||
@@ -7,4 +7,5 @@ type ExtractData<T extends (...args: any) => any> =
|
||||
|
||||
export namespace HonoApiClient {
|
||||
export type Subscription_Get = ExtractData<typeof apiClient.subscriptions.$get>
|
||||
export type List_Get = ExtractData<typeof apiClient.lists.$get>
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ import {
|
||||
import { SearchHeader } from "@/src/modules/discover/search"
|
||||
import { SearchFeed } from "@/src/modules/discover/search-tabs/SearchFeed"
|
||||
import { SearchList } from "@/src/modules/discover/search-tabs/SearchList"
|
||||
import { SearchRSSHub } from "@/src/modules/discover/search-tabs/SearchRSSHub"
|
||||
import { SearchUser } from "@/src/modules/discover/search-tabs/SearchUser"
|
||||
|
||||
const Search = () => {
|
||||
@@ -41,7 +40,6 @@ const SearchType2RenderContent: Record<SearchType, React.FC> = {
|
||||
[SearchType.Feed]: SearchFeed,
|
||||
[SearchType.List]: SearchList,
|
||||
[SearchType.User]: SearchUser,
|
||||
[SearchType.RSSHub]: SearchRSSHub,
|
||||
}
|
||||
const PlaceholderLazyView = () => {
|
||||
const windowWidth = Dimensions.get("window").width
|
||||
|
||||
@@ -1,195 +1,20 @@
|
||||
import { FeedViewType } from "@follow/constants"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { StackActions } from "@react-navigation/native"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { router, Stack, useLocalSearchParams, useNavigation } from "expo-router"
|
||||
import { useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { ScrollView, Text, View } from "react-native"
|
||||
import { z } from "zod"
|
||||
import { useLocalSearchParams } from "expo-router"
|
||||
|
||||
import {
|
||||
ModalHeaderCloseButton,
|
||||
ModalHeaderShubmitButton,
|
||||
} from "@/src/components/common/ModalSharedComponents"
|
||||
import { FormProvider } from "@/src/components/ui/form/FormProvider"
|
||||
import { FormLabel } from "@/src/components/ui/form/Label"
|
||||
import { FormSwitch } from "@/src/components/ui/form/Switch"
|
||||
import { TextField } from "@/src/components/ui/form/TextField"
|
||||
import { FeedIcon } from "@/src/components/ui/icon/feed-icon"
|
||||
import { LoadingIndicator } from "@/src/components/ui/loading"
|
||||
import { useIsRouteOnlyOne } from "@/src/hooks/useIsRouteOnlyOne"
|
||||
import { FeedViewSelector } from "@/src/modules/feed/view-selector"
|
||||
import { useFeed } from "@/src/store/feed/hooks"
|
||||
import { feedSyncServices } from "@/src/store/feed/store"
|
||||
import { useSubscriptionByFeedId } from "@/src/store/subscription/hooks"
|
||||
import { subscriptionSyncService } from "@/src/store/subscription/store"
|
||||
import type { SubscriptionForm } from "@/src/store/subscription/types"
|
||||
import { FollowFeed } from "@/src/modules/feed/FollowFeed"
|
||||
import { FollowList } from "@/src/modules/list/FollowList"
|
||||
|
||||
const formSchema = z.object({
|
||||
view: z.string(),
|
||||
category: z.string().nullable().optional(),
|
||||
isPrivate: z.boolean().optional(),
|
||||
title: z.string().optional(),
|
||||
})
|
||||
const defaultValues = { view: FeedViewType.Articles.toString() }
|
||||
export default function Follow() {
|
||||
const { id } = useLocalSearchParams()
|
||||
const feed = useFeed(id as string)
|
||||
const { isLoading } = useQuery({
|
||||
queryKey: ["feed", id],
|
||||
queryFn: () => feedSyncServices.fetchFeedById({ id: id as string }),
|
||||
enabled: !feed,
|
||||
})
|
||||
const { id, type = "feed" } = useLocalSearchParams()
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<View className="mt-24 flex-1 flex-row items-start justify-center">
|
||||
<LoadingIndicator size={36} />
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
return <FollowImpl />
|
||||
}
|
||||
function FollowImpl() {
|
||||
const { id } = useLocalSearchParams()
|
||||
|
||||
const feed = useFeed(id as string)
|
||||
const isSubscribed = useSubscriptionByFeedId(feed?.id || "")
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues,
|
||||
})
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const routeOnlyOne = useIsRouteOnlyOne()
|
||||
const navigate = useNavigation()
|
||||
const parentRoute = navigate.getParent()
|
||||
const submit = async () => {
|
||||
setIsLoading(true)
|
||||
const values = form.getValues()
|
||||
const body: SubscriptionForm = {
|
||||
url: feed.url,
|
||||
view: Number.parseInt(values.view),
|
||||
category: values.category ?? "",
|
||||
isPrivate: values.isPrivate ?? false,
|
||||
title: values.title ?? "",
|
||||
feedId: feed.id,
|
||||
switch (type) {
|
||||
case "feed": {
|
||||
return <FollowFeed id={id as string} />
|
||||
}
|
||||
|
||||
await subscriptionSyncService.subscribe(body).finally(() => {
|
||||
setIsLoading(false)
|
||||
})
|
||||
|
||||
if (router.canDismiss()) {
|
||||
router.dismissAll()
|
||||
|
||||
if (!routeOnlyOne) {
|
||||
parentRoute?.dispatch(StackActions.popToTop())
|
||||
}
|
||||
case "list": {
|
||||
return <FollowList id={id as string} />
|
||||
}
|
||||
default: {
|
||||
return <FollowFeed id={id as string} />
|
||||
}
|
||||
}
|
||||
|
||||
const { isValid, isDirty } = form.formState
|
||||
|
||||
if (!feed?.id) {
|
||||
return <Text className="text-text">Feed ({id}) not found</Text>
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollView contentContainerClassName="px-2 pt-4 gap-y-4">
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: `${isSubscribed ? "Edit" : "Follow"} - ${feed?.title}`,
|
||||
headerLeft: ModalHeaderCloseButton,
|
||||
gestureEnabled: !isDirty,
|
||||
headerRight: () => (
|
||||
<ModalHeaderShubmitButton
|
||||
isValid={isValid}
|
||||
onPress={form.handleSubmit(submit)}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
),
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* Group 1 */}
|
||||
<View className="bg-secondary-system-grouped-background rounded-lg p-4">
|
||||
<View className="flex flex-row gap-4">
|
||||
<View className="size-[50px] overflow-hidden rounded-lg">
|
||||
<FeedIcon feed={feed} size={50} />
|
||||
</View>
|
||||
<View className="flex-1 flex-col gap-y-1">
|
||||
<Text className="text-text text-lg font-semibold">{feed?.title}</Text>
|
||||
<Text className="text-secondary-label text-sm">{feed?.description}</Text>
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
{/* Group 2 */}
|
||||
<View className="bg-secondary-system-grouped-background gap-y-4 rounded-lg p-4">
|
||||
<FormProvider form={form}>
|
||||
<View>
|
||||
<Controller
|
||||
name="title"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, ref, value } }) => (
|
||||
<TextField
|
||||
label="Title"
|
||||
description="Custom title for this Feed. Leave empty to use the default."
|
||||
onChangeText={onChange}
|
||||
value={value}
|
||||
ref={ref}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View>
|
||||
<Controller
|
||||
name="category"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, ref, value } }) => (
|
||||
<TextField
|
||||
label="Category"
|
||||
description="By default, your follows will be grouped by website."
|
||||
onChangeText={onChange}
|
||||
value={value || ""}
|
||||
ref={ref}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View>
|
||||
<Controller
|
||||
name="isPrivate"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, value } }) => (
|
||||
<FormSwitch
|
||||
value={value}
|
||||
label="Private"
|
||||
description="Private feeds are only visible to you."
|
||||
onValueChange={onChange}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View className="-mx-4">
|
||||
<FormLabel className="mb-4 pl-5" label="View" optional />
|
||||
|
||||
<Controller
|
||||
name="view"
|
||||
control={form.control}
|
||||
render={({ field: { onChange, value } }) => (
|
||||
<FeedViewSelector value={value as any as FeedViewType} onChange={onChange} />
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
</FormProvider>
|
||||
</View>
|
||||
</ScrollView>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { FeedViewType } from "@follow/constants"
|
||||
import { PlatformPressable } from "@react-navigation/elements/src/PlatformPressable"
|
||||
import { router, Tabs } from "expo-router"
|
||||
import { View } from "react-native"
|
||||
import { Easing, View } from "react-native"
|
||||
import { Gesture, GestureDetector } from "react-native-gesture-handler"
|
||||
import { runOnJS } from "react-native-reanimated"
|
||||
|
||||
@@ -33,6 +33,14 @@ export default function TabLayout() {
|
||||
tabBarStyle: {
|
||||
position: "absolute",
|
||||
},
|
||||
animation: "fade",
|
||||
transitionSpec: {
|
||||
animation: "timing",
|
||||
config: {
|
||||
duration: 50,
|
||||
easing: Easing.ease,
|
||||
},
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Tabs.Screen
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import type { ListSchema } from "@/src/database/schemas/types"
|
||||
import { apiClient } from "@/src/lib/api-fetch"
|
||||
import { honoMorph } from "@/src/morph/hono"
|
||||
import { storeDbMorph } from "@/src/morph/store-db"
|
||||
import { ListService } from "@/src/services/list"
|
||||
|
||||
@@ -49,3 +51,15 @@ class ListActions {
|
||||
}
|
||||
|
||||
export const listActions = new ListActions()
|
||||
|
||||
class ListSyncServices {
|
||||
async fetchListById(params: { id: string }) {
|
||||
const list = await apiClient.lists.$get({ query: { listId: params.id } })
|
||||
|
||||
listActions.upsertMany([honoMorph.toList(list.data)])
|
||||
|
||||
return list.data
|
||||
}
|
||||
}
|
||||
|
||||
export const listSyncServices = new ListSyncServices()
|
||||
|
||||
@@ -193,3 +193,6 @@ export const useListSubscriptionCategory = (view: FeedViewType) => {
|
||||
|
||||
export const useSubscriptionByFeedId = (feedId: string) =>
|
||||
useSubscriptionStore(useCallback((state) => state.data[feedId] || null, [feedId]))
|
||||
|
||||
export const useSubscriptionByListId = (listId: string) =>
|
||||
useSubscriptionStore(useCallback((state) => state.data[listId] || null, [listId]))
|
||||
|
||||
Reference in New Issue
Block a user