Files
pos/resources/js/admin/tailadmin/components/common/PageMeta.tsx
2026-03-06 03:26:11 +07:00

65 lines
1.6 KiB
TypeScript

import { useEffect, useState } from "react";
import { HelmetProvider, Helmet } from "react-helmet-async";
import { apiGet } from "../../lib/api";
let cachedStoreName: string | null = null;
let storeNamePromise: Promise<string | null> | null = null;
async function getStoreName(): Promise<string | null> {
if (cachedStoreName !== null) return cachedStoreName;
if (!storeNamePromise) {
storeNamePromise = apiGet<{ data: { toko?: { nama?: string } } }>(
"/admin-api/pengaturan"
)
.then((res) => {
const name = res?.data?.toko?.nama?.trim();
cachedStoreName = name ? name : null;
return cachedStoreName;
})
.catch(() => {
cachedStoreName = null;
return null;
})
.finally(() => {
storeNamePromise = null;
});
}
return storeNamePromise;
}
const PageMeta = ({
title,
description,
}: {
title: string;
description: string;
}) => {
const [storeName, setStoreName] = useState<string | null>(cachedStoreName);
useEffect(() => {
let mounted = true;
if (cachedStoreName !== null) return;
getStoreName().then((name) => {
if (mounted) setStoreName(name);
});
return () => {
mounted = false;
};
}, []);
const fullTitle = storeName ? `${storeName} - ${title}` : title;
return (
<Helmet>
<title>{fullTitle}</title>
<meta name="description" content={description} />
</Helmet>
);
};
export const AppWrapper = ({ children }: { children: React.ReactNode }) => (
<HelmetProvider>{children}</HelmetProvider>
);
export default PageMeta;