Files
paseo/packages/app/src/usage/hosts.ts
T
Mohamed Boudra 4ede81e841 Open Usage as a modal over the current screen (#6216)
* Open Usage as a modal over the current screen

The sidebar Usage icon and summary now open Usage in the adaptive modal
(a dialog on wide layouts, a bottom sheet on compact ones) instead of
navigating to the /usage screen, so closing it leaves you where you were.
The /usage route and UsageScreen are removed.

* Reset Usage modal cards when the host changes; keep scroll test body linear
2026-10-06 12:54:09 +01:00

60 lines
1.9 KiB
TypeScript

import { useCallback, useMemo } from "react";
import { useAppSettings } from "@/hooks/use-settings";
import {
useActiveWorkspaceSelection,
useLastWorkspaceSelection,
} from "@/stores/navigation-active-workspace-store";
import {
resolveUsageHostId,
resolveUsageModalHostId,
type UsageHost,
type UsageHostChoice,
} from "./model";
import { setUsageHost } from "./preferences";
import { useUsageHosts } from "./queries";
/** The active workspace's host; off a workspace route, the last workspace visited. */
function useActiveServerId(): string | null {
const active = useActiveWorkspaceSelection();
const last = useLastWorkspaceSelection();
return active?.serverId ?? last?.serverId ?? null;
}
function useUsageHostChoice(): UsageHostChoice {
const { settings } = useAppSettings();
return {
pickedServerId: settings.usage.serverId,
activeServerId: useActiveServerId(),
hosts: useUsageHosts(),
};
}
/** The host the sidebar Usage row reads, or null when none reports usage. */
export function useUsageHostId(): string | null {
return resolveUsageHostId(useUsageHostChoice());
}
/**
* The host the Usage modal shows, and the hosts to pick from. A pick
* is saved on the device, so the sidebar Usage row and later visits show the same host.
*/
export function useUsageHostSelection(): {
serverId: string | null;
connectedHosts: UsageHost[];
select: (serverId: string) => void;
} {
const { updateSettings } = useAppSettings();
const choice = useUsageHostChoice();
const connectedHosts = useMemo(
() => choice.hosts.filter((host) => host.isConnected),
[choice.hosts],
);
const select = useCallback(
(serverId: string) => {
void updateSettings((current) => ({ usage: setUsageHost(current.usage, serverId) }));
},
[updateSettings],
);
return { serverId: resolveUsageModalHostId(choice), connectedHosts, select };
}