Pulse/frontend-modern/src/utils/url.ts
rcourtman 6eb1a10d9b Refactor: Code cleanup and localStorage consolidation
This commit includes comprehensive codebase cleanup and refactoring:

## Code Cleanup
- Remove dead TypeScript code (types/monitoring.ts - 194 lines duplicate)
- Remove unused Go functions (GetClusterNodes, MigratePassword, GetClusterHealthInfo)
- Clean up commented-out code blocks across multiple files
- Remove unused TypeScript exports (helpTextClass, private tag color helpers)
- Delete obsolete test files and components

## localStorage Consolidation
- Centralize all storage keys into STORAGE_KEYS constant
- Update 5 files to use centralized keys:
  * utils/apiClient.ts (AUTH, LEGACY_TOKEN)
  * components/Dashboard/Dashboard.tsx (GUEST_METADATA)
  * components/Docker/DockerHosts.tsx (DOCKER_METADATA)
  * App.tsx (PLATFORMS_SEEN)
  * stores/updates.ts (UPDATES)
- Benefits: Single source of truth, prevents typos, better maintainability

## Previous Work Committed
- Docker monitoring improvements and disk metrics
- Security enhancements and setup fixes
- API refactoring and cleanup
- Documentation updates
- Build system improvements

## Testing
- All frontend tests pass (29 tests)
- All Go tests pass (15 packages)
- Production build successful
- Zero breaking changes

Total: 186 files changed, 5825 insertions(+), 11602 deletions(-)
2025-11-04 21:50:46 +00:00

61 lines
1.4 KiB
TypeScript

const FALLBACK_BASE_URL = 'http://localhost:7655';
export function getPulseBaseUrl(): string {
if (typeof window === 'undefined' || !window.location) {
return FALLBACK_BASE_URL;
}
const { origin, protocol, hostname, port } = window.location;
if (origin && origin !== 'null') {
return origin;
}
if (!protocol || !hostname) {
return FALLBACK_BASE_URL;
}
const base = `${protocol}//${hostname}`;
return port ? `${base}:${port}` : base;
}
function getPulseOriginUrl(): URL | null {
try {
return new URL(getPulseBaseUrl());
} catch {
return null;
}
}
export function getPulseHostname(): string {
const origin = getPulseOriginUrl();
return origin?.hostname || 'localhost';
}
export function getPulsePort(): string {
const origin = getPulseOriginUrl();
if (!origin) {
return '7655';
}
if (origin.port) {
return origin.port;
}
return origin.protocol === 'https:' ? '443' : '80';
}
export function isPulseHttps(): boolean {
const origin = getPulseOriginUrl();
return origin?.protocol === 'https:';
}
export function getPulseWebSocketUrl(path = '/ws'): string {
const origin = getPulseOriginUrl();
const normalizedPath = path.startsWith('/') ? path : `/${path}`;
if (!origin) {
return `ws://localhost${normalizedPath}`;
}
const protocol = origin.protocol === 'https:' ? 'wss:' : 'ws:';
return `${protocol}//${origin.host}${normalizedPath}`;
}