@@ -5,16 +5,16 @@ import { useRouter } from "next/navigation"
55import { PostsContextProvider } from "@/contexts/posts-context-provider"
66import { useUsernameContext } from "@/lib/hooks"
77import { Post } from "@/lib/types"
8+ import { baseUrl } from "@/lib/utils"
9+ import { postsDataSchema } from "@/lib/types"
810
9- type ClientProtectionProps = {
10- children : React . ReactNode
11- data : Post [ ]
12- }
11+ type ClientProtectionProps = { children : React . ReactNode }
1312
14- export function FeedClientWrapper ( { data , children } : ClientProtectionProps ) {
13+ export function FeedClientWrapper ( { children } : ClientProtectionProps ) {
1514 const router = useRouter ( )
1615 const { usernameState } = useUsernameContext ( )
1716 const [ hasMounted , setHasMounted ] = useState ( false )
17+ const [ posts , setPosts ] = useState < Post [ ] | null > ( null )
1818
1919 useEffect ( ( ) => {
2020 setHasMounted ( true )
@@ -26,6 +26,20 @@ export function FeedClientWrapper({ data, children }: ClientProtectionProps) {
2626 }
2727 } , [ hasMounted , usernameState , router ] )
2828
29+ useEffect ( ( ) => {
30+ async function loadPosts ( ) {
31+ const response = await fetch ( baseUrl )
32+ const data : unknown = await response . json ( )
33+ const validatedData = postsDataSchema . safeParse ( data )
34+ setPosts ( validatedData . success ? validatedData . data . results : [ ] )
35+ }
36+ loadPosts ( )
37+ } , [ ] )
38+
39+ if ( posts === null ) {
40+ return < p className = "text-center" > Loading posts...</ p >
41+ }
42+
2943 // no conditional null return to avoid hydration mismatch
30- return < PostsContextProvider data = { data } > { children } </ PostsContextProvider >
44+ return < PostsContextProvider data = { posts } > { children } </ PostsContextProvider >
3145}
0 commit comments