1
1
import { Theme } from '@logto/schemas' ;
2
2
import type { TFuncKey } from 'i18next' ;
3
- import { useContext } from 'react' ;
3
+ import { useContext , useMemo } from 'react' ;
4
4
5
5
import StaticPageLayout from '@/Layout/StaticPageLayout' ;
6
6
import PageContext from '@/Providers/PageContextProvider/PageContext' ;
@@ -19,22 +19,40 @@ type Props = {
19
19
readonly rawMessage ?: string ;
20
20
} ;
21
21
22
+ const determineErrorKeysBySearchParams = ( ) :
23
+ | { titleKey : TFuncKey ; messageKey : TFuncKey }
24
+ | undefined => {
25
+ const searchParams = new URLSearchParams ( window . location . search ) ;
26
+
27
+ if ( searchParams . get ( 'code' ) ?. startsWith ( 'one_time_token.' ) ) {
28
+ return { titleKey : 'error.invalid_link' , messageKey : 'error.invalid_link_description' } ;
29
+ }
30
+
31
+ return undefined ;
32
+ } ;
33
+
34
+ /**
35
+ * Error page that accepts data from other pages or directly from the URL params.
36
+ */
22
37
const ErrorPage = ( { title = 'description.not_found' , message, rawMessage } : Props ) => {
23
38
const { theme } = useContext ( PageContext ) ;
24
- const errorMessage = Boolean ( rawMessage ?? message ) ;
39
+ const { titleKey = title , messageKey = message } = determineErrorKeysBySearchParams ( ) ?? { } ;
40
+
41
+ const errorMessage = useMemo (
42
+ ( ) => rawMessage ?? < DynamicT forKey = { messageKey } /> ,
43
+ [ rawMessage , messageKey ]
44
+ ) ;
25
45
26
46
return (
27
47
< StaticPageLayout >
28
- < PageMeta titleKey = { title } />
48
+ < PageMeta titleKey = { titleKey } />
29
49
{ history . length > 1 && < NavBar /> }
30
50
< div className = { styles . container } >
31
51
{ theme === Theme . Light ? < EmptyState /> : < EmptyStateDark /> }
32
52
< div className = { styles . title } >
33
- < DynamicT forKey = { title } />
53
+ < DynamicT forKey = { titleKey } />
34
54
</ div >
35
- { errorMessage && (
36
- < div className = { styles . message } > { rawMessage ?? < DynamicT forKey = { message } /> } </ div >
37
- ) }
55
+ { errorMessage && < div className = { styles . message } > { errorMessage } </ div > }
38
56
< SupportInfo />
39
57
</ div >
40
58
</ StaticPageLayout >
0 commit comments