Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/components/CatalogueContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSearchParams } from "next/navigation";
import { useEffect, useState } from "react";
import axios from "axios";
import { Button } from "@/components/ui/button";
import { type IPaper, type Filters, type StoredSubjects, type ApiResponse } from "@/interface";
import { type IPaper, type Filters, type ApiResponse } from "@/interface";
import Card from "./Card";
import Loader from "./ui/loader";
import SideBar from "../components/SideBar";
Expand All @@ -20,6 +20,7 @@ import posthog from "posthog-js";
import EmptyState from "./ui/EmptyState";
import SelectionToolbar from "./SelectionToolbar";
import SortComponent from "./ui/sorting";
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";

const CatalogueContentInner = ({ subject }: { subject: string | null }) => {
const [isMounted, setIsMounted] = useState(false);
Expand Down Expand Up @@ -94,9 +95,7 @@ const CatalogueContentInner = ({ subject }: { subject: string | null }) => {
useEffect(() => {
setIsMounted(true);
if (searchParams) {
const currentPinnedSubjects = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as StoredSubjects;
const currentPinnedSubjects = readPinnedSubjects();
const subjectName = searchParams.get("subject");
setSelectedExams(
searchParams.get("exams")?.split(",").filter(Boolean) ?? [],
Expand Down Expand Up @@ -136,9 +135,7 @@ const CatalogueContentInner = ({ subject }: { subject: string | null }) => {
const current = !pinned;
setPinned(current);

const saved = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as string[];
const saved = readPinnedSubjects();
const updated = current
? [...new Set([...saved, subject])]
: saved.filter((s) => s !== subject);
Expand Down
10 changes: 3 additions & 7 deletions src/components/PinnedPapersCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
} from "@/components/ui/carousel";
import Autoplay from "embla-carousel-autoplay";
import { chunkArray } from "@/lib/utils/array";
import { type StoredSubjects } from "@/interface";
import SkeletonPaperCard from "./SkeletonPaperCard";
import PinnedModal from "./ui/PinnedModal";
import { type ApiResponse } from "@/interface"
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";

interface userPaperResponse {
subject: string;
Expand Down Expand Up @@ -63,9 +63,7 @@ function PinnedPapersCarousel() {
try {
setIsLoading(true);

const storedSubjects = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as StoredSubjects;
const storedSubjects = readPinnedSubjects();

const response = await axios.post<ApiResponse<userPaperResponse[]>>(
"/api/user-papers",
Expand Down Expand Up @@ -116,9 +114,7 @@ function PinnedPapersCarousel() {
const handleSubjectsChange = () => {
void (async () => {
try {
const storedSubjects = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as StoredSubjects;
const storedSubjects = readPinnedSubjects();

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

Expand Down
19 changes: 6 additions & 13 deletions src/components/Searchbar/pinned-searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Search } from "lucide-react";
import { Input } from "@/components/ui/input";
import PinButton from "../PinButton";
import Fuse from "fuse.js";
import { type StoredSubjects, type ICourseWithCount, type IUpcomingPaper } from "@/interface";
import { type ICourseWithCount, type IUpcomingPaper } from "@/interface";
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";

function PinnedSearchBar({
initialSubjects,
Expand Down Expand Up @@ -53,9 +54,7 @@ function PinnedSearchBar({
const handleSelectSuggestion = (suggestion: string) => {
setSearchText(suggestion);

const currentPinnedSubjects = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as StoredSubjects;
const currentPinnedSubjects = readPinnedSubjects();

if (suggestion && Array.isArray(currentPinnedSubjects)) {
setPinned(currentPinnedSubjects.includes(suggestion));
Expand Down Expand Up @@ -94,9 +93,7 @@ function PinnedSearchBar({
return;
}

const saved = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as string[];
const saved = readPinnedSubjects();

let updated: string[] = [];

Expand Down Expand Up @@ -133,9 +130,7 @@ function PinnedSearchBar({

useEffect(() => {
const handlePinsChange = () => {
const saved = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as string[];
const saved = readPinnedSubjects();

if (saved.length === 0) {
setShowControls(false);
Expand All @@ -152,9 +147,7 @@ function PinnedSearchBar({
}, []);

useEffect(() => {
const storedSubjects = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as string[];
const storedSubjects = readPinnedSubjects();

if (storedSubjects.length > 0) {
setShowControls(true);
Expand Down
10 changes: 3 additions & 7 deletions src/components/UpcomingPaper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
import { useRouter } from "next/navigation";
import { Pin } from "lucide-react";
import React, { useEffect, useState } from "react";
import { type StoredSubjects } from "@/interface";
import { useCourses } from "@/context/courseContext";
import { Capsule } from "@/components/ui/capsule";
import { readPinnedSubjects } from "@/lib/utils/pinned-subjects";

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

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

useEffect(() => {
const currentPinnedSubjects = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as StoredSubjects;
const currentPinnedSubjects = readPinnedSubjects();

setPinned(
Array.isArray(currentPinnedSubjects) &&
Expand Down
10 changes: 3 additions & 7 deletions src/components/ui/PinnedModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "@/components/ui/dialog";
import SearchBar from "@/components/Searchbar/searchbar";
import { type IUpcomingPaper } from "@/interface";
import { type StoredSubjects } from "@/interface";
import { useState, useEffect } from "react";
import { Pin, PinOff, Plus, GripVertical } from "lucide-react";
import {
Expand All @@ -31,6 +30,7 @@ import {
import { type ApiResponse } from '@/interface'

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

interface userPaperResponse {
subject: string;
Expand Down Expand Up @@ -102,9 +102,7 @@ const PinnedModal = ({
const fetchPapers = async () => {
try {
setIsLoading(true);
const storedSubjects = JSON.parse(
localStorage.getItem("userSubjects") ?? "[]",
) as StoredSubjects;
const storedSubjects = readPinnedSubjects();
const response = await axios.post<ApiResponse<userPaperResponse[]>>(
"/api/user-papers",
storedSubjects,
Expand Down Expand Up @@ -143,9 +141,7 @@ const PinnedModal = ({
};

const unpinSubject = (subjectToRemove: string) => {
const updatedSubjects = (
JSON.parse(localStorage.getItem("userSubjects") ?? "[]") as string[]
).filter((subj) => subj !== subjectToRemove);
const updatedSubjects = readPinnedSubjects().filter((subj) => subj !== subjectToRemove);
localStorage.setItem("userSubjects", JSON.stringify(updatedSubjects));
setDisplayPapers((prev) =>
prev.filter((paper) => paper.subject !== subjectToRemove),
Expand Down
22 changes: 22 additions & 0 deletions src/lib/utils/pinned-subjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pinnedSubjectsKey = "userSubjects";

function parseSaved(saved: string): unknown {
try {
return JSON.parse(saved);
} catch {
return null;
}
}

export function readPinnedSubjects(): string[] {
const saved = localStorage.getItem(pinnedSubjectsKey);
if (!saved) return [];

const subjects = parseSaved(saved);
if (Array.isArray(subjects) && subjects.every((subject): subject is string => typeof subject === "string")) {
return subjects;
}

localStorage.removeItem(pinnedSubjectsKey);
return [];
}