1
1
import Header from 'components/Header'
2
- import { Stack , useSearchParams } from 'expo-router'
2
+ import { useSearchParams } from 'expo-router'
3
3
import { extract } from 'lib/parser'
4
4
import { useEffect , useState } from 'react'
5
5
import { Pressable } from 'react-native'
6
6
import { YStack , Text , XStack , Spinner } from 'tamagui'
7
- import { FeedData } from 'types'
8
- import { useAppSelector } from 'store/hooks'
7
+ import { Feed , FeedData , FeedListType } from 'types'
9
8
import Favicon from 'components/Favicon'
10
9
import _ from 'lodash'
11
10
import FeedInfo from 'components/FeedInfo'
12
11
import { EmojiLookUp } from 'iconoir-react-native'
13
12
import AddFeedButton from 'components/AddFeedButton'
14
13
import EntryList from 'components/EntryList'
14
+ import useFeeds from 'hooks/useFeeds'
15
+ import { createEntries , resubFeed , subFeed , unsubFeed } from 'lib/db'
16
+ import useEntryFlow from 'hooks/useEntryFlow'
15
17
16
18
export default function FeedProfile ( ) {
17
19
const [ data , setData ] = useState < FeedData > ( )
18
20
const [ error , setError ] = useState ( )
19
21
const [ loading , setLoading ] = useState ( false )
20
- const { url, title, description } = useSearchParams ( )
21
- const flows = useAppSelector ( ( state ) => state . feed . flow )
22
- const source = flows . find ( ( f ) => f . url === url )
22
+ const { url, title, from } = useSearchParams ( )
23
+ const { feeds } = useFeeds ( )
24
+ const feed = feeds . find ( ( f ) => f . url === url )
25
+ const { entries } = useEntryFlow ( )
23
26
24
27
useEffect ( ( ) => {
25
28
setData ( undefined )
26
29
setError ( undefined )
27
- if ( source ) {
28
- setData ( source )
30
+ if ( feed ) {
31
+ setData ( feed )
29
32
} else if ( url ) {
30
33
setLoading ( true )
31
34
extract ( url as string )
32
35
. then ( ( res ) => {
33
36
if ( res ) {
34
37
setData ( { ...res , url } )
38
+ // auto sub
39
+ handleSubscribe ( { ...res , url } )
35
40
}
36
41
setLoading ( false )
37
42
} )
@@ -40,9 +45,43 @@ export default function FeedProfile() {
40
45
setLoading ( false )
41
46
} )
42
47
}
43
- } , [ url , source ] )
48
+ } , [ url , feed ] )
44
49
45
- const favicon = source ?. favicon || data ?. favicon
50
+ const handleSubscribe = async ( _data ?: FeedData ) => {
51
+ try {
52
+ const fd = _data || data
53
+
54
+ if ( ! fd ) {
55
+ return
56
+ }
57
+ const _feed : Feed = {
58
+ url : url as string ,
59
+ title : fd . title || '' ,
60
+ favicon : fd . favicon || '' ,
61
+ description : fd . description || '' ,
62
+ language : fd . language || '' ,
63
+ }
64
+
65
+ if ( feed ) {
66
+ if ( feed . deleted ) {
67
+ resubFeed ( _feed )
68
+ } else {
69
+ unsubFeed ( feed )
70
+ }
71
+ } else {
72
+ subFeed ( _feed )
73
+ if ( Array . isArray ( fd ?. entries ) ) {
74
+ createEntries ( fd . entries )
75
+ }
76
+ }
77
+ } catch ( error ) { }
78
+ }
79
+
80
+ const favicon = feed ?. favicon || data ?. favicon
81
+ const isSubed = feed && ! feed ?. deleted
82
+ const feedEntries = isSubed
83
+ ? entries . filter ( ( e ) => e . sourceUrl === url )
84
+ : data ?. entries || [ ]
46
85
47
86
return (
48
87
< YStack flex = { 1 } >
@@ -63,7 +102,7 @@ export default function FeedProfile() {
63
102
</ Text >
64
103
</ XStack >
65
104
}
66
- right = { < FeedInfo source = { data } /> }
105
+ right = { < FeedInfo source = { data } handleSubscribe = { handleSubscribe } /> }
67
106
/>
68
107
< YStack flex = { 1 } >
69
108
{ error && (
@@ -98,8 +137,8 @@ export default function FeedProfile() {
98
137
</ YStack >
99
138
) : (
100
139
< EntryList
101
- entries = { data ?. entries || [ ] }
102
- type = " tags"
140
+ entries = { feedEntries }
141
+ type = { ( from as FeedListType ) || ' tags' }
103
142
withHeader = { false }
104
143
/>
105
144
) }
0 commit comments