Move to Phase 4-5

This commit is contained in:
gpt-engineer-app[bot]
2025-10-21 18:15:22 +00:00
parent ae548aa389
commit 3b82887974
8 changed files with 329 additions and 77 deletions

View File

@@ -13,6 +13,7 @@ import { useDebounce } from '@/hooks/useDebounce';
import { logger } from '@/lib/logger';
import { MODERATION_CONSTANTS } from '@/lib/moderation/constants';
import type { EntityFilter, StatusFilter, QueueTab, SortConfig, SortField } from '@/types/moderation';
import * as storage from '@/lib/localStorage';
export interface ModerationFiltersConfig {
/** Initial entity filter */
@@ -184,29 +185,18 @@ export function useModerationFilters(
// Persist filters to localStorage
useEffect(() => {
if (persist) {
try {
localStorage.setItem(
storageKey,
JSON.stringify({
entityFilter,
statusFilter,
activeTab,
})
);
} catch (error: unknown) {
console.error('Failed to persist filters:', error);
}
storage.setJSON(storageKey, {
entityFilter,
statusFilter,
activeTab,
});
}
}, [entityFilter, statusFilter, activeTab, persist, storageKey]);
// Persist sort to localStorage
useEffect(() => {
if (persist) {
try {
localStorage.setItem(`${storageKey}_sort`, JSON.stringify(sortConfig));
} catch (error: unknown) {
console.error('Failed to persist sort:', error);
}
storage.setJSON(`${storageKey}_sort`, sortConfig);
}
}, [sortConfig, persist, storageKey]);

View File

@@ -6,6 +6,7 @@
import { useState, useCallback, useEffect, useMemo } from 'react';
import { MODERATION_CONSTANTS } from '@/lib/moderation/constants';
import * as storage from '@/lib/localStorage';
export interface PaginationConfig {
/** Initial page number (1-indexed) */
@@ -149,17 +150,10 @@ export function usePagination(config: PaginationConfig = {}): PaginationState {
// Persist state
useEffect(() => {
if (persist) {
try {
localStorage.setItem(
storageKey,
JSON.stringify({
currentPage,
pageSize,
})
);
} catch (error: unknown) {
console.error('Failed to persist pagination state:', error);
}
storage.setJSON(storageKey, {
currentPage,
pageSize,
});
}
}, [currentPage, pageSize, persist, storageKey]);

View File

@@ -2,20 +2,7 @@ import { useEffect } from 'react';
import { useAuth } from '@/hooks/useAuth';
import { useUnitPreferences } from '@/hooks/useUnitPreferences';
import { logger } from '@/lib/logger';
function isLocalStorageAvailable(): boolean {
try {
if (typeof window === 'undefined' || typeof localStorage === 'undefined') {
return false;
}
const testKey = '__localStorage_test__';
localStorage.setItem(testKey, 'test');
localStorage.removeItem(testKey);
return true;
} catch {
return false;
}
}
import * as storage from '@/lib/localStorage';
export function useLocationAutoDetect() {
const { user } = useAuth();
@@ -26,25 +13,21 @@ export function useLocationAutoDetect() {
if (loading) return;
// Check if localStorage is available
if (!isLocalStorageAvailable()) {
if (!storage.isLocalStorageAvailable()) {
logger.warn('localStorage is not available, skipping location auto-detection');
return;
}
// Check if we've already attempted detection
const hasAttemptedDetection = localStorage.getItem('location_detection_attempted');
const hasAttemptedDetection = storage.getItem('location_detection_attempted');
// Auto-detect if we haven't attempted it yet and auto_detect is enabled
if (preferences.auto_detect && !hasAttemptedDetection) {
autoDetectPreferences().then(() => {
if (isLocalStorageAvailable()) {
localStorage.setItem('location_detection_attempted', 'true');
}
storage.setItem('location_detection_attempted', 'true');
}).catch((error) => {
console.error('Failed to auto-detect location:', error);
if (isLocalStorageAvailable()) {
localStorage.setItem('location_detection_attempted', 'true');
}
logger.error('Failed to auto-detect location', { error });
storage.setItem('location_detection_attempted', 'true');
});
}
}, [user, loading, preferences.auto_detect]);

View File

@@ -2,6 +2,7 @@ import { useState, useEffect, useMemo, useCallback } from 'react';
import { supabase } from '@/integrations/supabase/client';
import { Park, Ride, Company } from '@/types/database';
import { logger } from '@/lib/logger';
import * as storage from '@/lib/localStorage';
export interface SearchResult {
id: string;
@@ -59,27 +60,9 @@ export function useSearch(options: UseSearchOptions = {}) {
// Load recent searches from localStorage
useEffect(() => {
try {
const stored = localStorage.getItem('thrillwiki_recent_searches');
if (stored) {
try {
const parsed = JSON.parse(stored);
if (Array.isArray(parsed)) {
setRecentSearches(parsed);
} else {
// Invalid format, clear it
logger.warn('Recent searches data is not an array, clearing');
localStorage.removeItem('thrillwiki_recent_searches');
}
} catch (parseError: unknown) {
// JSON parse failed, data is corrupted
console.error('Failed to parse recent searches from localStorage:', parseError);
localStorage.removeItem('thrillwiki_recent_searches');
}
}
} catch (error: unknown) {
// localStorage access failed
console.error('Error accessing localStorage:', error);
const searches = storage.getJSON<string[]>('thrillwiki_recent_searches', []);
if (Array.isArray(searches)) {
setRecentSearches(searches);
}
}, []);
@@ -206,13 +189,13 @@ export function useSearch(options: UseSearchOptions = {}) {
const updated = [searchQuery, ...recentSearches.filter(s => s !== searchQuery)].slice(0, 5);
setRecentSearches(updated);
localStorage.setItem('thrillwiki_recent_searches', JSON.stringify(updated));
storage.setJSON('thrillwiki_recent_searches', updated);
};
// Clear recent searches
const clearRecentSearches = () => {
setRecentSearches([]);
localStorage.removeItem('thrillwiki_recent_searches');
storage.removeItem('thrillwiki_recent_searches');
};
// Get suggestions (recent searches when no query)

View File

@@ -5,6 +5,7 @@ import { supabase } from '@/integrations/supabase/client';
import { logger } from '@/lib/logger';
import { UnitPreferences, getMeasurementSystemFromCountry } from '@/lib/units';
import type { Json } from '@/integrations/supabase/types';
import * as storage from '@/lib/localStorage';
// Type guard for unit preferences
function isValidUnitPreferences(obj: unknown): obj is UnitPreferences {
@@ -147,7 +148,7 @@ export function useUnitPreferences() {
action: 'update_unit_preferences'
});
} else {
localStorage.setItem('unit_preferences', JSON.stringify(updated));
storage.setJSON('unit_preferences', updated);
}
} catch (error: unknown) {
logger.error('Error saving unit preferences', {