|
| 1 | +import { liveQuery } from 'dexie'; |
| 2 | +import { useEffect, useState } from 'preact/hooks'; |
| 3 | + |
| 4 | +import { db } from '../services/db'; |
| 5 | + |
| 6 | +const EXCLUDED_USER_EMAILS_KEY = 'QUIZ_FILTER_EXCLUDED_USER_EMAILS'; |
| 7 | +const IS_FILTERING_ON_ILLEGIBLE_KEY = 'QUIZ_FILTER_IS_FILTERING_ON_ILLEGIBLE'; |
| 8 | +const CURRENT_USER_KEY = 'CURRENT_USER_EMAIL'; |
| 9 | + |
| 10 | +export interface QuizFilters { |
| 11 | + excludedUserEmails: string[]; |
| 12 | + isFilteringOnIllegible: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +const DEFAULT_FILTERS: QuizFilters = { |
| 16 | + excludedUserEmails: [], |
| 17 | + isFilteringOnIllegible: true, |
| 18 | +}; |
| 19 | + |
| 20 | +export function useQuizFilters(authenticatedUserEmail?: string | null) { |
| 21 | + // Default filter excludes quizzes completed by the authenticated user |
| 22 | + const initialFilters: QuizFilters = { |
| 23 | + ...DEFAULT_FILTERS, |
| 24 | + excludedUserEmails: authenticatedUserEmail ? [authenticatedUserEmail] : [], |
| 25 | + }; |
| 26 | + |
| 27 | + const [filters, setFilters] = useState<QuizFilters | undefined>(undefined); |
| 28 | + |
| 29 | + const settings$ = liveQuery(() => db.settings.toArray()); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + const subscription = settings$.subscribe(async (settings) => { |
| 33 | + // Check if we have a stored user |
| 34 | + const storedUserSetting = settings.find((setting) => setting.name === CURRENT_USER_KEY); |
| 35 | + const storedUserEmail = storedUserSetting ? storedUserSetting.value : null; |
| 36 | + |
| 37 | + // If the authenticated user has changed or no user is stored yet |
| 38 | + if (authenticatedUserEmail && storedUserEmail !== authenticatedUserEmail) { |
| 39 | + // User has changed or first login - clear settings and store new user |
| 40 | + await db.settings.clear(); |
| 41 | + |
| 42 | + // Store the new user |
| 43 | + await db.settings.add({ |
| 44 | + name: CURRENT_USER_KEY, |
| 45 | + value: authenticatedUserEmail, |
| 46 | + }); |
| 47 | + |
| 48 | + // Initialize excludedUserEmails setting |
| 49 | + await db.settings.add({ |
| 50 | + name: EXCLUDED_USER_EMAILS_KEY, |
| 51 | + value: JSON.stringify([authenticatedUserEmail]), |
| 52 | + }); |
| 53 | + |
| 54 | + // Initialize isFilteringOnIllegible setting |
| 55 | + await db.settings.add({ |
| 56 | + name: IS_FILTERING_ON_ILLEGIBLE_KEY, |
| 57 | + value: String(initialFilters.isFilteringOnIllegible), |
| 58 | + }); |
| 59 | + |
| 60 | + // Set filters directly to avoid delay in UI update |
| 61 | + setFilters({ |
| 62 | + excludedUserEmails: [authenticatedUserEmail], |
| 63 | + isFilteringOnIllegible: initialFilters.isFilteringOnIllegible, |
| 64 | + }); |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // If settings are empty (for any other reason) |
| 70 | + if (settings.length === 0 || !settings.some((s) => s.name === EXCLUDED_USER_EMAILS_KEY)) { |
| 71 | + if (authenticatedUserEmail) { |
| 72 | + // Initialize excludedUserEmails setting |
| 73 | + await db.settings.add({ |
| 74 | + name: EXCLUDED_USER_EMAILS_KEY, |
| 75 | + value: JSON.stringify([authenticatedUserEmail]), |
| 76 | + }); |
| 77 | + |
| 78 | + // Initialize isFilteringOnIllegible setting |
| 79 | + await db.settings.add({ |
| 80 | + name: IS_FILTERING_ON_ILLEGIBLE_KEY, |
| 81 | + value: String(initialFilters.isFilteringOnIllegible), |
| 82 | + }); |
| 83 | + |
| 84 | + // Store current user if not already stored |
| 85 | + if (!settings.some((s) => s.name === CURRENT_USER_KEY)) { |
| 86 | + await db.settings.add({ |
| 87 | + name: CURRENT_USER_KEY, |
| 88 | + value: authenticatedUserEmail, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + // Set filters directly to avoid delay in UI update |
| 93 | + setFilters({ |
| 94 | + excludedUserEmails: [authenticatedUserEmail], |
| 95 | + isFilteringOnIllegible: initialFilters.isFilteringOnIllegible, |
| 96 | + }); |
| 97 | + |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + const excludedUserEmailsSetting = settings.find((setting) => setting.name === EXCLUDED_USER_EMAILS_KEY); |
| 103 | + let excludedUserEmails = initialFilters.excludedUserEmails; |
| 104 | + |
| 105 | + if (excludedUserEmailsSetting) { |
| 106 | + try { |
| 107 | + excludedUserEmails = JSON.parse(excludedUserEmailsSetting.value); |
| 108 | + } catch (e) { |
| 109 | + console.error('Failed to parse excluded user emails', e); |
| 110 | + } |
| 111 | + } else { |
| 112 | + await db.settings.add({ |
| 113 | + name: EXCLUDED_USER_EMAILS_KEY, |
| 114 | + value: JSON.stringify(initialFilters.excludedUserEmails), |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + const isFilteringOnIllegibleSetting = settings.find((setting) => setting.name === IS_FILTERING_ON_ILLEGIBLE_KEY); |
| 119 | + let isFilteringOnIllegible = initialFilters.isFilteringOnIllegible; |
| 120 | + |
| 121 | + if (isFilteringOnIllegibleSetting) { |
| 122 | + isFilteringOnIllegible = isFilteringOnIllegibleSetting.value === 'true'; |
| 123 | + } else { |
| 124 | + await db.settings.add({ |
| 125 | + name: IS_FILTERING_ON_ILLEGIBLE_KEY, |
| 126 | + value: String(initialFilters.isFilteringOnIllegible), |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + setFilters({ |
| 131 | + excludedUserEmails, |
| 132 | + isFilteringOnIllegible, |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + return () => { |
| 137 | + subscription.unsubscribe(); |
| 138 | + }; |
| 139 | + }, [settings$, initialFilters]); |
| 140 | + |
| 141 | + const updateFilters = async (changes: Partial<QuizFilters>) => { |
| 142 | + if (changes.excludedUserEmails !== undefined) { |
| 143 | + await db.settings.put({ |
| 144 | + name: EXCLUDED_USER_EMAILS_KEY, |
| 145 | + value: JSON.stringify(changes.excludedUserEmails), |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + if (changes.isFilteringOnIllegible !== undefined) { |
| 150 | + await db.settings.put({ |
| 151 | + name: IS_FILTERING_ON_ILLEGIBLE_KEY, |
| 152 | + value: String(changes.isFilteringOnIllegible), |
| 153 | + }); |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + return { |
| 158 | + filters: filters ?? initialFilters, |
| 159 | + updateFilters, |
| 160 | + loading: filters === undefined, |
| 161 | + }; |
| 162 | +} |
0 commit comments