-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.tsx
180 lines (171 loc) · 5.71 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { ParcelConfigObject } from 'single-spa';
import { hasOwn, useRootComponentProps } from '@akashaorg/ui-awf-hooks';
import { BlockInstanceMethods, ContentBlockModes } from '@akashaorg/typings/lib/ui';
import { GetContentBlockByIdQuery } from '@akashaorg/typings/lib/sdk/graphql-operation-types-new';
import { useGetContentBlockByIdLazyQuery } from '@akashaorg/ui-awf-hooks/lib/generated/apollo';
import { RenderError } from './render-error';
import { RenderBlock } from './render-block';
import { BlockError } from '../block-error-card';
import { resolveConfigs } from '../resolve-configs';
import { MatchingBlock } from '../common.types';
export type ContentBlockExtensionProps = {
blockRef?: React.RefObject<BlockInstanceMethods>;
fetchError?: BlockError;
contentLoadError?: BlockError;
installButtonLabel: string;
notInstalledTitle: string;
notInstalledDescription1: string;
notInstalledDescription2: string;
// cache to prevent re-loading block config with the same block id
cacheBlockConfig?: boolean;
onError?: (error: Error) => void;
onClickInstall: (e: React.SyntheticEvent) => void;
} & (
| {
blockID: string;
}
| {
blockData: GetContentBlockByIdQuery['node'];
matchingBlocks?: MatchingBlock[];
error?: string;
refreshLabel?: string;
onRefresh?: () => void;
}
);
export const ContentBlockExtension: React.FC<ContentBlockExtensionProps> = props => {
const {
blockRef,
fetchError,
contentLoadError,
installButtonLabel,
notInstalledTitle,
notInstalledDescription1,
notInstalledDescription2,
cacheBlockConfig,
onError,
onClickInstall,
...remainingProps
} = props;
const { logger, getExtensionsPlugin } = useRootComponentProps();
const contentBlockStoreRef = useRef(getExtensionsPlugin()?.contentBlockStore);
const [hasContentLoadError, setHasContentLoadError] = useState(false);
const [state, setState] = useState<{
parcels: (MatchingBlock & { config: ParcelConfigObject })[];
}>({
parcels: [],
});
const [fetchBlockInfo, blockInfoQuery] = useGetContentBlockByIdLazyQuery();
// fetch data error
const fetchDataError = useMemo(() => {
return hasOwn(remainingProps, 'blockData')
? remainingProps?.error
: blockInfoQuery.error?.message;
}, [remainingProps, blockInfoQuery]);
// block data
const blockData = useMemo(() => {
if (hasOwn(remainingProps, 'blockData')) {
if (remainingProps.blockData && hasOwn(remainingProps.blockData, 'id')) {
return remainingProps.blockData;
}
}
if (blockInfoQuery.data?.node && hasOwn(blockInfoQuery.data?.node, 'id')) {
return blockInfoQuery.data?.node;
}
return null;
}, [remainingProps, blockInfoQuery]);
// find matching blocks
const matchingBlocks: MatchingBlock[] = useMemo(() => {
if (hasOwn(remainingProps, 'blockData') && remainingProps?.matchingBlocks)
return remainingProps.matchingBlocks;
return !blockData ? [] : contentBlockStoreRef.current.getMatchingBlocks(blockData);
}, [blockData, remainingProps]);
useLayoutEffect(() => {
if (matchingBlocks && matchingBlocks.length !== state.parcels.length) {
resolveConfigs({ matchingBlocks, mode: ContentBlockModes.READONLY, cache: cacheBlockConfig })
.then(newBlocks => {
setState({
parcels: newBlocks,
});
})
.catch(err => {
setHasContentLoadError(true);
logger.error('failed to load content blocks', err);
});
} else if (blockInfoQuery.called && !blockInfoQuery.loading) {
setState({
parcels: [],
});
}
}, [
logger,
blockInfoQuery.called,
blockInfoQuery.loading,
matchingBlocks,
state,
cacheBlockConfig,
]);
/**
* remainingProps could have either just the blockID or other necessary data passed from the parent
* Each of the variables and hooks below evaluates the props available in the remainingProps and acts accordingly.
*/
// if only blockID, fetch the necessary data
useEffect(() => {
if (hasOwn(remainingProps, 'blockID')) {
fetchBlockInfo({
variables: {
id: remainingProps.blockID,
},
}).catch(err => logger.error(`failed to fetch content block: ${JSON.stringify(err)}`));
}
}, [logger, fetchBlockInfo, remainingProps]);
useEffect(() => {
return () => {
setState({
parcels: [],
});
};
}, []);
const appInfo = useMemo(() => {
if (blockData) {
return blockData.appVersion?.application;
}
}, [blockData]);
if (hasContentLoadError || fetchDataError) {
return (
<RenderError
fetchError={fetchError}
contentLoadError={contentLoadError}
fetchDataError={fetchDataError}
hasContentLoadError={hasContentLoadError}
refreshLabel={hasOwn(remainingProps, 'refreshLabel') ? remainingProps.refreshLabel : ''}
handleRefresh={() => {
if (hasOwn(remainingProps, 'blockID')) {
blockInfoQuery.refetch({
id: remainingProps?.blockID,
});
}
if (hasOwn(remainingProps, 'blockData')) {
remainingProps.onRefresh?.();
}
}}
/>
);
}
return (
<RenderBlock
state={state}
appInfo={appInfo}
blockId={blockData?.id}
blockRef={blockRef}
blockData={blockData}
matchingBlocks={matchingBlocks}
notInstalledTitle={notInstalledTitle}
installButtonLabel={installButtonLabel}
notInstalledDescription1={notInstalledDescription1}
notInstalledDescription2={notInstalledDescription2}
onError={onError}
onClickInstall={onClickInstall}
/>
);
};