-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStore.ts
282 lines (255 loc) · 8.44 KB
/
Store.ts
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import produce from 'immer';
import {
getInitialHydrationData,
getStoreWithHydrationSettings,
} from 'stores/extensions/Hydration';
import { JournalDataStoreNames } from 'stores/names';
import { OpsLogFlowDocument } from 'types';
import { devtoolsOptions } from 'utils/store-utils';
import { create, StoreApi } from 'zustand';
import { devtools, NamedSet } from 'zustand/middleware';
import { JournalDataLogsState } from './types';
// Since journal data reads data and always returns an array it gets a little weird
// to hydrate the store synchronously. This means we have to wait for one of two
// things to happen:
// 1. We parsed all bytes and have no document
// 2. We have at least one document
// This is why we have a hydrate function but do not mark the hydrated/noData fields
// instead of within the normal hydrate function.
const getReadyToRenderFlags = (
docs: OpsLogFlowDocument[] | null,
olderFinished: boolean
) => {
if (!docs) {
return {
// We only know there is no data when we're done reading all bytes
noData: olderFinished,
// We have read all data and have still have no docs so we are hydrated
hydrated: olderFinished,
};
} else {
return {
noData: olderFinished ? docs.length === 0 : false,
// We are hydrated if we read all bytes or we have gotten _some_ docs back to render
hydrated: olderFinished || docs.length > 0,
};
}
};
const getInitialStateData = (): Pick<
JournalDataLogsState,
| 'allowFetchingMore'
| 'documents'
| 'lastCount'
| 'lastParsed'
| 'lastTopUuid'
| 'fetchingNewer'
| 'fetchingOlder'
| 'noData'
| 'olderFinished'
| 'refresh'
| 'scrollToWhenDone'
| 'tailNewLogs'
> => ({
allowFetchingMore: false,
documents: null,
lastCount: -1,
lastParsed: -1,
scrollToWhenDone: [-1, 'end'],
lastTopUuid: null,
fetchingNewer: false,
fetchingOlder: false,
noData: false,
olderFinished: false,
refresh: null,
tailNewLogs: false,
});
const getInitialState = (
set: NamedSet<JournalDataLogsState>,
get: StoreApi<JournalDataLogsState>['getState']
): JournalDataLogsState => ({
...getInitialStateData(),
...getInitialHydrationData(),
...getStoreWithHydrationSettings('JournalsData:Logs', set),
hydrate: async (docs, refresh, olderFinished, lastParsed, error) => {
const {
active,
addNewDocuments,
hydrated,
setHydrationErrorsExist,
setNetworkFailed,
setRefresh,
} = get();
if (!active || hydrated) {
return;
}
setRefresh(refresh);
setHydrationErrorsExist(Boolean(error));
if (error) {
setNetworkFailed(error.message);
addNewDocuments([], true, 0);
return;
}
addNewDocuments(docs, olderFinished, lastParsed);
},
fetchMoreLogs: (option) => {
const {
allowFetchingMore,
fetchingNewer,
fetchingOlder,
lastParsed,
olderFinished,
refresh,
setFetchingOlder,
setFetchingNewer,
} = get();
if (!allowFetchingMore || !refresh || fetchingNewer || fetchingOlder) {
return;
}
if (option === 'old') {
if (olderFinished) {
return;
}
setFetchingOlder(true);
refresh({
offset: 0,
endOffset: lastParsed,
});
} else {
setFetchingNewer(true);
refresh();
}
},
addNewDocuments: (docs, olderFinished, lastParsed) => {
console.log('addNewDocuments');
set(
produce((state: JournalDataLogsState) => {
if (!docs) {
const { hydrated, noData } = getReadyToRenderFlags(
docs,
olderFinished
);
state.hydrated = hydrated;
state.noData = noData;
return;
}
if (state.fetchingOlder) {
// When fetching newer keep the previous first item in view
// and then add the new to the start of the list
state.scrollToWhenDone = [docs.length + 1, 'start'];
state.documents = [...docs, ...(state.documents ?? [])];
state.fetchingOlder = false;
} else if (state.fetchingNewer) {
state.documents = [...(state.documents ?? []), ...docs];
// Since fetching newer adds items to the end the browser
// will keep the scroll in the same position. So we only set this
// if we're forcing failing of new logs
if (state.tailNewLogs) {
// We have 2 fake rows so add 2 here
state.scrollToWhenDone = [
state.documents.length + 2,
'start',
];
}
state.fetchingNewer = false;
} else {
// Initial hydration we want to set the array and scroll to near the bottom
state.scrollToWhenDone = [
Math.round(docs.length * 0.95),
'end',
];
state.documents = docs;
}
// Helper props for future calls and scrolling
state.olderFinished = Boolean(olderFinished);
state.lastParsed = lastParsed;
if (state.documents.length > 0) {
state.lastTopUuid = state.documents[0]._meta.uuid;
}
// Now the we have processed some documents we need to mark fields related to hydration
const { hydrated, noData } = getReadyToRenderFlags(
state.documents,
state.olderFinished
);
state.hydrated = hydrated;
state.noData = noData;
}),
false,
'JournalsData:Logs: Documents Added'
);
},
setAllowFetchingMore: (newState) => {
set(
produce((state: JournalDataLogsState) => {
state.allowFetchingMore = newState;
}),
false,
'JournalsData:Logs: Fetching more can start'
);
},
setRefresh: (newState) => {
set(
produce((state: JournalDataLogsState) => {
state.refresh = newState;
}),
false,
'JournalsData:Logs: Refresh set'
);
},
setTailNewLogs: (newState) => {
set(
produce((state: JournalDataLogsState) => {
state.tailNewLogs = newState;
}),
false,
'JournalsData:Logs: Tail new logs set'
);
},
setLastCount: (newState) => {
set(
produce((state: JournalDataLogsState) => {
state.lastCount = newState;
}),
false,
'JournalsData:Logs: Last Count Set'
);
},
setFetchingNewer: (newState) => {
set(
produce((state: JournalDataLogsState) => {
state.fetchingNewer = newState;
}),
false,
'JournalsData:Logs: Fetching Newer Set'
);
},
setFetchingOlder: (newState) => {
set(
produce((state: JournalDataLogsState) => {
state.fetchingOlder = newState;
}),
false,
'JournalsData:Logs: Fetching Older Set'
);
},
setOlderFinished: (newState) => {
set(
produce((state: JournalDataLogsState) => {
state.olderFinished = newState;
}),
false,
'JournalsData:Logs: Older Finished Set'
);
},
resetState: () => {
set(
{ ...getInitialStateData(), ...getInitialHydrationData() },
false,
'JournalsData:Logs: Reset'
);
},
});
export const createJournalDataLogsStore = (key: JournalDataStoreNames) => {
return create<JournalDataLogsState>()(
devtools((set, get) => getInitialState(set, get), devtoolsOptions(key))
);
};