Files
SnowLuma/.pnpm-store/v11/files/39/17a698f4ff4cbc759119f57c7ee427f79102779190533538b98a0f4cbf77f6ff3ae984998da351241fd95a3a33b4c48dabe8d1e532e36f7770052034be8fc0
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

81 lines
2.2 KiB
Plaintext

import * as React from 'react'
import { isNotFound } from '@tanstack/router-core'
import { isServer } from '@tanstack/router-core/isServer'
import { useStore } from '@tanstack/react-store'
import { CatchBoundary } from './CatchBoundary'
import { useRouter } from './useRouter'
import type { ErrorInfo } from 'react'
import type { NotFoundError } from '@tanstack/router-core'
export function CatchNotFound(props: {
fallback?: (error: NotFoundError) => React.ReactElement
onCatch?: (error: Error, errorInfo: ErrorInfo) => void
children: React.ReactNode
}) {
const router = useRouter()
if (isServer ?? router.isServer) {
const pathname = router.stores.location.get().pathname
const status = router.stores.status.get()
const resetKey = `not-found-${pathname}-${status}`
return (
<CatchBoundary
getResetKey={() => resetKey}
onCatch={(error, errorInfo) => {
if (isNotFound(error)) {
props.onCatch?.(error, errorInfo)
} else {
throw error
}
}}
errorComponent={({ error }) => {
if (isNotFound(error)) {
return props.fallback?.(error)
} else {
throw error
}
}}
>
{props.children}
</CatchBoundary>
)
}
// TODO: Some way for the user to programmatically reset the not-found boundary?
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
const pathname = useStore(
router.stores.location,
(location) => location.pathname,
)
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
const status = useStore(router.stores.status, (status) => status)
const resetKey = `not-found-${pathname}-${status}`
return (
<CatchBoundary
getResetKey={() => resetKey}
onCatch={(error, errorInfo) => {
if (isNotFound(error)) {
props.onCatch?.(error, errorInfo)
} else {
throw error
}
}}
errorComponent={({ error }) => {
if (isNotFound(error)) {
return props.fallback?.(error)
} else {
throw error
}
}}
>
{props.children}
</CatchBoundary>
)
}
export function DefaultGlobalNotFound() {
return <p>Not Found</p>
}