dm_new: Lnb 제목 외
This commit is contained in:
@@ -349,22 +349,40 @@ function normalizeMenus(items: MenuItem[]): MenuItem[] {
|
||||
}));
|
||||
}
|
||||
|
||||
export const REUSE_SITE_ID = process.env.NEXT_PUBLIC_SITE_ID || "DN";
|
||||
export const REUSE_SITE_ID = process.env.NEXT_PUBLIC_SITE_ID || "DM";
|
||||
const anonymousMenuCache = new Map<string, MenuItem[]>();
|
||||
const anonymousMenuInFlight = new Map<string, Promise<MenuItem[]>>();
|
||||
|
||||
export async function fetchAnonymousMenu(siteId = REUSE_SITE_ID): Promise<MenuItem[]> {
|
||||
const response = await fetch(`/cms-service/api/v1/menu/anonymous/user/${siteId}`, {
|
||||
method: "GET",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
cache: "no-store",
|
||||
});
|
||||
const cached = anonymousMenuCache.get(siteId);
|
||||
if (cached) return cached;
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load menu: ${response.status}`);
|
||||
const inFlight = anonymousMenuInFlight.get(siteId);
|
||||
if (inFlight) return inFlight;
|
||||
|
||||
const request = (async () => {
|
||||
const response = await fetch(`/cms-service/api/v1/menu/anonymous/user/${siteId}`, {
|
||||
method: "GET",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load menu: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as MenuItem[];
|
||||
const normalized = Array.isArray(data) ? normalizeMenus(data) : [];
|
||||
anonymousMenuCache.set(siteId, normalized);
|
||||
return normalized;
|
||||
})();
|
||||
|
||||
anonymousMenuInFlight.set(siteId, request);
|
||||
try {
|
||||
return await request;
|
||||
} finally {
|
||||
anonymousMenuInFlight.delete(siteId);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as MenuItem[];
|
||||
if (!Array.isArray(data)) return [];
|
||||
return normalizeMenus(data);
|
||||
}
|
||||
|
||||
function isPathMatched(pathname: string, urlPath: string) {
|
||||
@@ -372,11 +390,14 @@ function isPathMatched(pathname: string, urlPath: string) {
|
||||
return pathname === urlPath || pathname.startsWith(`${urlPath}/`);
|
||||
}
|
||||
|
||||
function hasMatchedPathInTree(menu: MenuItem, pathname: string): boolean {
|
||||
if (isPathMatched(pathname, menu.urlPath)) return true;
|
||||
return (menu.children ?? []).some((child) => hasMatchedPathInTree(child, pathname));
|
||||
}
|
||||
|
||||
export function findActiveTopMenu(menus: MenuItem[], pathname: string): MenuItem | null {
|
||||
const level1Menus = menus.filter((menu) => menu.levelNo === 1 || menu.parentId === null);
|
||||
const matched = level1Menus
|
||||
.filter((menu) => isPathMatched(pathname, menu.urlPath))
|
||||
.sort((a, b) => b.urlPath.length - a.urlPath.length);
|
||||
const matched = level1Menus.filter((menu) => hasMatchedPathInTree(menu, pathname));
|
||||
|
||||
return matched[0] ?? level1Menus[0] ?? null;
|
||||
}
|
||||
@@ -475,21 +496,7 @@ export const CommonReducer:(set:any)=>CommonAction=(set: any) => {
|
||||
}
|
||||
})
|
||||
)},
|
||||
fetchAnonymousMenu: async (siteId = REUSE_SITE_ID) => {
|
||||
const response = await fetch(`/cms-service/api/v1/menu/anonymous/user/${siteId}`, {
|
||||
method: "GET",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load menu: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as MenuItem[];
|
||||
if (!Array.isArray(data)) return [];
|
||||
return normalizeMenus(data);
|
||||
}
|
||||
fetchAnonymousMenu: async (siteId = REUSE_SITE_ID) => fetchAnonymousMenu(siteId)
|
||||
}
|
||||
}
|
||||
export type CommonStore = CommonType & CommonAction;
|
||||
|
||||
Reference in New Issue
Block a user