Files
Portfolio/src/hooks/useProjectData.ts
T

38 lines
1.2 KiB
TypeScript

/**
* Project Data Hook
* Custom hook for dynamically loading project data based on current language
*/
import { useTranslation } from 'react-i18next';
import { useState, useEffect } from 'react';
/**
* Loads project data dynamically based on project ID and current language
*
* @template T - The type of the project data object
* @param {string} projectId - The unique identifier of the project
* @returns {T | null} The project data object or null if loading fails
*/
export const useProjectData = <T = unknown>(projectId: string): T | null => {
const { i18n } = useTranslation();
const [projectData, setProjectData] = useState<T | null>(null);
useEffect(() => {
const loadProjectData = async () => {
try {
const projectModule = await import(
`../i18n/locales/${i18n.language}/portfolio/projects/${projectId}`
);
setProjectData(projectModule[projectId] as T);
} catch (error) {
console.error(`Failed to load project data for ${projectId}:`, error);
setProjectData(null);
}
};
loadProjectData();
}, [projectId, i18n.language]);
return projectData;
};