-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWidgetStore.tsx
195 lines (187 loc) · 4.93 KB
/
WidgetStore.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { UseDisclosureReturn } from '@chakra-ui/react';
import React from 'react';
import { Layout } from 'react-grid-layout';
import { v4 as uuidv4 } from 'uuid';
import { useStoreContext } from '../../../../store/useStore';
import { TokenBalanceChartWidget } from '../../components/TokenBalanceChartWidget/TokenBalanceChartWidget';
import story from '../../components/TokenBalanceChartWidget/TokenBalanceChartWidget.stories';
import { TokenBalanceWidget } from '../../components/TokenBalanceWidget/TokenBalanceWidget';
import {
WidgetName,
WidgetSettingsData,
} from '../../components/WidgetsLayout/WidgetsLayout';
import {
AvailableWidgets,
WidgetSize,
WidgetStore as WidgetStoreComponent,
} from '../../components/WidgetStore/WidgetStore';
// list of available widgets to be displayed in the WidgetStore component
const availableWidgets: AvailableWidgets = [
{
// name of the underlying widget container, that will be rendered upon addition of the widget
name: 'TokenBalanceWidget',
displayName: 'Token Balance',
// default settings for the widget
settings: {
token: '0',
historicalPeriod: '30d',
},
// props to render the example component with
props: {
token: {
id: '0',
name: 'Tezos',
symbol: 'XTZ',
contract: {
address: '0',
},
metadata: {
decimals: '6',
},
},
// TODO: use user's actual selected currency here
currency: 'USD',
spotPriceToken: {
id: '0',
level: '1',
currency: 'USD',
price: '1',
},
spotPriceTokenHistorical: {
id: '0',
level: '1',
currency: 'USD',
price: '1',
},
spotPriceNativeToken: {
id: '0',
level: '1',
currency: 'USD',
price: '1',
},
spotPriceNativeTokenHistorical: {
id: '0',
level: '1',
currency: 'USD',
price: '1',
},
balance: {
id: '0',
level: '0',
amount: '100000',
},
historicalBalance: {
id: '0',
level: '0',
amount: '80000',
},
isLoading: false,
settingsDisabled: true,
// mocked event handlers
onWidgetRemove: () => {},
onSettingsChange: () => {},
},
// component to render the 'example' widget with in the widget store
component: TokenBalanceWidget,
// size of the component on the dashboard widget layout grid
size: {
w: 4,
h: 3,
},
maxWidth: '33%',
},
{
// name of the underlying widget container, that will be rendered upon addition of the widget
name: 'TokenBalanceChartWidget',
displayName: 'Relative token balance change',
// default settings for the widget
settings: {
token: '0',
historicalPeriod: '30d',
},
// props to render the example component with
props: {
token: {
id: '0',
name: 'Tezos',
symbol: 'XTZ',
contract: {
address: '0',
},
metadata: {
decimals: '6',
},
},
spotPriceToken: {
id: '0',
level: '1',
currency: 'USD',
price: '1',
},
spotPriceNativeToken: {
id: '0',
level: '1',
currency: 'USD',
price: '1',
},
isLoading: false,
settingsDisabled: true,
historicalBalances: story.args?.historicalBalances,
// mocked event handlers
onWidgetRemove: () => {},
onSettingsChange: () => {},
},
// component to render the 'example' widget with in the widget store
component: TokenBalanceChartWidget,
// size of the component on the dashboard widget layout grid
size: {
w: 6,
h: 6,
},
maxWidth: '50%',
},
];
export type WidgetStoreProps = UseDisclosureReturn;
export const WidgetStore: React.FC<WidgetStoreProps> = ({
isOpen,
onClose,
}) => {
const [state, dispatch] = useStoreContext();
const handleWidgetSelection = (
widget: WidgetName,
settings: WidgetSettingsData,
size: WidgetSize
): void => {
// generate a new random uuid for the widget
const id = uuidv4();
const newWidgetCoordinates: Layout = {
i: id,
// TODO: @matehoo, what exactly does this do?
x: (state.settings.widgets.length * size.w) % 12,
y: Infinity, // puts it at the bottom
w: size.w,
h: size.h,
};
// close the WidgetStore
onClose();
// add the selected widget to the state
// delay to mitigate grid layout performance issues
setTimeout(() => {
dispatch({
type: 'ADD_WIDGET',
payload: {
layout: [...state.settings.layout, newWidgetCoordinates],
widget: { name: widget, id, settings },
},
});
}, 200);
};
return (
<WidgetStoreComponent
availableWidgets={availableWidgets}
isOpen={isOpen}
onAddWidget={handleWidgetSelection}
onClose={onClose}
/>
);
};