forked from WebKit/Speedometer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-context.js
More file actions
68 lines (54 loc) · 2.21 KB
/
data-context.js
File metadata and controls
68 lines (54 loc) · 2.21 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
import { createContext, useContext } from "react";
import { dataSource } from "../data";
import { v4 as uuidv4 } from "uuid";
const RTL_LOCALES = ["ar", "he", "fa", "ps", "ur"];
const DEFAULT_LANG = "en";
const DEFAULT_DIR = "ltr";
const DataContext = createContext(null);
export const DataContextProvider = ({ children }) => {
const urlParams = new URLSearchParams(window.location.search);
const langFromUrl = urlParams.get("lang")?.toLowerCase();
const lang = langFromUrl && langFromUrl in dataSource ? langFromUrl : DEFAULT_LANG;
const dir = lang && RTL_LOCALES.includes(lang) ? "rtl" : DEFAULT_DIR;
document.documentElement.setAttribute("dir", dir);
document.documentElement.setAttribute("lang", lang);
const { content } = dataSource[lang];
// Generate unique IDs for all articles, and their content items where appropriate.
const contentWithIds = Object.create(null);
Object.keys(content).forEach((key) => {
const { sections } = content[key];
const currentSections = sections.map((section) => {
const currentSection = { ...section };
currentSection.articles = section.articles.map((article) => {
const currentArticle = { ...article };
currentArticle.id = uuidv4();
if (Array.isArray(article.content)) {
currentArticle.content = article.content.map((item) => {
const currentItem = { ...item };
currentItem.id = uuidv4();
return currentItem;
});
}
return currentArticle;
});
return currentSection;
});
contentWithIds[key] = {
...content[key],
sections: currentSections,
};
});
const value = {
lang,
dir,
...dataSource[lang],
content: contentWithIds,
};
return <DataContext.Provider value={value}>{children}</DataContext.Provider>;
};
export const useDataContext = () => {
const dataContext = useContext(DataContext);
if (!dataContext)
throw new Error("A DataProvider must be rendered before using useDataContext");
return dataContext;
};