Refactor database handling: switch YAML to JSON imports, update data loader, and clean build process.
This commit is contained in:
@@ -5,8 +5,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"copy-yaml": "mkdir -p public/data/database && cp src/assets/database/*.yaml public/data/database/",
|
"build": "tsc -b && vite build",
|
||||||
"build": "npm run copy-yaml && tsc -b && vite build",
|
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
|
@@ -12,7 +12,7 @@ function App() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadData = async () => {
|
const loadData = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/data/Faelyn Eichenhauch.json');
|
const response = await fetch('/data/Faelyn Eichenahauch.json');
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
@@ -67,11 +67,19 @@ import type {Skill, Skills} from '../types/Skill';
|
|||||||
*/
|
*/
|
||||||
async function loadData<T>(fileName: string): Promise<T[]> {
|
async function loadData<T>(fileName: string): Promise<T[]> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/data/database/${fileName}`);
|
// Ensure proper file extension
|
||||||
if (!response.ok) {
|
const fileNameWithExt = fileName.endsWith('.json') ? fileName : `${fileName}.json`;
|
||||||
throw new Error(`Failed to load ${fileName}: ${response.statusText}`);
|
const module = await import(`../assets/database/${fileNameWithExt}`);
|
||||||
|
|
||||||
|
// Access the default export or named export
|
||||||
|
const data = module.default || module;
|
||||||
|
|
||||||
|
// Validate it's an array
|
||||||
|
if (!Array.isArray(data)) {
|
||||||
|
throw new Error(`Data from ${fileName} is not an array`);
|
||||||
}
|
}
|
||||||
return await response.json();
|
|
||||||
|
return data as T[];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error loading ${fileName}:`, error);
|
console.error(`Error loading ${fileName}:`, error);
|
||||||
throw error;
|
throw error;
|
||||||
|
Reference in New Issue
Block a user