2026-02-27 20:03:42 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { useProjectsStore } from '@/stores/useProjectsStore';
|
2026-06-03 02:42:00 +03:00
|
|
|
import { isDesktopLocalOriginActive, isDesktopShell } from '@/lib/desktop';
|
2026-06-02 00:43:05 +03:00
|
|
|
import { desktopHostsGet, getDesktopHostApiUrl, locationMatchesHost, redactSensitiveUrl } from '@/lib/desktopHosts';
|
2026-04-20 15:41:15 +03:00
|
|
|
import { setDesktopWindowTitle } from '@/lib/desktopNative';
|
2026-06-02 00:43:05 +03:00
|
|
|
import { getRuntimeApiBaseUrl } from '@/lib/runtime-switch';
|
2026-02-27 20:03:42 +02:00
|
|
|
|
|
|
|
|
const APP_TITLE = 'OpenChamber';
|
|
|
|
|
|
|
|
|
|
const formatProjectLabel = (label: string): string => {
|
|
|
|
|
return label.replace(/[-_]/g, ' ').replace(/\b\w/g, (char) => char.toUpperCase());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getProjectNameFromPath = (path: string): string => {
|
|
|
|
|
const normalized = path.replace(/\\/g, '/').replace(/\/+$/, '');
|
|
|
|
|
const segments = normalized.split('/').filter(Boolean);
|
|
|
|
|
return segments[segments.length - 1] ?? '';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const buildWindowTitle = (projectLabel: string | null, instanceLabel: string | null): string => {
|
|
|
|
|
const parts = [projectLabel, instanceLabel, APP_TITLE].filter((part): part is string => typeof part === 'string' && part.trim().length > 0);
|
|
|
|
|
return parts.join(' | ');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useWindowTitle = () => {
|
|
|
|
|
const activeProject = useProjectsStore((state) => {
|
|
|
|
|
if (!state.activeProjectId) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return state.projects.find((project) => project.id === state.activeProjectId) ?? null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const projectLabel = React.useMemo(() => {
|
|
|
|
|
if (!activeProject) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const label = activeProject.label?.trim();
|
|
|
|
|
if (label) {
|
|
|
|
|
return formatProjectLabel(label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pathName = getProjectNameFromPath(activeProject.path);
|
|
|
|
|
if (pathName) {
|
|
|
|
|
return formatProjectLabel(pathName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}, [activeProject]);
|
|
|
|
|
|
|
|
|
|
const [instanceLabel, setInstanceLabel] = React.useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (typeof window === 'undefined' || !isDesktopShell()) {
|
|
|
|
|
setInstanceLabel(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
|
|
|
|
const refreshInstanceLabel = async () => {
|
|
|
|
|
try {
|
2026-06-02 00:43:05 +03:00
|
|
|
if (isDesktopLocalOriginActive()) {
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setInstanceLabel(null);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 20:03:42 +02:00
|
|
|
const localOrigin = window.__OPENCHAMBER_LOCAL_ORIGIN__ || window.location.origin;
|
2026-06-02 00:43:05 +03:00
|
|
|
const runtimeApiBaseUrl = getRuntimeApiBaseUrl();
|
2026-02-27 20:03:42 +02:00
|
|
|
|
2026-06-02 00:43:05 +03:00
|
|
|
if (runtimeApiBaseUrl && locationMatchesHost(runtimeApiBaseUrl, localOrigin)) {
|
2026-02-27 20:03:42 +02:00
|
|
|
if (!cancelled) {
|
|
|
|
|
setInstanceLabel(null);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cfg = await desktopHostsGet();
|
2026-06-02 00:43:05 +03:00
|
|
|
const match = cfg.hosts.find((host) => runtimeApiBaseUrl ? locationMatchesHost(runtimeApiBaseUrl, getDesktopHostApiUrl(host)) : false);
|
2026-02-27 20:03:42 +02:00
|
|
|
const nextLabel = match?.label?.trim() ? redactSensitiveUrl(match.label.trim()) : 'Instance';
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setInstanceLabel(nextLabel);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setInstanceLabel('Instance');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void refreshInstanceLabel();
|
|
|
|
|
|
|
|
|
|
const handleFocus = () => {
|
|
|
|
|
void refreshInstanceLabel();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener('focus', handleFocus);
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
window.removeEventListener('focus', handleFocus);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const title = React.useMemo(() => buildWindowTitle(projectLabel, instanceLabel), [projectLabel, instanceLabel]);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (typeof document !== 'undefined') {
|
|
|
|
|
document.title = title;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 02:42:00 +03:00
|
|
|
if (!isDesktopShell()) {
|
2026-02-27 20:03:42 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const applyTitle = async () => {
|
|
|
|
|
try {
|
2026-03-04 19:16:45 +02:00
|
|
|
const isMac = typeof navigator !== 'undefined' && /Macintosh|Mac OS X/.test(navigator.userAgent || '');
|
|
|
|
|
if (isMac) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 15:41:15 +03:00
|
|
|
await setDesktopWindowTitle(title);
|
2026-02-27 20:03:42 +02:00
|
|
|
} catch {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void applyTitle();
|
|
|
|
|
}, [title]);
|
|
|
|
|
};
|