/** * Subdomain-aware URL helpers for rSpace navigation. * * Canonical URL pattern: {space}.rspace.online/{moduleId} * Fallback (localhost): /{space}/{moduleId} */ const RESERVED_SUBDOMAINS = ["www", "rspace", "create", "new", "start", "auth"]; /** Detect if the current page is on a {space}.rspace.online subdomain */ export function isSubdomain(): boolean { const parts = window.location.host.split(":")[0].split("."); return ( parts.length >= 3 && parts.slice(-2).join(".") === "rspace.online" && !RESERVED_SUBDOMAINS.includes(parts[0]) ); } /** Get the current space from subdomain or path */ export function getCurrentSpace(): string { const hostParts = window.location.host.split(":")[0].split("."); if ( hostParts.length >= 3 && hostParts.slice(-2).join(".") === "rspace.online" && !RESERVED_SUBDOMAINS.includes(hostParts[0]) ) { return hostParts[0]; } const pathParts = window.location.pathname.split("/").filter(Boolean); return pathParts[0] || "demo"; } /** Get the current module from the path (works for both subdomain and path routing) */ export function getCurrentModule(): string { const parts = window.location.pathname.split("/").filter(Boolean); if (isSubdomain()) { return parts[0] || "canvas"; } return parts[1] || "canvas"; } /** * Generate a navigation URL for a given space + module. * * On subdomains: same-space links use /{moduleId}, cross-space links * switch the subdomain to {newSpace}.rspace.online/{moduleId}. * On bare domain or localhost: uses /{space}/{moduleId}. */ export function rspaceNavUrl(space: string, moduleId: string): string { const hostParts = window.location.host.split(":")[0].split("."); const onSubdomain = hostParts.length >= 3 && hostParts.slice(-2).join(".") === "rspace.online" && !RESERVED_SUBDOMAINS.includes(hostParts[0]); if (onSubdomain) { // Same space → just change the path if (hostParts[0] === space) { return `/${moduleId}`; } // Different space → switch subdomain const baseDomain = hostParts.slice(-2).join("."); return `${window.location.protocol}//${space}.${baseDomain}/${moduleId}`; } // Bare domain (rspace.online) or localhost → use /{space}/{moduleId} const onRspace = window.location.host.includes("rspace.online") && !window.location.host.startsWith("www"); if (onRspace) { // Prefer subdomain routing return `${window.location.protocol}//${space}.rspace.online/${moduleId}`; } // Localhost/dev return `/${space}/${moduleId}`; }