/**
* Menu management APIs.
*
* i18n: all endpoints accept an optional `locale`. When omitted, the server
* returns or acts on all locales (legacy behaviour for clients that haven't
* been updated yet).
*/
import { i18n } from "@lingui/core";
import { msg } from "@lingui/core/macro";
import { API_BASE, apiFetch, parseApiResponse, throwResponseError } from "./client.js";
export interface Menu {
id: string;
name: string;
label: string;
createdAt: string;
updatedAt: string;
itemCount?: number;
locale: string;
translationGroup: string | null;
}
export interface MenuItem {
id: string;
menuId: string;
parentId: string | null;
sortOrder: number;
type: string;
referenceCollection: string | null;
referenceId: string | null;
customUrl: string | null;
label: string;
titleAttr: string | null;
target: string | null;
cssClasses: string | null;
createdAt: string;
locale: string;
translationGroup: string | null;
}
export interface MenuWithItems extends Menu {
items: MenuItem[];
}
export interface MenuTranslation {
id: string;
name: string;
label: string;
locale: string;
updatedAt: string;
}
export interface MenuTranslationsResponse {
translationGroup: string | null;
translations: MenuTranslation[];
}
export interface CreateMenuInput {
name: string;
label: string;
locale?: string;
translationOf?: string;
}
export interface UpdateMenuInput {
label?: string;
}
export interface CreateMenuItemInput {
type: string;
label: string;
referenceCollection?: string;
referenceId?: string;
customUrl?: string;
target?: string;
titleAttr?: string;
cssClasses?: string;
parentId?: string;
sortOrder?: number;
}
export interface UpdateMenuItemInput {
label?: string;
customUrl?: string;
target?: string;
titleAttr?: string;
cssClasses?: string;
parentId?: string | null;
sortOrder?: number;
}
export interface ReorderMenuItemsInput {
items: Array<{
id: string;
parentId: string | null;
sortOrder: number;
}>;
}
export interface LocaleOptions {
locale?: string;
}
function withLocale(path: string, locale?: string): string {
return locale
? `${path}${path.includes("?") ? "&" : "?"}locale=${encodeURIComponent(locale)}`
: path;
}
/**
* Fetch all menus
*/
export async function fetchMenus(options: LocaleOptions = {}): Promise