NeoScan_Radiologist/app/store/hooks.ts

237 lines
6.4 KiB
TypeScript

/*
* File: hooks.ts
* Description: Custom Redux hooks for easy store access
* Design & Developed by Tech4Biz Solutions
* Copyright (c) Spurrin Innovations. All rights reserved.
*/
import { useDispatch, useSelector, TypedUseSelectorHook } from 'react-redux';
import { RootState, AppDispatch } from './index';
// ============================================================================
// TYPED HOOKS
// ============================================================================
/**
* Typed Use Dispatch Hook
*
* Purpose: Provide typed dispatch function
*
* Features:
* - Type-safe dispatch function
* - Proper TypeScript support
* - Consistent with Redux Toolkit patterns
*/
export const useAppDispatch = () => useDispatch<AppDispatch>();
/**
* Typed Use Selector Hook
*
* Purpose: Provide typed selector function
*
* Features:
* - Type-safe selector function
* - Proper TypeScript support
* - Consistent with Redux Toolkit patterns
*/
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
// ============================================================================
// AUTH HOOKS
// ============================================================================
/**
* Use Auth Hook
*
* Purpose: Get authentication state and actions
*
* @returns Authentication state and actions
*/
export const useAuth = () => {
const auth = useAppSelector((state) => state.auth);
const dispatch = useAppDispatch();
return {
// State
user: auth.user,
isLoading: auth.loading,
isLoggingIn: auth.loading,
isLoggingOut: false,
isAuthenticated: auth.isAuthenticated,
error: auth.error,
// Computed values
isLoggedIn: auth.isAuthenticated,
hasUser: !!auth.user,
userRole: auth.user?.dashboard_role || null,
userId: auth.user?.user_id || null,
userEmail: auth.user?.email || null,
userName: auth.user?.display_name || null,
userAvatar: auth.user?.profile_photo_url || null,
userPermissions: [],
isOnboarded: auth.user?.onboarded || false,
// Actions
dispatch,
};
};
/**
* Use User Hook
*
* Purpose: Get current user information
*
* @returns Current user data
*/
export const useUser = () => {
const user = useAppSelector((state) => state.auth.user);
return {
user,
isLoggedIn: !!user,
};
};
// ============================================================================
// DASHBOARD HOOKS
// ============================================================================
/**
* Use Dashboard Hook
*
* Purpose: Get dashboard state and actions
*
* @returns Dashboard state and actions
*/
export const useDashboard = () => {
const dashboard = useAppSelector((state) => state.dashboard);
const dispatch = useAppDispatch();
return {
// State
dashboard: dashboard.dashboard,
isLoading: dashboard.isLoading,
isRefreshing: dashboard.isRefreshing,
error: dashboard.error,
lastUpdated: dashboard.lastUpdated,
isConnected: dashboard.isConnected,
selectedFilter: dashboard.selectedFilter,
sortBy: dashboard.sortBy,
sortOrder: dashboard.sortOrder,
// Actions
dispatch,
};
};
// ============================================================================
// PATIENT CARE HOOKS
// ============================================================================
/**
* Use Patient Care Hook
*
* Purpose: Get patient care state and actions
*
* @returns Patient care state and actions
*/
export const usePatientCare = () => {
const patientCare = useAppSelector((state) => state.patientCare);
const dispatch = useAppDispatch();
return {
// State
patients: patientCare.patients,
currentPatient: patientCare.currentPatient,
isLoading: patientCare.isLoading,
isRefreshing: patientCare.isRefreshing,
isLoadingPatientDetails: patientCare.isLoadingPatientDetails,
error: patientCare.error,
searchQuery: patientCare.searchQuery,
selectedFilter: patientCare.selectedFilter,
sortBy: patientCare.sortBy,
sortOrder: patientCare.sortOrder,
currentPage: patientCare.currentPage,
itemsPerPage: patientCare.itemsPerPage,
totalItems: patientCare.totalItems,
lastUpdated: patientCare.lastUpdated,
// Computed values
activePatients: patientCare.patients.filter(patient => patient.patient_info.status === 'Active'),
criticalPatients: patientCare.patients.filter(patient => patient.patient_info.report_status === 'Critical'),
dischargedPatients: patientCare.patients.filter(patient => patient.patient_info.status === 'Discharged'),
// Actions
dispatch,
};
};
// ============================================================================
// SETTINGS HOOKS
// ============================================================================
/**
* Use Settings Hook
*
* Purpose: Get settings state and actions
*
* @returns Settings state and actions
*/
export const useSettings = () => {
const settings = useAppSelector((state) => state.settings);
const dispatch = useAppDispatch();
return {
// State
userProfile: settings.userProfile,
userPreferences: settings.userPreferences,
isLoading: settings.isLoading,
isUpdatingProfile: settings.isUpdatingProfile,
isUpdatingPreferences: settings.isUpdatingPreferences,
error: settings.error,
isProfileValid: settings.isProfileValid,
isPreferencesValid: settings.isPreferencesValid,
profileLastUpdated: settings.profileLastUpdated,
preferencesLastUpdated: settings.preferencesLastUpdated,
// Actions
dispatch,
};
};
// ============================================================================
// UTILITY HOOKS
// ============================================================================
/**
* Use Store Hook
*
* Purpose: Get access to the entire store state
*
* @returns Entire store state
*/
export const useStore = () => {
return useAppSelector((state) => state);
};
/**
* Use Persistence Status Hook
*
* Purpose: Get Redux Persist status
*
* @returns Persistence status
*/
export const usePersistenceStatus = () => {
// This would need to be implemented with a custom selector
// that accesses the persistor state
return {
isRehydrated: true, // Placeholder
isPersisting: false, // Placeholder
};
};
/*
* End of File: hooks.ts
* Design & Developed by Tech4Biz Solutions
* Copyright (c) Spurrin Innovations. All rights reserved.
*/