mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 13:51:12 -05:00
146 lines
3.6 KiB
TypeScript
146 lines
3.6 KiB
TypeScript
/**
|
|
* Moderation Queue Sort Hook
|
|
*
|
|
* Manages sort configuration for the moderation queue with persistence.
|
|
*/
|
|
|
|
import { useState, useCallback, useEffect, useMemo } from 'react';
|
|
import type { SortConfig, SortField, SortDirection } from '@/types/moderation';
|
|
import {
|
|
getDefaultSortConfig,
|
|
loadSortConfig,
|
|
saveSortConfig,
|
|
toggleSortDirection as toggleDirection,
|
|
isDefaultSortConfig,
|
|
} from '@/lib/moderation/sorting';
|
|
|
|
export interface ModerationSortConfig {
|
|
/** Initial sort configuration */
|
|
initialConfig?: SortConfig;
|
|
|
|
/** Whether to persist sort config */
|
|
persist?: boolean;
|
|
|
|
/** localStorage key for persistence */
|
|
storageKey?: string;
|
|
|
|
/** Callback when sort config changes */
|
|
onChange?: (config: SortConfig) => void;
|
|
}
|
|
|
|
export interface ModerationSort {
|
|
/** Current sort configuration */
|
|
config: SortConfig;
|
|
|
|
/** Sort field */
|
|
field: SortField;
|
|
|
|
/** Sort direction */
|
|
direction: SortDirection;
|
|
|
|
/** Set sort field */
|
|
setField: (field: SortField) => void;
|
|
|
|
/** Set sort direction */
|
|
setDirection: (direction: SortDirection) => void;
|
|
|
|
/** Toggle sort direction */
|
|
toggleDirection: () => void;
|
|
|
|
/** Set both field and direction */
|
|
setConfig: (config: SortConfig) => void;
|
|
|
|
/** Reset to default */
|
|
reset: () => void;
|
|
|
|
/** Check if using default config */
|
|
isDefault: boolean;
|
|
}
|
|
|
|
/**
|
|
* Hook for managing moderation queue sort configuration
|
|
*
|
|
* @param config - Configuration options
|
|
* @returns Sort state and actions
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* const sort = useModerationSort({
|
|
* persist: true,
|
|
* onChange: (config) => fetchItems(config)
|
|
* });
|
|
*
|
|
* // Use in component
|
|
* <Select value={sort.field} onValueChange={sort.setField}>
|
|
* <SelectItem value="created_at">Date</SelectItem>
|
|
* <SelectItem value="username">User</SelectItem>
|
|
* </Select>
|
|
* ```
|
|
*/
|
|
export function useModerationSort(config: ModerationSortConfig = {}): ModerationSort {
|
|
const {
|
|
initialConfig,
|
|
persist = true,
|
|
storageKey = 'moderationQueue_sortConfig',
|
|
onChange,
|
|
} = config;
|
|
|
|
// Load persisted or use initial/default config
|
|
const [sortConfig, setSortConfig] = useState<SortConfig>(() => {
|
|
if (initialConfig) return initialConfig;
|
|
if (persist) return loadSortConfig(storageKey);
|
|
return getDefaultSortConfig();
|
|
});
|
|
|
|
// Persist changes
|
|
useEffect(() => {
|
|
if (persist) {
|
|
saveSortConfig(sortConfig, storageKey);
|
|
}
|
|
onChange?.(sortConfig);
|
|
}, [sortConfig, persist, storageKey, onChange]);
|
|
|
|
// Set sort field (keep direction)
|
|
const setField = useCallback((field: SortField) => {
|
|
setSortConfig((prev) => ({ ...prev, field }));
|
|
}, []);
|
|
|
|
// Set sort direction (keep field)
|
|
const setDirection = useCallback((direction: SortDirection) => {
|
|
setSortConfig((prev) => ({ ...prev, direction }));
|
|
}, []);
|
|
|
|
// Toggle sort direction
|
|
const toggleSortDirection = useCallback(() => {
|
|
setSortConfig((prev) => ({
|
|
...prev,
|
|
direction: toggleDirection(prev.direction),
|
|
}));
|
|
}, []);
|
|
|
|
// Set entire config
|
|
const setConfig = useCallback((newConfig: SortConfig) => {
|
|
setSortConfig(newConfig);
|
|
}, []);
|
|
|
|
// Reset to default
|
|
const reset = useCallback(() => {
|
|
setSortConfig(getDefaultSortConfig());
|
|
}, []);
|
|
|
|
// Check if using default config
|
|
const isDefault = isDefaultSortConfig(sortConfig);
|
|
|
|
return useMemo(() => ({
|
|
config: sortConfig,
|
|
field: sortConfig.field,
|
|
direction: sortConfig.direction,
|
|
setField,
|
|
setDirection,
|
|
toggleDirection: toggleSortDirection,
|
|
setConfig,
|
|
reset,
|
|
isDefault,
|
|
}), [sortConfig, setField, setDirection, toggleSortDirection, setConfig, reset, isDefault]);
|
|
}
|