Skip to content

Commit 8546c0b

Browse files
committed
fix: add backwards compatibility for string values
1 parent 0fc257c commit 8546c0b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/components/Quiz/useLocalQuizData.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { CompletedQuizzes, UserStats } from "@/lib/types"
2-
3-
import allQuizzesData from "@/data/quizzes"
1+
import { UserStats } from "@/lib/types"
42

53
import { USER_STATS_KEY } from "@/lib/constants"
64

@@ -15,5 +13,13 @@ export const INITIAL_USER_STATS: UserStats = {
1513
export const useLocalQuizData = () => {
1614
const data = useLocalStorage(USER_STATS_KEY, INITIAL_USER_STATS)
1715

16+
// If the user has an old version of the app, convert the
17+
// `completed` value from a string to an object.
18+
const [current, setCurrent] = data
19+
if (typeof current.completed === "string") {
20+
const newCompleted = JSON.parse(current.completed)
21+
setCurrent({ ...current, completed: newCompleted })
22+
}
23+
1824
return data
1925
}

0 commit comments

Comments
 (0)