File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { usePageContext } from 'vike-react/usePageContext'
2
+
3
+ export function Page ( ) {
4
+ const pageContext = usePageContext ( )
5
+
6
+ let msg : string // Message shown to the user
7
+ const { abortReason, abortStatusCode } = pageContext
8
+ if ( abortReason ?. notAdmin ) {
9
+ // Handle `throw render(403, { notAdmin: true })`
10
+ msg = "You cannot access this page because you aren't an administrator."
11
+ } else if ( typeof abortReason === 'string' ) {
12
+ // Handle `throw render(abortStatusCode, `You cannot access ${someCustomMessage}`)`
13
+ msg = abortReason
14
+ } else if ( abortStatusCode === 403 ) {
15
+ // Handle `throw render(403)`
16
+ msg = "You cannot access this page because you don't have enough privileges."
17
+ } else if ( abortStatusCode === 401 ) {
18
+ // Handle `throw render(401)`
19
+ msg = "You cannot access this page because you aren't logged in. Please log in."
20
+ } else {
21
+ // Fallback error message
22
+ msg = pageContext . is404 ?
23
+ "This page doesn't exist." :
24
+ "Failed to fetch data; is teuthology-api reachable?"
25
+ }
26
+
27
+ console . log ( pageContext )
28
+ return < p > { msg } </ p >
29
+ }
30
+
31
+ // When using TypeScript you can define the type of `abortReason`
32
+ declare global {
33
+ namespace Vike {
34
+ interface PageContext {
35
+ abortReason ?:
36
+ | string
37
+ | { notAdmin : true }
38
+ }
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments