show demo

This commit is contained in:
hamid zarghami
2026-06-27 11:31:57 +03:30
parent 050ffc255e
commit 047bfc6f46
7 changed files with 366 additions and 19 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

+65
View File
@@ -0,0 +1,65 @@
export const MENU_SITE_ORIGIN = "https://dmenu.danakcorp.com";
/** Admin routes whose first segment must not be proxied to the public menu site. */
export const MENU_PREVIEW_RESERVED_SEGMENTS = new Set([
"auth",
"dashboard",
"setting",
"statistics",
"schedule",
"payment_methods",
"foods",
"orders",
"customers",
"discounts",
"announcements",
"reports",
"comments",
"roles",
"admins",
"shipment_methods",
"coupons",
"reviews",
"pagers",
"notifications",
"assets",
"menu-preview",
]);
export const isMenuPreviewProxyPath = (pathname: string) => {
if (!pathname.startsWith("/menu-preview/")) return false;
const rest = pathname.slice("/menu-preview".length);
return rest.length > 0;
};
export const isMenuSlugProxyPath = (pathname: string) => {
const [firstSegment] = pathname.split("/").filter(Boolean);
if (!firstSegment) return false;
return !MENU_PREVIEW_RESERVED_SEGMENTS.has(firstSegment);
};
export const toMenuPreviewProxyPath = (slug: string) => `/${slug}`;
export const MENU_PREVIEW_CACHE_PARAM = "_preview";
export const buildMenuPreviewUrl = (slug: string, cacheKey: number) =>
`${toMenuPreviewProxyPath(slug)}?${MENU_PREVIEW_CACHE_PARAM}=${cacheKey}`;
export const stripMenuPreviewCacheParam = (search: string) => {
const params = new URLSearchParams(search.startsWith("?") ? search.slice(1) : search);
params.delete(MENU_PREVIEW_CACHE_PARAM);
const query = params.toString();
return query ? `?${query}` : "";
};
export const toMenuSitePath = (pathname: string) => {
if (isMenuPreviewProxyPath(pathname)) {
return pathname.replace(/^\/menu-preview/, "") || "/";
}
if (isMenuSlugProxyPath(pathname)) {
return pathname;
}
return null;
};
@@ -1,3 +1,6 @@
import { buildMenuPreviewUrl } from "@/config/menuPreview";
import iphoneCamera from "@/assets/images/iphone_camera.png";
import iphoneFrame from "@/assets/images/iphonr.png";
import Button from "@/components/Button";
import ColorPicker from "@/components/ColorPicker";
import Radio from "@/components/Radio";
@@ -19,6 +22,28 @@ type BackgroundType = "pattern" | "custom";
const UPLOAD_BOX_CLASS = "w-[200px] h-[362px] rounded-2xl border border-dashed border-description";
const PHONE_FRAME_WIDTH = 375;
/** Measured from `iphonr.png` (839×1759) — white screen rect inside the bezel. */
const PHONE_FRAME_IMAGE = { width: 839, height: 1759 } as const;
const PHONE_SCREEN_RECT = { left: 33, top: 36, right: 806, bottom: 1756, radius: 89 } as const;
const toFramePercent = (value: number, axis: "width" | "height") =>
`${(value / PHONE_FRAME_IMAGE[axis]) * 100}%`;
const PHONE_SCREEN_STYLE = {
top: toFramePercent(PHONE_SCREEN_RECT.top, "height"),
left: toFramePercent(PHONE_SCREEN_RECT.left, "width"),
right: toFramePercent(PHONE_FRAME_IMAGE.width - 1 - PHONE_SCREEN_RECT.right, "width"),
bottom: toFramePercent(PHONE_FRAME_IMAGE.height - 1 - PHONE_SCREEN_RECT.bottom, "height"),
borderRadius: (PHONE_SCREEN_RECT.radius / PHONE_FRAME_IMAGE.width) * PHONE_FRAME_WIDTH,
} as const;
const PHONE_CAMERA_STYLE = {
top: toFramePercent(18, "height"),
width: toFramePercent(178, "width"),
} as const;
const getPatternBgUrl = (item: BackgroundItemType) => item.url ?? item.bgUrl ?? "";
const normalizeBgUrl = (url: string) => {
@@ -45,6 +70,7 @@ const DesignSettings: FC = () => {
const [imageBlur, setImageBlur] = useState(0);
const [overlayDarkness, setOverlayDarkness] = useState(0);
const [isInitialized, setIsInitialized] = useState(false);
const [previewReloadKey, setPreviewReloadKey] = useState(0);
const { data: backgrounds } = useGetBackgrounds();
const { mutate: setBackground, isPending: isSavingBackground } = useSetBackground();
const { mutate: updateRestaurant, isPending: isUpdatingRestaurant } = useUpdateRestaurant();
@@ -52,6 +78,11 @@ const DesignSettings: FC = () => {
const { patterns, isLoading: isPatternsLoading, isError: isPatternsError } = useBackgroundPatterns(appliedColor);
const { data: restaurant, refetch } = useGetRestaurant();
const backgroundItems = useMemo(() => backgrounds?.data ?? [], [backgrounds]);
const menuPreviewUrl = useMemo(() => {
const slug = restaurant?.data?.slug;
if (!slug) return "";
return buildMenuPreviewUrl(slug, previewReloadKey);
}, [restaurant?.data?.slug, previewReloadKey]);
const isSaving = isSavingBackground || isUploadingImage || isUpdatingRestaurant;
useEffect(() => {
@@ -127,6 +158,12 @@ const DesignSettings: FC = () => {
setOverlayDarkness(0);
};
const handleSaveSuccess = useCallback(() => {
toast.success("تنظیمات طراحی با موفقیت ذخیره شد");
refetch();
setPreviewReloadKey(Date.now());
}, [refetch]);
const saveCustomBackground = (bgUrl: string) => {
const backgroundPayload: SetBackgroundType = {
bgUrl,
@@ -140,10 +177,7 @@ const DesignSettings: FC = () => {
}
setBackground(backgroundPayload, {
onSuccess: () => {
toast.success("تنظیمات طراحی با موفقیت ذخیره شد");
refetch();
},
onSuccess: handleSaveSuccess,
onError: (error: unknown) => {
toast.error(extractErrorMessage(error as ErrorType, "خطا در ذخیره تنظیمات طراحی"));
},
@@ -174,10 +208,7 @@ const DesignSettings: FC = () => {
bgType: resolveBgType(backgroundType, selectedPatternId),
},
{
onSuccess: () => {
toast.success("تنظیمات طراحی با موفقیت ذخیره شد");
refetch();
},
onSuccess: handleSaveSuccess,
onError: (error: unknown) => {
toast.error(extractErrorMessage(error as ErrorType, "خطا در ذخیره تنظیمات طراحی"));
},
@@ -225,8 +256,8 @@ const DesignSettings: FC = () => {
<div className="bg-white rounded-4xl p-8">
<div className="text-lg font-light">تنظیمات طراحی</div>
<div className="flex mt-5">
<div className="max-w-[506px] w-full">
<div className="flex mt-5 gap-10">
<div className="max-w-[506px] w-full shrink-0">
<ColorPicker label="رنگ منو" value={menuColor} onChange={setMenuColor} />
<div className="mt-7">
@@ -322,6 +353,40 @@ const DesignSettings: FC = () => {
</Button>
</div>
</div>
<div className="flex-1 flex justify-center items-start min-w-0 sticky top-4">
<div className="relative shrink-0 select-none overflow-hidden" style={{ width: PHONE_FRAME_WIDTH }}>
<img src={iphoneFrame} alt="" className="relative z-0 w-full h-auto block pointer-events-none" draggable={false} />
<div
className="absolute z-10 overflow-hidden"
style={{
top: PHONE_SCREEN_STYLE.top,
left: PHONE_SCREEN_STYLE.left,
right: PHONE_SCREEN_STYLE.right,
bottom: PHONE_SCREEN_STYLE.bottom,
borderRadius: PHONE_SCREEN_STYLE.borderRadius,
}}
>
{menuPreviewUrl ? (
<iframe
key={previewReloadKey}
src={menuPreviewUrl}
title="پیش‌نمایش منو"
className="block h-full w-full border-0 bg-white"
/>
) : (
<div className="flex h-full w-full items-center justify-center bg-white text-description text-xs">در حال بارگذاری...</div>
)}
</div>
<img
src={iphoneCamera}
alt=""
className="pointer-events-none absolute left-1/2 z-20 -translate-x-1/2"
style={{ top: PHONE_CAMERA_STYLE.top, width: PHONE_CAMERA_STYLE.width }}
draggable={false}
/>
</div>
</div>
</div>
</div>
);