Update Fix
This commit is contained in:
64
resources/js/admin/tailadmin/components/common/PageMeta.tsx
Normal file
64
resources/js/admin/tailadmin/components/common/PageMeta.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user