2022-10-14 01:16:22 +00:00
|
|
|
/* eslint-disable react-hooks/rules-of-hooks */
|
2022-05-22 08:36:05 +00:00
|
|
|
import { isEmpty } from 'lodash'
|
2022-03-09 02:43:30 +00:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useState, useEffect } from 'react'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { IS_PRIVATE_MANIFOLD } from 'common/envs/constants'
|
2022-03-09 02:43:30 +00:00
|
|
|
|
|
|
|
type PropzProps = {
|
|
|
|
// Params from the router query
|
|
|
|
params: any
|
|
|
|
}
|
|
|
|
|
|
|
|
// getStaticPropz should exactly match getStaticProps
|
|
|
|
// This allows us to client-side render the page for authenticated users.
|
|
|
|
// TODO: Could cache the result using stale-while-revalidate: https://swr.vercel.app/
|
|
|
|
export function usePropz(
|
2022-10-14 01:16:22 +00:00
|
|
|
initialProps: Record<string, unknown>,
|
2022-03-09 02:43:30 +00:00
|
|
|
getStaticPropz: (props: PropzProps) => Promise<any>
|
|
|
|
) {
|
|
|
|
// If props were successfully server-side generated, just use those
|
2022-05-22 08:36:05 +00:00
|
|
|
if (!isEmpty(initialProps)) {
|
2022-03-09 02:43:30 +00:00
|
|
|
return initialProps
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, get params from router
|
|
|
|
const router = useRouter()
|
|
|
|
const params = router.query
|
|
|
|
|
|
|
|
const [propz, setPropz] = useState<any>(undefined)
|
|
|
|
useEffect(() => {
|
|
|
|
if (router.isReady) {
|
|
|
|
getStaticPropz({ params }).then((result) => setPropz(result.props))
|
|
|
|
}
|
2022-10-14 01:16:22 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-03-09 02:43:30 +00:00
|
|
|
}, [params])
|
|
|
|
return propz
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conditionally disable SSG for private Manifold instances
|
|
|
|
export function fromPropz(getStaticPropz: (props: PropzProps) => Promise<any>) {
|
|
|
|
return IS_PRIVATE_MANIFOLD ? async () => ({ props: {} }) : getStaticPropz
|
|
|
|
}
|