From 0a532532d1a5a5cc55aad7ede73a256a2e209ccd Mon Sep 17 00:00:00 2001 From: walexjnr Date: Fri, 15 May 2026 12:30:15 +0100 Subject: [PATCH] feat: add centralised store selectors --- src/store/selectors.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/store/selectors.ts diff --git a/src/store/selectors.ts b/src/store/selectors.ts new file mode 100644 index 00000000..34cee9dc --- /dev/null +++ b/src/store/selectors.ts @@ -0,0 +1,42 @@ +/** + * Centralised store selectors. + * Import from here instead of accessing store state inline to keep + * components decoupled from store internals. + */ + +import { useSearchStore } from '@/app/store/searchStore'; +import { useNotificationStore } from '@/app/store/notificationStore'; +import { useQuizStore } from '@/app/store/quizStore'; + +// ── Search selectors ────────────────────────────────────────────────────────── +export const useSearchFilters = () => + useSearchStore((s) => ({ + difficulty: s.difficulty, + duration: s.duration, + topics: s.topics, + instructors: s.instructors, + sortBy: s.sortBy, + price: s.price, + })); + +export const useSearchHistory = () => useSearchStore((s) => s.searchHistory); + +// ── Notification selectors ──────────────────────────────────────────────────── +export const useUnreadNotifications = () => + useNotificationStore((s) => s.notifications.filter((n) => !n.read)); + +export const useUnreadCount = () => + useNotificationStore((s) => s.notifications.filter((n) => !n.read).length); + +// ── Quiz selectors ──────────────────────────────────────────────────────────── +export const useCurrentQuestion = () => + useQuizStore((s) => + s.currentQuiz ? s.currentQuiz.questions[s.currentQuestionIndex] ?? null : null, + ); + +export const useQuizProgress = () => + useQuizStore((s) => ({ + current: s.currentQuestionIndex + 1, + total: s.currentQuiz?.questions.length ?? 0, + isReviewMode: s.isReviewMode, + })); \ No newline at end of file