-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlueBaseApp.tsx
114 lines (94 loc) 路 2.41 KB
/
BlueBaseApp.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { BlueBase, BlueBaseProgress, BootOptions } from '../../BlueBase';
import {
BlueBaseAppError,
BlueBaseAppErrorProps,
BlueBaseAppLoading,
BlueBaseAppLoadingProps,
BlueBaseRoot,
} from '..';
import React from 'react';
const MISSING_ERROR = 'An unknown error occured.';
export interface BlueBaseAppProps extends Partial<BootOptions> {
/** BlueBase Context */
BB?: BlueBase;
/** Component rendered when an error occurs */
ErrorComponent: React.ComponentType<BlueBaseAppErrorProps>;
/** Component rendered when an error occurs */
LoadingComponent: React.ComponentType<BlueBaseAppLoadingProps>;
/**
* If this prop is provided, this tree will be rendered instead of BlueBase's own view.
*/
children?: React.ReactNode;
/**
* Used to locate this view in end-to-end tests.
*/
testID?: string;
}
interface BlueBaseAppState {
/**
* Has the app booted yet
*/
readonly booted: boolean;
/**
* Are we loading the app
*/
readonly loading: boolean;
/**
* Any errors occured while booting the app
*/
readonly error: any;
/**
* BlueBase instance used by the app
*/
readonly BB: BlueBase;
}
/**
* # 馃殌 BlueBaseApp
*
* The main BlueBase app. This is the top level component in BlueBase. Takes care
* of initialisation, and renders either children, or app with routing.
*
* ## Usage
* ```jsx
* <BlueBaseApp BB={BB} plugins={{}} filter={{}} themes={{}} />
* ```
*/
export class BlueBaseApp extends React.Component<BlueBaseAppProps, BlueBaseAppState> {
static defaultProps: Partial<BlueBaseAppProps> = {
ErrorComponent: BlueBaseAppError,
LoadingComponent: BlueBaseAppLoading,
};
readonly state: BlueBaseAppState = {
BB: this.props.BB || new BlueBase(),
booted: false,
error: null,
loading: true,
};
async componentDidMount() {
const BB = this.state.BB;
BB.boot({ ...this.props, onProgress: this.onProgress });
}
onProgress = async (params: BlueBaseProgress) => {
await this.setState(params);
}
componentDidCatch(error: Error | null) {
this.setState({
error: error || new Error(MISSING_ERROR),
});
}
render() {
const { loading, error, BB } = this.state;
const {
ErrorComponent = BlueBaseAppError,
LoadingComponent = BlueBaseAppLoading,
children,
} = this.props;
if (loading) {
return <LoadingComponent BB={BB} />;
}
if (error) {
return <ErrorComponent error={error} BB={BB} />;
}
return <BlueBaseRoot BB={BB}>{children}</BlueBaseRoot>;
}
}