Skip to content

Commit 952ce1e

Browse files
wip migrate
1 parent cbaab48 commit 952ce1e

File tree

5 files changed

+26
-89
lines changed

5 files changed

+26
-89
lines changed

gatsby-ssr.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as React from 'react';
22
import { wrapRootElement as wrap } from './root-wrapper';
3-
import { themeKey } from './src/context/UserDataContext/properties/themeProperty';
3+
import { themeKey } from './src/context/UserDataContext/UserDataContext';
44

55
export const wrapRootElement = wrap;
66

77
// https://joshwcomeau.com/gatsby/dark-mode/
88
const MagicScriptTag = () => {
9-
// Note: see also src/context/UserDataContext/properties/themeProperty.ts if any of the below code needs to be changed.
9+
// Note: see also src/context/UserDataContext/UserDataContext.ts if any of the below code needs to be changed.
1010
const codeToRunOnClient = `
1111
(function(){
1212
var dark = false;

src/components/ProblemSolutions.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import Filter from 'bad-words';
22
import * as React from 'react';
3-
import { useContext, useState } from 'react';
3+
import { useState } from 'react';
44
import ContactUsSlideover from '../components/ContactUsSlideover/ContactUsSlideover';
55
import { useDarkMode } from '../context/DarkModeContext';
66
import { SignInContext } from '../context/SignInContext';
7-
import { LANGUAGE_LABELS } from '../context/UserDataContext/properties/userLang';
8-
import UserDataContext from '../context/UserDataContext/UserDataContext';
7+
import {
8+
LANGUAGE_LABELS,
9+
useUserLangSetting,
10+
} from '../context/UserDataContext/properties/simpleProperties';
11+
import { useFirebaseUser } from '../context/UserDataContext/UserDataContext';
912
import { useUserPermissions } from '../context/UserDataContext/UserPermissionsContext';
1013
import useUserProblemSolutionActions from '../hooks/useUserProblemSolutionActions';
1114
import useUserSolutionsForProblem from '../hooks/useUserSolutionsForProblem';
@@ -23,13 +26,14 @@ export default function ProblemSolutions({
2326
useUserSolutionsForProblem(problem);
2427
const { deleteSolution, upvoteSolution, undoUpvoteSolution, mutateSolution } =
2528
useUserProblemSolutionActions();
26-
const { firebaseUser, lang } = useContext(UserDataContext);
29+
const firebaseUser = useFirebaseUser();
30+
const lang = useUserLangSetting();
2731
const [isContactUsActive, setIsContactUsActive] = useState(false);
2832
const { signIn } = React.useContext(SignInContext);
2933
const canModerate = useUserPermissions().canModerate;
3034
const isDarkMode = useDarkMode();
3135
const filter = new Filter();
32-
const langArr = ['cpp', 'java', 'py'];
36+
const langArr: ('cpp' | 'java' | 'py')[] = ['cpp', 'java', 'py'];
3337
langArr.sort(function (first, second) {
3438
if (first === lang && second !== lang) {
3539
return -1;

src/components/markdown/CodeBlock/CodeBlock.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const CodeSnipButton = ({
131131
class CodeBlock extends React.Component<
132132
{
133133
children: string;
134-
className: string;
134+
className: string | undefined;
135135
isDarkMode: boolean;
136136
copyButton?: boolean;
137137
},

src/context/UserDataContext/UserDataContext.tsx

+12-80
Original file line numberDiff line numberDiff line change
@@ -98,93 +98,17 @@ export const assignDefaultsToUserData = (data: object): UserData => {
9898
};
9999
};
100100

101+
// localstorage key for theme. We need this to set light / dark theme the moment the page loads.
102+
// duplicated from guide:userData:v100.
103+
export const themeKey = 'guide:userData:theme';
104+
101105
// Todo figure out why we even need defaults
102106
const UserDataContext = createContext<UserDataContextAPI>({
103-
// make suer CREATING_ACCOUNT_FOR_FIRST_TIME is here
104107
userData: assignDefaultsToUserData({}),
105108
updateUserData: _ => {},
106109
signOut: () => Promise.resolve(),
107110
firebaseUser: null,
108-
// firebaseUser: null,
109-
// getDataExport: () => Promise.resolve(),
110-
// importUserData: () => true,
111-
// showTags: false,
112-
// hideDifficulty: false,
113-
// hideModules: false,
114-
// divisionTableQuery: {
115-
// division: '',
116-
// season: '',
117-
// },
118111
isLoaded: true,
119-
// lang: 'cpp',
120-
// lastReadAnnouncement: 'open-source',
121-
// lastViewedModule: 'binary-search-sorted',
122-
// lastVisitDate: 1608324157466,
123-
// numPageviews: 130,
124-
// onlineUsers: -1,
125-
// pageviewsPerDay: {
126-
// 1606896000000: 4,
127-
// 1607068800000: 17,
128-
// 1608192000000: 27,
129-
// 1608278400000: 82,
130-
// },
131-
// theme: 'system',
132-
// setTheme: _x => {
133-
// // do nothing
134-
// },
135-
// setShowTags: _x => {
136-
// // do nothing
137-
// },
138-
// setHideDifficulty: _x => {
139-
// // do nothing
140-
// },
141-
// setHideModules: _x => {
142-
// // do nothing
143-
// },
144-
// setDivisionTableQuery: _x => {
145-
// // do nothing
146-
// },
147-
// setLang: _x => {
148-
// // do nothing
149-
// },
150-
// setLastReadAnnouncement: _x => {
151-
// // do nothing
152-
// },
153-
// setLastViewedModule: _x => {
154-
// // do nothing
155-
// },
156-
// setLastVisitDate: _x => {
157-
// // do nothing
158-
// },
159-
// setModuleProgress: (_moduleID, _progress) => {
160-
// // do nothing/
161-
// },
162-
// setShowIgnored: _x => {
163-
// // do nothing
164-
// },
165-
// setUserProgressOnProblems: (_problemId, _status) => {
166-
// // do nothing
167-
// },
168-
// setUserProgressOnResources: (_moduleId, _status) => {
169-
// // do nothing
170-
// },
171-
// showIgnored: false,
172-
// signOut: () => {
173-
// // do nothing
174-
// return Promise.resolve();
175-
// },
176-
// triggerUserDataContextRerender: () => {},
177-
// userProgressOnModules: {},
178-
// userProgressOnModulesActivity: [],
179-
// userProgressOnProblems: {},
180-
// userProgressOnProblemsActivity: [],
181-
// userProgressOnResources: {},
182-
// adSettings: {
183-
// hideMarch2021: false,
184-
// },
185-
// setAdSettings: () => {
186-
// // do nothing
187-
// },
188112
});
189113

190114
export const UserDataProvider = ({
@@ -199,6 +123,9 @@ export const UserDataProvider = ({
199123
if (updates.lang && prevState.lang !== updates.lang) {
200124
updateLangURL(updates.lang);
201125
}
126+
if (updates.theme && prevState.theme !== updates.theme) {
127+
localStorage.setItem(themeKey, JSON.stringify(updates.theme));
128+
}
202129
return { ...prevState, ...updates };
203130
},
204131
null,
@@ -387,6 +314,7 @@ export const UserDataProvider = ({
387314
// // );
388315
// },
389316

317+
// Todo: make sure CREATING_ACCOUNT_FOR_FIRST_TIME is set correctly
390318
// importUserData: (data: Record<string, any>): boolean => {
391319
// // if (
392320
// // confirm(
@@ -436,3 +364,7 @@ export const useUserData = (): UserData => {
436364
export const useUpdateUserData = () => {
437365
return React.useContext(UserDataContext).updateUserData;
438366
};
367+
368+
export const useFirebaseUser = () => {
369+
return React.useContext(UserDataContext).firebaseUser;
370+
};

src/context/UserDataContext/properties/simpleProperties.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ export const useSetHideModulesSetting = createUserDataMutation(
5757
);
5858

5959
export type Language = 'showAll' | 'cpp' | 'java' | 'py';
60-
export const LANGUAGE_LABELS: { [key in Language]: string } = {
60+
export const LANGUAGE_LABELS: { [key in Language | 'unknown']: string } = {
6161
showAll: 'All',
6262
cpp: 'C++',
6363
java: 'Java',
6464
py: 'Python',
65+
unknown: 'Unknown',
6566
};
6667

6768
// Note: User Lang is synchronized with a URL query parameter

0 commit comments

Comments
 (0)