1
+ 'use-client'
2
+
3
+ import { useEffect } from 'react'
1
4
import { motion } from 'framer-motion'
2
5
import { useQuery } from 'urql'
3
6
6
9
setThreadsTab ,
7
10
useAnswerEngineStore
8
11
} from '@/lib/stores/answer-engine-store'
9
- import { contextInfoQuery } from '@/lib/tabby/query'
12
+ import { contextInfoQuery , listMyThreads , listThreads } from '@/lib/tabby/query'
10
13
import { cn } from '@/lib/utils'
11
14
import { IconSpinner } from '@/components/ui/icons'
12
15
import { Tabs , TabsContent , TabsList , TabsTrigger } from '@/components/ui/tabs'
@@ -25,13 +28,49 @@ export function ThreadFeeds({
25
28
className,
26
29
onNavigateToThread
27
30
} : ThreadFeedsProps ) {
28
- // FIXME rename
29
31
const threadsTab = useAnswerEngineStore ( state => state . threadsTab )
30
32
const [ allUsers , fetchingUsers ] = useAllMembers ( )
31
33
const [ { data : contextInfoData , fetching : fetchingSources } ] = useQuery ( {
32
34
query : contextInfoQuery
33
35
} )
34
36
37
+ const [ { data : persistedThreads , fetching : fetchingPersistedThreads } ] =
38
+ useQuery ( {
39
+ query : listThreads ,
40
+ variables : {
41
+ last : 1 ,
42
+ isEphemeral : false
43
+ }
44
+ } )
45
+ const [ { data : myThreads , fetching : fetchingMyThreads } ] = useQuery ( {
46
+ query : listMyThreads ,
47
+ variables : {
48
+ last : 1
49
+ }
50
+ } )
51
+
52
+ const loading =
53
+ fetchingPersistedThreads ||
54
+ fetchingMyThreads ||
55
+ fetchingSources ||
56
+ fetchingUsers
57
+ const hasPersistedThreads = ! ! persistedThreads ?. threads ?. edges ?. length
58
+ const hasMyThreads = ! ! myThreads ?. myThreads . edges . length
59
+
60
+ useEffect ( ( ) => {
61
+ if ( loading ) return
62
+ if ( ! hasPersistedThreads && ! hasMyThreads ) return
63
+
64
+ if ( ! hasPersistedThreads && threadsTab === 'all' ) {
65
+ setThreadsTab ( 'mine' )
66
+ } else if ( ! hasMyThreads && threadsTab === 'mine' ) {
67
+ setThreadsTab ( 'all' )
68
+ }
69
+ } , [ loading , hasPersistedThreads , hasMyThreads , threadsTab ] )
70
+
71
+ // if there's no thread, hide the section
72
+ if ( ! hasPersistedThreads && ! hasMyThreads ) return null
73
+
35
74
return (
36
75
< ThreadFeedsContext . Provider
37
76
value = { {
@@ -55,8 +94,7 @@ export function ThreadFeeds({
55
94
} }
56
95
>
57
96
< LoadingWrapper
58
- // FIXME fetching threads
59
- loading = { fetchingSources || fetchingUsers }
97
+ loading = { loading }
60
98
fallback = {
61
99
< div className = "flex justify-center" >
62
100
< IconSpinner className = "h-8 w-8" />
@@ -71,18 +109,22 @@ export function ThreadFeeds({
71
109
>
72
110
< div className = "flex items-center justify-between pb-3" >
73
111
< TabsList className = "w-full justify-start border-b bg-transparent p-0" >
74
- < TabsTrigger
75
- value = "all"
76
- className = "rounded-none border-b-2 border-b-transparent bg-transparent px-3 py-2 text-base font-medium shadow-none data-[state=active]:border-b-primary data-[state=active]:text-foreground data-[state=active]:shadow-none"
77
- >
78
- Recent Activities
79
- </ TabsTrigger >
80
- < TabsTrigger
81
- value = "mine"
82
- className = "rounded-none border-b-2 border-b-transparent bg-transparent px-3 py-2 text-base font-medium shadow-none data-[state=active]:border-b-primary data-[state=active]:text-foreground data-[state=active]:shadow-none"
83
- >
84
- My Activities
85
- </ TabsTrigger >
112
+ { ! ! hasPersistedThreads && (
113
+ < TabsTrigger
114
+ value = "all"
115
+ className = "rounded-none border-b-2 border-b-transparent bg-transparent px-3 py-2 text-base font-medium shadow-none data-[state=active]:border-b-primary data-[state=active]:text-foreground data-[state=active]:shadow-none"
116
+ >
117
+ Recent Activities
118
+ </ TabsTrigger >
119
+ ) }
120
+ { ! ! hasMyThreads && (
121
+ < TabsTrigger
122
+ value = "mine"
123
+ className = "rounded-none border-b-2 border-b-transparent bg-transparent px-3 py-2 text-base font-medium shadow-none data-[state=active]:border-b-primary data-[state=active]:text-foreground data-[state=active]:shadow-none"
124
+ >
125
+ My Activities
126
+ </ TabsTrigger >
127
+ ) }
86
128
</ TabsList >
87
129
</ div >
88
130
< TabsContent value = "all" >
0 commit comments