1
1
'use client'
2
2
import { getGroupsAction } from '@/app/groups/actions'
3
+ import { AddGroupByUrlButton } from '@/app/groups/add-group-by-url-button'
3
4
import {
4
5
RecentGroups ,
5
6
getArchivedGroups ,
@@ -10,7 +11,7 @@ import { Button } from '@/components/ui/button'
10
11
import { getGroups } from '@/lib/api'
11
12
import { Loader2 } from 'lucide-react'
12
13
import Link from 'next/link'
13
- import { SetStateAction , useEffect , useState } from 'react'
14
+ import { PropsWithChildren , SetStateAction , useEffect , useState } from 'react'
14
15
import { RecentGroupListCard } from './recent-group-list-card'
15
16
16
17
export type RecentGroupsState =
@@ -54,7 +55,7 @@ function sortGroups(
54
55
export function RecentGroupList ( ) {
55
56
const [ state , setState ] = useState < RecentGroupsState > ( { status : 'pending' } )
56
57
57
- useEffect ( ( ) => {
58
+ function loadGroups ( ) {
58
59
const groupsInStorage = getRecentGroups ( )
59
60
const starredGroups = getStarredGroups ( )
60
61
const archivedGroups = getArchivedGroups ( )
@@ -73,35 +74,43 @@ export function RecentGroupList() {
73
74
archivedGroups,
74
75
} )
75
76
} )
77
+ }
78
+
79
+ useEffect ( ( ) => {
80
+ loadGroups ( )
76
81
} , [ ] )
77
82
78
83
if ( state . status === 'pending' ) {
79
84
return (
80
- < p >
81
- < Loader2 className = "w-4 m-4 mr-2 inline animate-spin" /> Loading recent
82
- groups…
83
- </ p >
85
+ < GroupsPage reload = { loadGroups } >
86
+ < p >
87
+ < Loader2 className = "w-4 m-4 mr-2 inline animate-spin" /> Loading
88
+ recent groups…
89
+ </ p >
90
+ </ GroupsPage >
84
91
)
85
92
}
86
93
87
94
if ( state . groups . length === 0 ) {
88
95
return (
89
- < div className = "text-sm space-y-2" >
90
- < p > You have not visited any group recently.</ p >
91
- < p >
92
- < Button variant = "link" asChild className = "-m-4" >
93
- < Link href = { `/groups/create` } > Create one</ Link >
94
- </ Button > { ' ' }
95
- or ask a friend to send you the link to an existing one.
96
- </ p >
97
- </ div >
96
+ < GroupsPage reload = { loadGroups } >
97
+ < div className = "text-sm space-y-2" >
98
+ < p > You have not visited any group recently.</ p >
99
+ < p >
100
+ < Button variant = "link" asChild className = "-m-4" >
101
+ < Link href = { `/groups/create` } > Create one</ Link >
102
+ </ Button > { ' ' }
103
+ or ask a friend to send you the link to an existing one.
104
+ </ p >
105
+ </ div >
106
+ </ GroupsPage >
98
107
)
99
108
}
100
109
101
110
const { starredGroupInfo, groupInfo, archivedGroupInfo } = sortGroups ( state )
102
111
103
112
return (
104
- < >
113
+ < GroupsPage reload = { loadGroups } >
105
114
{ starredGroupInfo . length > 0 && (
106
115
< >
107
116
< h2 className = "mb-2" > Starred groups</ h2 >
@@ -132,7 +141,7 @@ export function RecentGroupList() {
132
141
</ div >
133
142
</ >
134
143
) }
135
- </ >
144
+ </ GroupsPage >
136
145
)
137
146
}
138
147
@@ -158,3 +167,28 @@ function GroupList({
158
167
</ ul >
159
168
)
160
169
}
170
+
171
+ function GroupsPage ( {
172
+ children,
173
+ reload,
174
+ } : PropsWithChildren < { reload : ( ) => void } > ) {
175
+ return (
176
+ < >
177
+ < div className = "flex flex-col sm:flex-row sm:items-center justify-between gap-2" >
178
+ < h1 className = "font-bold text-2xl flex-1" >
179
+ < Link href = "/groups" > My groups</ Link >
180
+ </ h1 >
181
+ < div className = "flex gap-2" >
182
+ < AddGroupByUrlButton reload = { reload } />
183
+ < Button asChild >
184
+ < Link href = "/groups/create" >
185
+ { /* <Plus className="w-4 h-4 mr-2" /> */ }
186
+ < > Create</ >
187
+ </ Link >
188
+ </ Button >
189
+ </ div >
190
+ </ div >
191
+ < div > { children } </ div >
192
+ </ >
193
+ )
194
+ }
0 commit comments