@@ -16,11 +16,12 @@ import {ProfileInfo} from 'web/components/profile/profile-info'
1616import { User } from 'common/user'
1717import { getUserForStaticProps } from 'common/supabase/users'
1818import { GetStaticPropsContext } from 'next'
19- import { CompassLoadingIndicator } from "web/components/widgets/loading-indicator" ;
19+ import { CompassLoadingIndicator } from "web/components/widgets/loading-indicator"
2020import Custom404 from '../404'
21- import { sleep } from "common/util/time" ;
22- import { isNativeMobile } from "web/lib/util/webview" ;
23- import { getPixelHeight } from "web/lib/util/css" ;
21+ import { sleep } from "common/util/time"
22+ import { isNativeMobile } from "web/lib/util/webview"
23+ import { getPixelHeight } from "web/lib/util/css"
24+ import { getPageData } from "web/lib/util/page-data" ;
2425
2526async function getUser ( username : string ) {
2627 const user = await getUserForStaticProps ( db , username )
@@ -39,29 +40,29 @@ async function getProfile(userId: string) {
3940 }
4041 }
4142 console . debug ( `Profile loaded after ${ i } tries` )
42- return profile ;
43+ return profile
4344}
4445
4546// getServerSideProps is a Next.js function that can be used to fetch data and render the contents of a page at request time.
4647// export async function getServerSideProps(context: any) {
4748// if (!isNativeMobile()) {
4849// // Not mobile → let SSG handle it
49- // return {notFound: true};
50+ // return {notFound: true}
5051// }
5152//
5253// // Mobile path: server-side fetch
53- // const username = context.params.username;
54- // const user = await getUser(username);
54+ // const username = context.params.username
55+ // const user = await getUser(username)
5556//
5657// if (!user) {
57- // return {props: {notFoundCustomText: 'User not found'}};
58+ // return {props: {notFoundCustomText: 'User not found'}}
5859// }
5960//
60- // const profile = await getProfile(user.id);
61+ // const profile = await getProfile(user.id)
6162//
6263// console.log('getServerSideProps', {user, profile, username})
6364//
64- // return {props: {user, profile, username}};
65+ // return {props: {user, profile, username}}
6566// }
6667
6768// SSG: static site generation
@@ -154,49 +155,37 @@ export default function UserPage(props: UserPageProps) {
154155 useEffect ( ( ) => {
155156 console . log ( 'safe-area-inset-bottom:' , getPixelHeight ( 'safe-area-inset-bottom' ) )
156157 console . log ( 'safe-area-inset-top:' , getPixelHeight ( 'safe-area-inset-top' ) )
157- } , [ ] ) ;
158+ } , [ ] )
158159
159160 const nativeMobile = isNativeMobile ( )
160161 const router = useRouter ( )
161162 const username = ( nativeMobile ? router . query . username : props . username ) as string
162- const [ user , setUser ] = useState < User | undefined > ( props . user )
163- const [ profile , setProfile ] = useState < ProfileRow | undefined > ( props . profile )
164- const [ notFoundCustomText , setNotFoundCustomText ] = useState ( props . notFoundCustomText )
163+ const [ fetchedProps , setFetchedProps ] = useState ( props )
165164 const [ loading , setLoading ] = useState ( nativeMobile )
166165
167- console . log ( 'UserPage state:' , { username, user , profile , notFoundCustomText , loading, nativeMobile} )
166+ console . log ( 'UserPage state:' , { username, fetchedProps , loading, nativeMobile} )
168167
169168 useEffect ( ( ) => {
170169 if ( nativeMobile ) {
171- // Mobile/WebView scenario: fetch profile dynamically
170+ // Mobile/WebView scenario: fetch profile dynamically from the remote web server (to benefit from SSR and ISR)
172171 async function load ( ) {
173172 setLoading ( true )
174- const fetchedUser = await getUser ( username )
175- if ( ! fetchedUser ) return
176- if ( fetchedUser . username !== username ) {
177- console . debug ( 'Found a case-insensitive match for native mobile' )
178- await router . push ( `/${ fetchedUser . username } ` )
179- }
180- setUser ( fetchedUser )
181- console . debug ( 'fetched user for native mobile:' , fetchedUser )
182- const fetchedProfile = await getProfile ( fetchedUser . id )
183- if ( ! fetchedProfile ) {
184- setNotFoundCustomText ( `${ username } hasn't created a profile yet.` )
185- } else {
186- setProfile ( fetchedProfile )
187- console . debug ( 'fetched profile for native mobile:' , fetchedProfile )
173+ try {
174+ const _props = await getPageData ( username )
175+ setFetchedProps ( _props )
176+ } catch ( e ) {
177+ console . error ( 'Failed to fetch profile for native mobile' , e )
178+ setFetchedProps ( { username, notFoundCustomText : 'Failed to fetch profile.' } )
188179 }
189180 setLoading ( false )
190181 }
191182
192183 load ( )
193184 } else {
194- setUser ( props . user )
195- setProfile ( props . profile )
196- setNotFoundCustomText ( props . notFoundCustomText )
185+ setFetchedProps ( props )
197186 }
198187 // On web, initialProfile from SSR/ISR is already loaded
199- } , [ username , nativeMobile ] ) ;
188+ } , [ username , nativeMobile ] )
200189
201190 if ( loading ) {
202191 return < PageBase
@@ -206,11 +195,11 @@ export default function UserPage(props: UserPageProps) {
206195 </ PageBase >
207196 }
208197
209- if ( notFoundCustomText ) {
210- return < Custom404 customText = { notFoundCustomText } />
198+ if ( fetchedProps . notFoundCustomText ) {
199+ return < Custom404 customText = { fetchedProps . notFoundCustomText } />
211200 }
212201
213- if ( ! user ) {
202+ if ( ! fetchedProps . user ) {
214203 return < PageBase
215204 trackPageView = { 'user page' }
216205 className = { 'relative p-2 sm:pt-0' }
@@ -223,7 +212,7 @@ export default function UserPage(props: UserPageProps) {
223212 </ PageBase >
224213 }
225214
226- if ( user . isBannedFromPosting ) {
215+ if ( fetchedProps . user ? .isBannedFromPosting ) {
227216 return < PageBase
228217 trackPageView = { 'user page' }
229218 className = { 'relative p-2 sm:pt-0' }
@@ -236,7 +225,7 @@ export default function UserPage(props: UserPageProps) {
236225 </ PageBase >
237226 }
238227
239- if ( ! profile ) {
228+ if ( ! fetchedProps . profile ) {
240229 return < PageBase
241230 trackPageView = { 'user page' }
242231 className = { 'relative p-2 sm:pt-0' }
@@ -249,11 +238,7 @@ export default function UserPage(props: UserPageProps) {
249238 </ PageBase >
250239 }
251240
252- return < UserPageInner
253- user = { user }
254- username = { username }
255- profile = { profile }
256- />
241+ return < UserPageInner { ...fetchedProps as ActiveUserPageProps } />
257242}
258243
259244function UserPageInner ( props : ActiveUserPageProps ) {
0 commit comments