Skip to content

Commit cf63d47

Browse files
authored
Merge pull request #566 from Dx-Alz-xD/krish/safe-pinned-subjects
fix(pinned): ignore a corrupted pinned subjects value
2 parents 6dcbc14 + 57ee073 commit cf63d47

6 files changed

Lines changed: 41 additions & 41 deletions

File tree

‎src/components/CatalogueContent.tsx‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useSearchParams } from "next/navigation";
44
import { useEffect, useState } from "react";
55
import axios from "axios";
66
import { Button } from "@/components/ui/button";
7-
import { type IPaper, type Filters, type StoredSubjects, type ApiResponse } from "@/interface";
7+
import { type IPaper, type Filters, type ApiResponse } from "@/interface";
88
import Card from "./Card";
99
import Loader from "./ui/loader";
1010
import SideBar from "../components/SideBar";
@@ -20,6 +20,7 @@ import posthog from "posthog-js";
2020
import EmptyState from "./ui/EmptyState";
2121
import SelectionToolbar from "./SelectionToolbar";
2222
import SortComponent from "./ui/sorting";
23+
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";
2324

2425
const CatalogueContentInner = ({ subject }: { subject: string | null }) => {
2526
const [isMounted, setIsMounted] = useState(false);
@@ -94,9 +95,7 @@ const CatalogueContentInner = ({ subject }: { subject: string | null }) => {
9495
useEffect(() => {
9596
setIsMounted(true);
9697
if (searchParams) {
97-
const currentPinnedSubjects = JSON.parse(
98-
localStorage.getItem("userSubjects") ?? "[]",
99-
) as StoredSubjects;
98+
const currentPinnedSubjects = readPinnedSubjects();
10099
const subjectName = searchParams.get("subject");
101100
setSelectedExams(
102101
searchParams.get("exams")?.split(",").filter(Boolean) ?? [],
@@ -136,9 +135,7 @@ const CatalogueContentInner = ({ subject }: { subject: string | null }) => {
136135
const current = !pinned;
137136
setPinned(current);
138137

139-
const saved = JSON.parse(
140-
localStorage.getItem("userSubjects") ?? "[]",
141-
) as string[];
138+
const saved = readPinnedSubjects();
142139
const updated = current
143140
? [...new Set([...saved, subject])]
144141
: saved.filter((s) => s !== subject);

‎src/components/PinnedPapersCarousel.tsx‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import {
1313
} from "@/components/ui/carousel";
1414
import Autoplay from "embla-carousel-autoplay";
1515
import { chunkArray } from "@/lib/utils/array";
16-
import { type StoredSubjects } from "@/interface";
1716
import SkeletonPaperCard from "./SkeletonPaperCard";
1817
import PinnedModal from "./ui/PinnedModal";
1918
import { type ApiResponse } from "@/interface"
19+
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";
2020

2121
interface userPaperResponse {
2222
subject: string;
@@ -63,9 +63,7 @@ function PinnedPapersCarousel() {
6363
try {
6464
setIsLoading(true);
6565

66-
const storedSubjects = JSON.parse(
67-
localStorage.getItem("userSubjects") ?? "[]",
68-
) as StoredSubjects;
66+
const storedSubjects = readPinnedSubjects();
6967

7068
const response = await axios.post<ApiResponse<userPaperResponse[]>>(
7169
"/api/user-papers",
@@ -116,9 +114,7 @@ function PinnedPapersCarousel() {
116114
const handleSubjectsChange = () => {
117115
void (async () => {
118116
try {
119-
const storedSubjects = JSON.parse(
120-
localStorage.getItem("userSubjects") ?? "[]",
121-
) as StoredSubjects;
117+
const storedSubjects = readPinnedSubjects();
122118

123119
const response = await axios.post<ApiResponse<userPaperResponse[]>>("/api/user-papers", storedSubjects);
124120

‎src/components/Searchbar/pinned-searchbar.tsx‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Search } from "lucide-react";
55
import { Input } from "@/components/ui/input";
66
import PinButton from "../PinButton";
77
import Fuse from "fuse.js";
8-
import { type StoredSubjects, type ICourseWithCount, type IUpcomingPaper } from "@/interface";
8+
import { type ICourseWithCount, type IUpcomingPaper } from "@/interface";
9+
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";
910

1011
function PinnedSearchBar({
1112
initialSubjects,
@@ -52,9 +53,7 @@ function PinnedSearchBar({
5253
const handleSelectSuggestion = (suggestion: string) => {
5354
setSearchText(suggestion);
5455

55-
const currentPinnedSubjects = JSON.parse(
56-
localStorage.getItem("userSubjects") ?? "[]",
57-
) as StoredSubjects;
56+
const currentPinnedSubjects = readPinnedSubjects();
5857

5958
if (suggestion && Array.isArray(currentPinnedSubjects)) {
6059
setPinned(currentPinnedSubjects.includes(suggestion));
@@ -93,9 +92,7 @@ function PinnedSearchBar({
9392
return;
9493
}
9594

96-
const saved = JSON.parse(
97-
localStorage.getItem("userSubjects") ?? "[]",
98-
) as string[];
95+
const saved = readPinnedSubjects();
9996

10097
let updated: string[] = [];
10198

@@ -132,9 +129,7 @@ function PinnedSearchBar({
132129

133130
useEffect(() => {
134131
const handlePinsChange = () => {
135-
const saved = JSON.parse(
136-
localStorage.getItem("userSubjects") ?? "[]",
137-
) as string[];
132+
const saved = readPinnedSubjects();
138133

139134
if (saved.length === 0) {
140135
setShowControls(false);
@@ -151,9 +146,7 @@ function PinnedSearchBar({
151146
}, []);
152147

153148
useEffect(() => {
154-
const storedSubjects = JSON.parse(
155-
localStorage.getItem("userSubjects") ?? "[]",
156-
) as string[];
149+
const storedSubjects = readPinnedSubjects();
157150

158151
if (storedSubjects.length > 0) {
159152
setShowControls(true);

‎src/components/UpcomingPaper.tsx‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
import { useRouter } from "next/navigation";
77
import { Pin } from "lucide-react";
88
import React, { useEffect, useState } from "react";
9-
import { type StoredSubjects } from "@/interface";
109
import { useCourses } from "@/context/courseContext";
1110
import { Capsule } from "@/components/ui/capsule";
11+
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";
1212

1313
interface PaperCardProps {
1414
subject: string;
@@ -26,9 +26,7 @@ export default function PaperCard({ subject, slots }: PaperCardProps) {
2626
const current = !pinned;
2727
setPinned(current);
2828

29-
const saved = JSON.parse(
30-
localStorage.getItem("userSubjects") ?? "[]",
31-
) as string[];
29+
const saved = readPinnedSubjects();
3230
const updated = current
3331
? [...new Set([...saved, subject])]
3432
: saved.filter((s) => s !== subject);
@@ -39,9 +37,7 @@ export default function PaperCard({ subject, slots }: PaperCardProps) {
3937
};
4038

4139
useEffect(() => {
42-
const currentPinnedSubjects = JSON.parse(
43-
localStorage.getItem("userSubjects") ?? "[]",
44-
) as StoredSubjects;
40+
const currentPinnedSubjects = readPinnedSubjects();
4541

4642
setPinned(
4743
Array.isArray(currentPinnedSubjects) &&

‎src/components/ui/PinnedModal.tsx‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from "@/components/ui/dialog";
1111
import SearchBar from "@/components/Searchbar/searchbar";
1212
import { type IUpcomingPaper } from "@/interface";
13-
import { type StoredSubjects } from "@/interface";
1413
import { useState, useEffect } from "react";
1514
import { Pin, PinOff, Plus, GripVertical } from "lucide-react";
1615
import {
@@ -31,6 +30,7 @@ import {
3130
import { type ApiResponse } from '@/interface'
3231

3332
import { CSS } from "@dnd-kit/utilities";
33+
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";
3434

3535
interface userPaperResponse {
3636
subject: string;
@@ -102,9 +102,7 @@ const PinnedModal = ({
102102
const fetchPapers = async () => {
103103
try {
104104
setIsLoading(true);
105-
const storedSubjects = JSON.parse(
106-
localStorage.getItem("userSubjects") ?? "[]",
107-
) as StoredSubjects;
105+
const storedSubjects = readPinnedSubjects();
108106
const response = await axios.post<ApiResponse<userPaperResponse[]>>(
109107
"/api/user-papers",
110108
storedSubjects,
@@ -143,9 +141,7 @@ const PinnedModal = ({
143141
};
144142

145143
const unpinSubject = (subjectToRemove: string) => {
146-
const updatedSubjects = (
147-
JSON.parse(localStorage.getItem("userSubjects") ?? "[]") as string[]
148-
).filter((subj) => subj !== subjectToRemove);
144+
const updatedSubjects = readPinnedSubjects().filter((subj) => subj !== subjectToRemove);
149145
localStorage.setItem("userSubjects", JSON.stringify(updatedSubjects));
150146
setDisplayPapers((prev) =>
151147
prev.filter((paper) => paper.subject !== subjectToRemove),

‎src/lib/utils/pinned-subjects.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const pinnedSubjectsKey = "userSubjects";
2+
3+
function parseSaved(saved: string): unknown {
4+
try {
5+
return JSON.parse(saved);
6+
} catch {
7+
return null;
8+
}
9+
}
10+
11+
export function readPinnedSubjects(): string[] {
12+
const saved = localStorage.getItem(pinnedSubjectsKey);
13+
if (!saved) return [];
14+
15+
const subjects = parseSaved(saved);
16+
if (Array.isArray(subjects) && subjects.every((subject): subject is string => typeof subject === "string")) {
17+
return subjects;
18+
}
19+
20+
localStorage.removeItem(pinnedSubjectsKey);
21+
return [];
22+
}

0 commit comments

Comments
 (0)