@@ -2,27 +2,24 @@ import 'windi.css'
2
2
import 'assets/styles/main.css'
3
3
import '../../third/qp/index.css'
4
4
5
- import NextApp from 'next/app'
6
5
import type { AppContext } from 'next/app'
6
+ import NextApp from 'next/app'
7
7
import { useRouter } from 'next/router'
8
8
import type { FC } from 'react'
9
- import React , { memo , useMemo , useRef } from 'react'
10
- import type { ToastContainerProps } from 'react-toastify'
11
- import { ToastContainer } from 'react-toastify'
12
- import { SWRConfig } from 'swr'
13
- import type { FullConfiguration } from 'swr/_internal'
9
+ import { useMemo } from 'react'
14
10
11
+ import { ProviderComposer } from '~/components/app/Composer'
15
12
import { NoDataErrorView } from '~/components/app/Error/no-data'
16
13
import { ErrorBoundary } from '~/components/app/ErrorBoundary'
17
14
import { BasicLayout } from '~/components/layouts/BasicLayout'
18
15
import { DebugLayout } from '~/components/layouts/DebugLayout'
19
- import type { InitialDataType } from '~/context/initial-data'
20
- import { InitialContextProvider } from '~/context/initial-data'
21
- import { RootStoreProvider } from '~/context/root-store'
16
+ import type { InitialDataType } from '~/provider/initial-data'
17
+ import { InitialContextProvider } from '~/provider/initial-data'
18
+ import { RootStoreProvider } from '~/provider/root-store'
19
+ import { SWRProvider } from '~/provider/swr'
22
20
import { isDev } from '~/utils/env'
23
- import { localStorageProvider } from '~/utils/swr'
24
21
25
- import { Content } from '../components/layouts/AppLayout'
22
+ import { AppLayout } from '../components/layouts/AppLayout'
26
23
import { attachRequestProxy , fetchInitialData } from '../utils/app'
27
24
28
25
interface DataModel {
@@ -35,12 +32,31 @@ const App: FC<DataModel & { Component: any; pageProps: any; err: any }> = (
35
32
36
33
const router = useRouter ( )
37
34
35
+ const PageProviders = useMemo (
36
+ ( ) => [
37
+ < SWRProvider key = "SWRProvider" /> ,
38
+ < ErrorBoundary key = "ErrorBoundary1" /> ,
39
+ < BasicLayout key = "BasicLayout" /> ,
40
+ < AppLayout key = "appLayout" /> ,
41
+ < ErrorBoundary key = "ErrorBoundary2" /> ,
42
+ ] ,
43
+ [ ] ,
44
+ )
45
+
46
+ const AppProviders = useMemo (
47
+ ( ) => [
48
+ < RootStoreProvider key = "RootStoreProvider" /> ,
49
+ < InitialContextProvider value = { initData } key = "InitialContextProvider" /> ,
50
+ ] ,
51
+ [ initData ] ,
52
+ )
53
+
38
54
const Inner = useMemo ( ( ) => {
39
55
// 兜底页
40
56
return initData . aggregateData ? (
41
- < Wrapper >
57
+ < ProviderComposer contexts = { PageProviders } >
42
58
< Component { ...pageProps } />
43
- </ Wrapper >
59
+ </ ProviderComposer >
44
60
) : (
45
61
< NoDataErrorView />
46
62
)
@@ -54,47 +70,8 @@ const App: FC<DataModel & { Component: any; pageProps: any; err: any }> = (
54
70
</ RootStoreProvider >
55
71
)
56
72
}
57
- return (
58
- < RootStoreProvider >
59
- < InitialContextProvider value = { initData } > { Inner } </ InitialContextProvider >
60
- </ RootStoreProvider >
61
- )
73
+ return < ProviderComposer contexts = { AppProviders } > { Inner } </ ProviderComposer >
62
74
}
63
- const Wrapper = memo ( ( props ) => {
64
- const toastOptions = useRef < ToastContainerProps > ( {
65
- autoClose : 3000 ,
66
- pauseOnHover : true ,
67
- hideProgressBar : true ,
68
- newestOnTop : true ,
69
- closeOnClick : true ,
70
- closeButton : false ,
71
- toastClassName : ( ) => '' ,
72
- bodyClassName : ( ) => '' ,
73
- } )
74
- const swrConfig = useRef <
75
- Partial < FullConfiguration > & {
76
- provider ?: any
77
- }
78
- > ( {
79
- refreshInterval : 30_000 ,
80
- provider : localStorageProvider ,
81
- } )
82
- return (
83
- < >
84
- < SWRConfig value = { swrConfig . current } >
85
- < ErrorBoundary >
86
- < BasicLayout >
87
- < Content >
88
- < ErrorBoundary > { props . children } </ ErrorBoundary >
89
- </ Content >
90
- </ BasicLayout >
91
- </ ErrorBoundary >
92
- </ SWRConfig >
93
-
94
- < ToastContainer { ...toastOptions . current } />
95
- </ >
96
- )
97
- } )
98
75
// @ts -ignore
99
76
App . getInitialProps = async ( props : AppContext ) => {
100
77
const ctx = props . ctx
@@ -109,6 +86,7 @@ App.getInitialProps = async (props: AppContext) => {
109
86
return await NextApp . getInitialProps ( props )
110
87
} catch ( e ) {
111
88
// TODO next rfc Layout, 出了就重构这里
89
+ // 2023 tmd next rfc 全是大饼,根本没法用
112
90
// 只有无数据 也就是 服务端不跑起来 或者接口不对的时候 捕获异常
113
91
// 这是为什么呢 说来说去还是 nextjs 太辣鸡了 只能各种 hack
114
92
// 只能这样了
0 commit comments