6 changed files with 92 additions and 9 deletions
@ -0,0 +1,54 @@ |
|||||
|
"use client"; |
||||
|
|
||||
|
import { useEffect, useState } from "react"; |
||||
|
import { DEFAULT_SITE_EXPLORE_URL } from "@/utils/loginUrl"; |
||||
|
|
||||
|
// Cache the resolved URL across component mounts so the link does not flicker
|
||||
|
// once /api/env-status has answered for the current environment.
|
||||
|
let cachedExploreUrl: string | null = null; |
||||
|
let pendingRequest: Promise<string> | null = null; |
||||
|
|
||||
|
function fetchSiteExploreUrl(): Promise<string> { |
||||
|
if (cachedExploreUrl) return Promise.resolve(cachedExploreUrl); |
||||
|
if (pendingRequest) return pendingRequest; |
||||
|
|
||||
|
pendingRequest = fetch("/api/env-status") |
||||
|
.then((response) => (response.ok ? response.json() : null)) |
||||
|
.then((status) => { |
||||
|
const url = |
||||
|
typeof status?.siteExploreUrl === "string" && status.siteExploreUrl |
||||
|
? status.siteExploreUrl |
||||
|
: DEFAULT_SITE_EXPLORE_URL; |
||||
|
cachedExploreUrl = url; |
||||
|
return url; |
||||
|
}) |
||||
|
.catch(() => DEFAULT_SITE_EXPLORE_URL) |
||||
|
.finally(() => { |
||||
|
pendingRequest = null; |
||||
|
}); |
||||
|
|
||||
|
return pendingRequest; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Resolves the Popi site explore URL for the current deployment environment |
||||
|
* (test -> https://wwwtest.popi.art/explore, prod -> https://www.popi.art/explore).
|
||||
|
* Derived server-side from POPIART_API_BASE_URL and exposed via /api/env-status. |
||||
|
*/ |
||||
|
export function useSiteExploreUrl(): string { |
||||
|
const [exploreUrl, setExploreUrl] = useState<string>( |
||||
|
cachedExploreUrl ?? DEFAULT_SITE_EXPLORE_URL, |
||||
|
); |
||||
|
|
||||
|
useEffect(() => { |
||||
|
let cancelled = false; |
||||
|
void fetchSiteExploreUrl().then((url) => { |
||||
|
if (!cancelled) setExploreUrl(url); |
||||
|
}); |
||||
|
return () => { |
||||
|
cancelled = true; |
||||
|
}; |
||||
|
}, []); |
||||
|
|
||||
|
return exploreUrl; |
||||
|
} |
||||
@ -1 +1,7 @@ |
|||||
export const LOGIN_REDIRECT_URL = "https://wwwtest.popi.art/explore"; |
// Fallback explore URL used before /api/env-status resolves, or if it fails.
|
||||
|
// The real per-environment URL is derived server-side from POPIART_API_BASE_URL
|
||||
|
// (see /api/env-status) and consumed via the useSiteExploreUrl() hook.
|
||||
|
export const DEFAULT_SITE_EXPLORE_URL = "https://www.popi.art/explore"; |
||||
|
|
||||
|
/** @deprecated Use the useSiteExploreUrl() hook so the target follows the deployment environment. */ |
||||
|
export const LOGIN_REDIRECT_URL = DEFAULT_SITE_EXPLORE_URL; |
||||
|
|||||
Loading…
Reference in new issue