Skip to content

Commit 2db1bb8

Browse files
committed
Updated SecretContext.tsx
1 parent 6797b11 commit 2db1bb8

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

‎src/context/SecretContext.tsx‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface Stat {
4040
// TODO: add a hint indicating how this can be unlocked
4141
}
4242

43-
interface AchievementStatSchema {
43+
export interface AchievementStatSchema {
4444
id: AchievementId;
4545
isUnlocked: boolean,
4646
dateUnlocked?: Date | null,
@@ -94,7 +94,7 @@ export const secrets: SecretMap =
9494
},
9595
};
9696

97-
interface SecretContextType {
97+
export interface SecretContextType {
9898
isKonamiSecretActive: boolean;
9999
isPspSecretActive: boolean;
100100
isIwhbydActive: boolean;
@@ -108,7 +108,11 @@ interface SecretContextType {
108108
loadPlayerStats: () => Map<AchievementId, PlayerStat>;
109109
}
110110

111-
const getSecretStat = (id: AchievementId) => {
111+
export const getSecretStat = (id: AchievementId) => {
112+
if (typeof window === 'undefined' || !window.localStorage) {
113+
return;
114+
}
115+
112116
// if it does not exist, save the current state
113117
if (!localStorage.getItem(id.toString())) {
114118
saveSecretStat(id, false);
@@ -122,7 +126,6 @@ const getSecretStat = (id: AchievementId) => {
122126
const loadedStat = localStorage.getItem(id.toString());
123127
if (!loadedStat) throw new Error(`Unable to load the stats for ${id}.`);
124128

125-
126129
try {
127130
const savedStat: AchievementStatSchema = JSON.parse(loadedStat);
128131
return savedStat;
@@ -141,6 +144,10 @@ const getSecretStat = (id: AchievementId) => {
141144
};
142145

143146
const saveSecretStat = (id: AchievementId, isUnlocked: boolean = true, overwrite: boolean = false) => {
147+
if (typeof window === 'undefined' || !window.localStorage) {
148+
return;
149+
}
150+
144151
// if this is already in storage, don't overwrite it (only if it's unlocked)
145152
const statText = localStorage.getItem(id.toString());
146153
if (statText) {
@@ -164,13 +171,16 @@ const saveSecretStat = (id: AchievementId, isUnlocked: boolean = true, overwrite
164171
};
165172

166173
const loadPlayerStats = () => {
174+
if (typeof window === 'undefined' || !window.localStorage) {
175+
return new Map<AchievementId, PlayerStat>();
176+
}
167177
const results = new Map<AchievementId, PlayerStat>();
168178
Object.keys(AchievementId)
169179
.map((id) => {
170180
const achId = AchievementId[id as keyof typeof AchievementId];
171181
const playerStat: PlayerStat = {
172182
id: achId,
173-
isUnlocked: getSecretStat(achId).isUnlocked,
183+
isUnlocked: getSecretStat(achId)?.isUnlocked ?? false,
174184
stat: secrets[achId],
175185
dateUnlocked: null,
176186
};

0 commit comments

Comments
 (0)