generated from YashTotale/boilerplate-react-with-redux-and-firebase
-
Notifications
You must be signed in to change notification settings - Fork 2
/
experience.ts
281 lines (233 loc) · 7.47 KB
/
experience.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
// External Imports
import { Document } from "@contentful/rich-text-types";
import { documentToPlainTextString } from "@contentful/rich-text-plain-text-renderer";
// Internal Imports
import { createResolver, createSorter, sortByDate } from "../funcs";
import {
Article,
Experience,
Project,
ResolvedExperience,
SubType,
Tag,
} from "../types";
import { getDefaultSortedExperience } from "./main";
import { getRawProject } from "./projects";
import { getRawArticle } from "./articles";
import { getRawTag } from "./tags";
import { getAsset } from "./assets";
// Redux Imports
import { useSelector } from "react-redux";
import {
ExperienceSort,
ExperienceState,
getExperienceProjectFilter,
getExperienceSearch,
getExperienceSort,
getExperienceTagFilter,
getExperienceTypeFilter,
} from "../../Redux/experience.slice";
// Data Imports
import experience from "../../Data/experience.json";
export const getExperience = (): Experience[] => {
return Object.values(experience) as unknown as Experience[];
};
export const getSingleExperience = createResolver(
(id: string, isSlug = false): ResolvedExperience | null => {
const single = getRawExperience(id, isSlug);
if (!single) return null;
const lightImage = getAsset(single.lightImage);
const darkImage = getAsset(single.darkImage);
const projects = single.projects.reduce((arr, project) => {
const resolved = getRawProject(project);
if (resolved) arr.push(resolved);
return arr;
}, [] as Project[]);
const articles = single.articles.reduce((arr, article) => {
const resolved = getRawArticle(article);
if (resolved) arr.push(resolved);
return arr;
}, [] as Article[]);
const tags = single.tags.reduce((arr, tag) => {
const resolved = getRawTag(tag);
if (resolved) arr.push(resolved);
return arr;
}, [] as Tag[]);
return { ...single, lightImage, darkImage, projects, articles, tags };
}
);
export const getRawExperience = (
identifier: string,
isSlug = false
): Experience | null => {
const all = experience as unknown as Record<string, Experience>;
const single = !isSlug
? all[identifier]
: Object.values(all).find((e) => e.slug === identifier);
if (!single) return null;
return single;
};
export const resolveExpImage = (
exp: Experience,
isDarkMode: boolean,
width = 30
): string => {
return `${
getAsset(isDarkMode ? exp.darkImage : exp.lightImage).file.url
}?w=${width}`;
};
interface ExpType {
label: string;
amount: number;
}
let typesCache: ExpType[] | null = null;
export const getExperienceTypes = (): ExpType[] => {
if (typesCache) return typesCache;
const experience = getExperience();
const types = experience.reduce((types, exp) => {
const exists = types.find((t) => t.label === exp.type);
if (exists) {
exists.amount++;
return types;
}
return [
...types,
{
label: exp.type,
amount: 1,
},
];
}, [] as ExpType[]);
const unique = types.sort((a, b) => a.label.localeCompare(b.label));
typesCache = unique;
return unique;
};
export const generateExperienceTitle = (
exp: Experience | ResolvedExperience
): string => {
return exp.title;
};
export const generateExperienceSubtitle = (
exp: Experience | ResolvedExperience
): string => {
return exp.role;
};
export const generateExperienceTimeline = (
exp: Experience | ResolvedExperience
): string => {
const start = exp.start;
let end = exp.end;
if (end === start) return start;
if (!end) end = "Present";
return `${start} - ${end}`;
};
export const checkType = (
e: ResolvedExperience,
type: ExperienceState["typeFilter"]
): boolean => {
if (type === null) return true;
return type === e.type;
};
export const checkProjects = (
e: ResolvedExperience,
projects: string[]
): boolean => {
if (!projects.length) return true;
return projects.every((project) =>
e.projects.some((p) => p.title === project)
);
};
export const checkTags = (e: ResolvedExperience, tags: string[]): boolean => {
if (!tags.length) return true;
return tags.every((tag) => e.tags.some((t) => t.title === tag));
};
const searchCache: Record<string, Record<string, boolean>> = {};
export const checkSearch = (e: ResolvedExperience, search: string): boolean => {
if (!search.length) return true;
if (!searchCache[e.id]) searchCache[e.id] = {};
const cache = searchCache[e.id][search];
if (typeof cache === "boolean") return cache;
const matches: (boolean | undefined)[] = [
generateExperienceTitle(e).toLowerCase().includes(search),
documentToPlainTextString(e.description as Document)
.toLowerCase()
.includes(search),
documentToPlainTextString(e.responsibilities as Document)
.toLowerCase()
.includes(search),
generateExperienceTimeline(e).toLowerCase().includes(search),
e.projects.some((project) => project.title.toLowerCase().includes(search)),
e.articles.some((article) => article.title.toLowerCase().includes(search)),
e.tags.some((tag) => tag.title.toLowerCase().includes(search)),
];
const result = matches.includes(true);
searchCache[e.id][search] = result;
return result;
};
export const useFilteredExperience = (): ResolvedExperience[] => {
const experience = useSortedExperience(true);
const search = useSelector(getExperienceSearch);
const normalizedSearch = search.toLowerCase();
const typeFilter = useSelector(getExperienceTypeFilter);
const projectFilter = useSelector(getExperienceProjectFilter);
const tagFilter = useSelector(getExperienceTagFilter);
return experience.filter((e) => {
if (!checkType(e, typeFilter)) return false;
if (!checkProjects(e, projectFilter)) return false;
if (!checkTags(e, tagFilter)) return false;
if (!checkSearch(e, normalizedSearch)) return false;
return true;
});
};
export function useSortedExperience(resolve: true): ResolvedExperience[];
export function useSortedExperience(resolve: false): Experience[];
export function useSortedExperience(): Experience[];
export function useSortedExperience(
resolve?: boolean
): (ResolvedExperience | Experience)[] {
const sort = useSelector(getExperienceSort);
const sorted = sortExperience(sort);
if (!resolve) return sorted;
return sorted.reduce((arr, e) => {
const exp = getSingleExperience(e.id);
if (exp) arr.push(exp);
return arr;
}, [] as ResolvedExperience[]);
}
export const sortExperience = createSorter<ExperienceSort, Experience>(
{
Default: (a, b) => {
const sorted = getDefaultSortedExperience();
const aIndex = sorted.indexOf(a.id);
const bIndex = sorted.indexOf(b.id);
return aIndex - bIndex;
},
Alphabetically: (a, b) => {
const aTitle = generateExperienceTitle(a).toLowerCase();
const bTitle = generateExperienceTitle(b).toLowerCase();
return aTitle.localeCompare(bTitle);
},
Latest: (a, b) => sortByDate(a, b),
},
getExperience()
);
interface RelatedExperience {
label: string;
amount: number;
image: string;
}
const relatedCache: Record<any, RelatedExperience[]> = {};
export const getExperienceAsRelated = (
key: SubType<Experience, any[]>,
isDarkMode: boolean
): RelatedExperience[] => {
if (relatedCache[key]) return relatedCache[key];
const allExperience = sortExperience("Alphabetically");
const related = allExperience.map((exp) => ({
label: generateExperienceTitle(exp),
amount: exp[key].length,
image: resolveExpImage(exp, isDarkMode),
}));
relatedCache[key] = related;
return related;
};