2026-03-28 13:51:35 +01:00
|
|
|
// ui/src/hooks/useScope.ts
|
|
|
|
|
import { useParams, useNavigate, useLocation } from 'react-router';
|
|
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
|
2026-04-08 16:23:30 +02:00
|
|
|
export type TabKey = 'exchanges' | 'dashboard' | 'runtime' | 'logs' | 'config' | 'apps';
|
2026-03-28 13:51:35 +01:00
|
|
|
|
2026-04-08 16:23:30 +02:00
|
|
|
const VALID_TABS = new Set<TabKey>(['exchanges', 'dashboard', 'runtime', 'logs', 'config', 'apps']);
|
2026-03-28 13:51:35 +01:00
|
|
|
|
|
|
|
|
export interface Scope {
|
|
|
|
|
tab: TabKey;
|
|
|
|
|
appId?: string;
|
|
|
|
|
routeId?: string;
|
|
|
|
|
exchangeId?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useScope() {
|
|
|
|
|
const params = useParams<{ tab?: string; appId?: string; routeId?: string; exchangeId?: string }>();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const location = useLocation();
|
|
|
|
|
|
|
|
|
|
// Derive tab from first URL segment — fallback to 'exchanges'
|
|
|
|
|
const rawTab = location.pathname.split('/').filter(Boolean)[0] ?? 'exchanges';
|
|
|
|
|
const tab: TabKey = VALID_TABS.has(rawTab as TabKey) ? (rawTab as TabKey) : 'exchanges';
|
|
|
|
|
|
|
|
|
|
const scope: Scope = {
|
|
|
|
|
tab,
|
|
|
|
|
appId: params.appId,
|
|
|
|
|
routeId: params.routeId,
|
|
|
|
|
exchangeId: params.exchangeId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setTab = useCallback((newTab: TabKey) => {
|
|
|
|
|
const parts = ['', newTab];
|
|
|
|
|
if (scope.appId) parts.push(scope.appId);
|
|
|
|
|
if (scope.routeId) parts.push(scope.routeId);
|
|
|
|
|
navigate(parts.join('/'));
|
|
|
|
|
}, [navigate, scope.appId, scope.routeId]);
|
|
|
|
|
|
2026-04-03 17:03:54 +02:00
|
|
|
return { scope, setTab };
|
2026-03-28 13:51:35 +01:00
|
|
|
}
|