Files
homaroma/apps/spa/src/store/menuStore.ts
T
2026-07-14 19:26:20 -04:00

63 lines
1.8 KiB
TypeScript

import { create } from 'zustand';
import { API_BASE } from '@homaroma/types';
export interface MenuLink {
label: Record<string, string> | string;
url?: string;
links?: MenuLink[];
imageUrl?: string | null; // รูปที่แสดงเมื่อ Hover ลิงก์
icon?: string; // ชื่อ icon จาก registry (lucide)
}
export interface MenuColumn {
title: Record<string, string> | string;
links: MenuLink[];
iconUrl?: string | null; // รูปประจำคอลัมน์ (1:1)
icon?: string; // ชื่อ icon จาก registry (lucide)
}
export interface MenuFeaturedCard {
enabled?: boolean;
sectionTitle?: Record<string, string> | string;
badge?: Record<string, string> | string;
title?: Record<string, string> | string;
subtitle?: Record<string, string> | string;
price?: string;
imageUrl?: string | null;
focalPoint?: { x: string; y: string };
url?: string;
ctaLabel?: Record<string, string> | string;
}
export interface MegaMenuPayload {
id?: string;
nodeId?: string;
label: Record<string, string> | string;
url?: string;
columns?: MenuColumn[];
rootImageUrl?: string | null; // รูปหลักฝั่งซ้ายสุด (1:1)
featured?: MenuFeaturedCard;
}
interface MenuState {
megaMenu: MegaMenuPayload[];
isLoading: boolean;
fetchMegaMenu: () => Promise<void>;
}
export const useMenuStore = create<MenuState>((set) => ({
megaMenu: [],
isLoading: false,
fetchMegaMenu: async () => {
set({ isLoading: true });
try {
const response = await fetch(`${API_BASE}/menus/main`);
const data = await response.json();
set({ megaMenu: data.payload || [], isLoading: false });
} catch (error) {
console.error('Failed to fetch mega menu', error);
set({ isLoading: false });
}
}
}));