-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathApp.js
More file actions
452 lines (426 loc) · 14.3 KB
/
App.js
File metadata and controls
452 lines (426 loc) · 14.3 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import React, { useState, useEffect, useMemo, useCallback } from "react";
import { Box, useMediaQuery, Typography, Rating } from "@mui/material";
import { HashRouter as Router, Switch, Route } from "react-router-dom";
import Upload from "./Upload";
import Results from "./Results";
import Login from "./Login";
import CssBaseline from "@mui/material/CssBaseline";
import { getDesignTokens, getThemedComponents } from "../conf/theme.ts";
import {
createTheme,
ThemeProvider,
responsiveFontSizes,
} from "@mui/material/styles";
import { simplifyApi } from "../utilities/simplifyApi";
import HarmonySidebar from "./HarmonySidebar";
import pattern from "../img/pattern.svg";
import { deepmerge } from "@mui/utils";
import { ColorModeContext } from "../contexts/ColorModeContext";
import { useData } from "../contexts/DataContext";
import { utils as XLSXutils, writeFile as XLSXwriteFile } from "xlsx";
import ReactGA from "react-ga4";
import CookieConsent, { getCookieConsentValue } from "react-cookie-consent";
import { ToastContainer, toast } from "react-toastify";
import MakeMeJSON from "./MakeMeJSON.js";
import "react-toastify/dist/ReactToastify.css";
import "../css/youtube.css";
function App() {
const [fullscreen, setFullscreen] = useState(false);
const [existingInstruments, setExistingInstruments] = useState([]);
const [apiData, setApiData] = useState({});
const [resultsOptions, setResultsOptions] = useState({
threshold: [70, 100],
searchTerm: "",
intraInstrument: false,
});
// Lock to light mode for compatibility with DiscoveryNext
const [mode, setMode] = useState("light");
const {
storeHarmonisation,
reportRating,
exampleInstruments,
match,
currentModel,
setCurrentModel,
getModels,
} = useData();
const [ratingValue, setRatingValue] = useState();
const [computedMatches, setComputedMatches] = useState();
const [fileInfos, setFileInfos] = useState();
const [allModels, setAllModels] = useState();
useEffect(() => {
const handleBeforeUnload = (event) => {
// stash the current fileInfos to sessionStorage so they can be retreived in the case of handling an import link
if (fileInfos.length)
sessionStorage["harmonyStashed"] = JSON.stringify(fileInfos);
};
window.addEventListener("beforeunload", handleBeforeUnload);
return () => {
window.removeEventListener("beforeunload", handleBeforeUnload);
};
}, [fileInfos]);
useEffect(() => {
if (
sessionStorage["harmonyStashed"] &&
sessionStorage["harmonyStashed"] !== "undefined"
)
setFileInfos(JSON.parse(sessionStorage["harmonyStashed"]));
}, []);
useEffect(() => {
//default to intraInstrument ON in the case of just one instument in the model
if (
fileInfos &&
fileInfos.length === 1 &&
resultsOptions.intraInstrument === false
) {
let newResultsOptions = { ...resultsOptions };
newResultsOptions.intraInstrument = true;
newResultsOptions.intraInstrumentPreviousState =
resultsOptions.intraInstrument;
setResultsOptions(newResultsOptions);
}
// If there is now more than 1 switch it back to what it was before we forced it.
if (
fileInfos &&
fileInfos.length > 1 &&
typeof resultsOptions.intraInstrumentPreviousState == "boolean"
) {
let newResultsOptions = { ...resultsOptions };
newResultsOptions.intraInstrument =
newResultsOptions.intraInstrumentPreviousState;
delete newResultsOptions.intraInstrumentPreviousState;
setResultsOptions(newResultsOptions);
}
}, [fileInfos, resultsOptions]);
useEffect(() => {
if (getCookieConsentValue("harmonyCookieConsent")) {
ReactGA.initialize("G-S79J6E39ZP");
console.log("GA enabled");
}
exampleInstruments()
.then((data) => {
if (Array.isArray(data)) {
setExistingInstruments(data);
} else {
console.error("Error fetching example instruments", data);
}
})
.catch((e) => {
console.error("Error fetching example instruments", e);
});
}, [exampleInstruments]);
const colorMode = useMemo(
() => ({
toggleColorMode: () => {
setMode((prevMode) => (prevMode === "light" ? "dark" : "light"));
},
}),
[]
);
const getQuestion = (qidx) => {
return apiData.instruments
.map((i) => {
return i.questions;
})
.flat()
.filter((q) => {
return q.question_index === qidx;
})[0];
};
const executeMatch = useCallback(
(forceModel) => {
if (fileInfos)
return match(fileInfos, forceModel).then((data) => {
let simpleApi = simplifyApi(data, fileInfos);
// Filter existing computedMatches to remove any references to removed instruments
setComputedMatches((prev) => {
if (!prev) return prev;
const validQuestionIndices = new Set(
fileInfos.flatMap((f, i) => f.questions.map((_, qIdx) => qIdx))
);
return prev.filter(
(match) =>
validQuestionIndices.has(match.qi) &&
validQuestionIndices.has(match.mqi)
);
});
setApiData(simpleApi);
});
},
[match, fileInfos]
);
useEffect(() => {
// Only execute match if we're on a model route but not loading a saved harmonisation
// (saved harmonisations are loaded via URL params like /model/abc123)
if (
window.location.href.includes("/model") &&
!window.location.href.includes("/model/")
) {
executeMatch(currentModel);
}
}, [currentModel, executeMatch]);
const makePublicShareLink = () => {
let h = {};
h.apiData = apiData;
h.resultsOptions = resultsOptions;
h.public = true;
return new Promise((resolve, reject) => {
storeHarmonisation(h)
.then((doc) => {
console.log(doc);
resolve(window.location.origin + "/app/#/model/" + doc.id);
})
.catch((e) => {
console.log(e);
reject("Could not create share link");
});
});
};
const ratingToast = () => {
if (
!document.cookie
.split("; ")
.find((row) => row.startsWith("harmonyHasRated"))
) {
toast(
<Box>
<Typography component="legend">Are you enjoying Harmony?</Typography>
<Box>
<Rating
name="simple-controlled"
value={ratingValue}
onChange={(event, newValue) => {
console.log(newValue);
setRatingValue(newValue);
reportRating(newValue);
document.cookie =
"harmonyHasRated=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; SameSite=None; Secure";
ReactGA &&
ReactGA.event({
category: "Actions",
action: "Rating",
value: Number(newValue),
});
}}
/>
</Box>
</Box>,
{
autoClose: false,
}
);
}
};
const saveToMyHarmony = () => {
setTimeout(ratingToast, 1000);
let h = {};
h.apiData = apiData;
h.resultsOptions = resultsOptions;
h.public = false;
h.created = new Date();
return new Promise((resolve, reject) => {
storeHarmonisation(h)
.then((docRef) => {
resolve(window.location.origin + "/#/match/" + docRef);
})
.catch((e) => {
console.log(e);
reject("Could not create share link");
});
});
};
const downloadExcel = () => {
setTimeout(ratingToast, 1000);
const matchSheet = computedMatches
.reduce(function (a, cm, i) {
let q = getQuestion(cm.qi);
let mq = getQuestion(cm.mqi);
a.push({
instrument1: q.instrument.name || "Instrument " + i,
question1_no: q.question_no,
question1_text: q.question_text,
question1_topics:
Array.isArray(q.topics_strengths) && q.topics_strengths.join(", "),
instrument2: mq.instrument.name,
question2_no: mq.question_no,
question2_text: mq.question_text,
question2_topics:
Array.isArray(mq.topics_strengths) &&
mq.topics_strengths.join(", "),
match: cm.match,
});
return a;
}, [])
.flat()
.sort((a, b) => {
if (Math.abs(a.match) < Math.abs(b.match)) {
return 1;
}
if (Math.abs(a.match) > Math.abs(b.match)) {
return -1;
}
return 0;
});
const allQs = apiData.instruments
.map((i) => {
return i.questions;
})
.flat()
.sort((a, b) => {
if (a.question_index > b.question_index) {
return 1;
}
if (a.question_index < b.question_index) {
return -1;
}
return 0;
});
const headers = allQs.map((q) => {
return q.instrument.name + " " + q.question_no;
});
const subheaders = allQs.map((q) => {
return q.question_text;
});
const matrixSheet = allQs.map((q, i) => {
return Array(i + 1).concat(q.matches);
});
matrixSheet.unshift(subheaders);
matrixSheet.unshift(headers);
const matches = XLSXutils.json_to_sheet(matchSheet);
const matrix = XLSXutils.aoa_to_sheet(matrixSheet);
const workbook = XLSXutils.book_new();
XLSXutils.book_append_sheet(workbook, matches, "Matches");
XLSXutils.book_append_sheet(workbook, matrix, "Matrix");
XLSXwriteFile(workbook, "Harmony.xlsx");
};
const handleModelSelect = (event) => {
setCurrentModel(event.target.value);
};
useEffect(() => {
getModels()
.then((models) => {
setAllModels(models);
})
.catch((e) => {
console.log(e);
});
}, [getModels]);
let theme = useMemo(
() =>
createTheme(deepmerge(getDesignTokens(mode), getThemedComponents(mode))),
[mode]
);
theme = responsiveFontSizes(theme);
return (
<ColorModeContext.Provider value={colorMode}>
<ThemeProvider theme={theme}>
<CssBaseline />
<Box
sx={{
display: "flex",
flexDirection: { xs: "column", md: "row" },
minHeight: "100vh",
// Add subtle background image
backgroundImage: `linear-gradient(rgba(39, 237, 185, 0.1), rgba(46, 95, 255, 0.1)), url(${pattern})`,
backgroundSize: "cover",
backgroundRepeat: "no-repeat",
backgroundAttachment: "fixed",
}}
>
<ToastContainer theme={theme.palette.mode} />
<Router>
<HarmonySidebar />
<Box
component="main"
sx={{
flexGrow: 1,
// Responsive spacing for sidebar
ml: { xs: 0, md: "72px" }, // No left margin on mobile, 72px on desktop
mt: { xs: "64px", md: 0 }, // 64px top margin on mobile, none on desktop
minHeight: { xs: "calc(100vh - 64px)", md: "100vh" }, // Account for top bar height
width: { xs: "100%", md: "calc(100% - 72px)" }, // Full width on mobile, minus sidebar on desktop
background: "rgba(250, 248, 255, 0.9)", // Semi-transparent background
padding: useMediaQuery(theme.breakpoints.only("xs"))
? "0.5rem"
: "2rem",
}}
>
<Switch>
<Route path="/login">
<Login />
</Route>
<Route path="/model/:stateHash?">
<Results
fileInfos={fileInfos}
apiData={apiData}
setApiData={setApiData}
setResultsOptions={setResultsOptions}
resultsOptions={resultsOptions}
toaster={toast}
computedMatches={computedMatches}
setComputedMatches={setComputedMatches}
ReactGA={ReactGA}
makePublicShareLink={makePublicShareLink}
saveToMyHarmony={saveToMyHarmony}
downloadExcel={downloadExcel}
currentModel={currentModel}
allModels={allModels}
handleModelSelect={handleModelSelect}
/>
</Route>
<Route path="/makeMeJSON">
<MakeMeJSON
appFileInfos={fileInfos}
setAppFileInfos={setFileInfos}
setApiData={setApiData}
existingInstruments={existingInstruments}
ReactGA={ReactGA}
/>
</Route>
<Route path="/import/:importId">
<Upload
executeMatch={executeMatch}
appFileInfos={fileInfos}
setAppFileInfos={setFileInfos}
existingInstruments={existingInstruments}
ReactGA={ReactGA}
fullscreen={fullscreen}
setFullscreen={setFullscreen}
/>
</Route>
<Route path="*">
<Upload
appFileInfos={fileInfos}
setAppFileInfos={setFileInfos}
executeMatch={executeMatch}
existingInstruments={existingInstruments}
ReactGA={ReactGA}
fullscreen={fullscreen}
setFullscreen={setFullscreen}
/>
</Route>
</Switch>
</Box>
</Router>
<CookieConsent
acceptOnScroll={false}
location="bottom"
buttonText="That's fine"
cookieName="harmonyCookieConsent"
style={{ background: "#2B373B" }}
buttonStyle={{ color: "#4e503b", fontSize: "13px" }}
expires={150}
onAccept={() => {
ReactGA.initialize("G-S79J6E39ZP");
console.log("GA enabled");
}}
>
This website uses analytics cookies to allow us to improve the user
experience.{" "}
</CookieConsent>
</Box>
</ThemeProvider>
</ColorModeContext.Provider>
);
}
export default App;