Files
gridpilot.gg/apps/website/lib/mode.ts
2025-12-02 00:19:49 +01:00

75 lines
1.8 KiB
TypeScript

/**
* Mode detection system for GridPilot website
*
* Controls whether the site shows pre-launch content or full platform
* Based on GRIDPILOT_MODE environment variable
*/
export type AppMode = 'pre-launch' | 'post-launch';
const VALID_MODES: readonly AppMode[] = ['pre-launch', 'post-launch'] as const;
/**
* Get the current application mode from environment variable
* Defaults to 'pre-launch' if not set or invalid
*
* @throws {Error} If mode is set but invalid (development only)
* @returns {AppMode} The current application mode
*/
export function getAppMode(): AppMode {
const mode = process.env.GRIDPILOT_MODE;
if (!mode) {
return 'pre-launch';
}
if (!isValidMode(mode)) {
const validModes = VALID_MODES.join(', ');
const error = `Invalid GRIDPILOT_MODE: "${mode}". Must be one of: ${validModes}`;
if (process.env.NODE_ENV === 'development') {
throw new Error(error);
}
console.error(error);
return 'pre-launch';
}
return mode;
}
/**
* Type guard to check if a string is a valid AppMode
*/
function isValidMode(mode: string): mode is AppMode {
return VALID_MODES.includes(mode as AppMode);
}
/**
* Check if currently in pre-launch mode
*/
export function isPreLaunch(): boolean {
return getAppMode() === 'pre-launch';
}
/**
* Check if currently in post-launch mode
*/
export function isPostLaunch(): boolean {
return getAppMode() === 'post-launch';
}
/**
* Get list of public routes that are always accessible
*/
export function getPublicRoutes(): readonly string[] {
return ['/', '/api/signup'] as const;
}
/**
* Check if a route is public (accessible in all modes)
*/
export function isPublicRoute(pathname: string): boolean {
const publicRoutes = getPublicRoutes();
return publicRoutes.includes(pathname);
}