import { Box, Flex, Image, Link, Text } from '@traefiklabs/faency' import { useMemo, useEffect, useState } from 'react' import { Helmet } from 'react-helmet-async' import { useParams } from 'react-router-dom' import verifySignature from '../../utils/workers/scriptVerification' import { PUBLIC_KEY } from './constants' import { SpinnerLoader } from 'components/SpinnerLoader' import { useIsDarkMode } from 'hooks/use-theme' import { TopNav } from 'layout/Navigation' const SCRIPT_URL = 'https://assets.traefik.io/hub-ui-demo.js' // Module-level cache to persist across component mount/unmount let cachedBlobUrl: string | null = null // Export a function to reset the cache (for testing) export const resetCache = () => { cachedBlobUrl = null } const HubDashboard = ({ path }: { path: string }) => { const isDarkMode = useIsDarkMode() const [scriptError, setScriptError] = useState(undefined) const [signatureVerified, setSignatureVerified] = useState(false) const [verificationInProgress, setVerificationInProgress] = useState(false) const [scriptBlobUrl, setScriptBlobUrl] = useState(null) const { id } = useParams() const usedPath = useMemo(() => { if (path?.includes(':id')) { const splitted = path.split(':') return `${splitted[0]}/${id}` } return path }, [id, path]) useEffect(() => { const verifyAndLoadScript = async () => { setVerificationInProgress(true) try { const { verified, scriptContent: content } = await verifySignature(SCRIPT_URL, `${SCRIPT_URL}.sig`, PUBLIC_KEY) if (!verified || !content) { setScriptError(true) setVerificationInProgress(false) } else { setScriptError(false) const blob = new Blob([content], { type: 'application/javascript' }) cachedBlobUrl = URL.createObjectURL(blob) setScriptBlobUrl(cachedBlobUrl) setSignatureVerified(true) setVerificationInProgress(false) } } catch { setScriptError(true) setVerificationInProgress(false) } } if (!cachedBlobUrl) { verifyAndLoadScript() } else { setScriptBlobUrl(cachedBlobUrl) setSignatureVerified(true) } }, []) if (scriptError && !verificationInProgress) { return ( Oops! We couldn't load the demo content. Don't worry — you can still learn more about{' '} Traefik Hub API Management {' '} on our{' '} website {' '} or in our{' '} documentation . ) } return ( Hub Demo - Traefik Proxy {signatureVerified && scriptBlobUrl && } {verificationInProgress ? ( ) : ( )} ) } export default HubDashboard