@@ -4,13 +4,16 @@ import type { UserProfile } from "@durhack/durhack-common/types/user-profile"
44import { useToast } from "@durhack/web-components/hooks/use-toast"
55import { Label } from "@durhack/web-components/ui/label"
66import { UpdateIcon } from "@radix-ui/react-icons"
7+ import { redirect , usePathname } from "next/navigation"
78import * as React from "react"
89import useSWR from "swr"
10+ import ModuleError from "module-error"
911
1012import { ApplicationStatusBadge } from "@/components/dashboard/application-status-indicator"
1113import { siteConfig } from "@/config/site"
1214import { isLoaded } from "@/lib/is-loaded"
1315import { cn } from "@/lib/utils"
16+ import { hasCode } from "@/lib/type-guards"
1417
1518import { ProfileCheckInButton } from "./check-in-button"
1619import { CvUploadBadge } from "./cv-upload-badge"
@@ -24,7 +27,7 @@ const profileFetcher = async (url: string): Promise<UserProfile> => {
2427 credentials : "include" ,
2528 } )
2629
27- if ( response . status === 404 ) throw new Error ( "Profile not found" )
30+ if ( response . status === 404 ) throw new ModuleError ( "Profile not found" , { code : "ERR_NOT_FOUND" } )
2831 if ( ! response . ok ) throw new Error ( "Failed to fetch data" )
2932 const payload : unknown = await response . json ( )
3033 if ( typeof payload !== "object" || Array . isArray ( payload ) )
@@ -44,14 +47,20 @@ function UserAttribute({ children, className, ...props }: React.HTMLAttributes<H
4447
4548export default function ProfilePage ( props : { params : Promise < { userId : string } > } ) {
4649 const params = React . use ( props . params )
50+ const pathname = usePathname ( )
4751 const { toast } = useToast ( )
4852
4953 const {
5054 data : profile ,
5155 mutate : mutateProfile ,
5256 error : profileError ,
5357 isLoading : profileIsLoading ,
54- } = useSWR ( `${ siteConfig . apiUrl } /profile/${ params . userId } ` , profileFetcher )
58+ } = useSWR < UserProfile , unknown > ( `${ siteConfig . apiUrl } /profile/${ params . userId } ` , profileFetcher )
59+
60+ if ( hasCode ( profileError ) && profileError . code === "ERR_NOT_FOUND" ) {
61+ const params = new URLSearchParams ( { status_code : "404" , from : pathname } )
62+ redirect ( `/error?${ params } ` )
63+ }
5564
5665 if ( ! isLoaded ( profile , profileIsLoading , profileError ) )
5766 return (
0 commit comments