useHostRuntimeSession returned the full HostRuntimeSnapshot, causing every consumer to rerender on any internal state change (probe cycles, client generation bumps). Replace with targeted hooks that return primitives/stable references so useSyncExternalStore skips rerenders when the subscribed value hasn't actually changed. New hooks: useHostRuntimeClient, useHostRuntimeConnectionStatus, useHostRuntimeLastError, useHostRuntimeAgentDirectoryStatus, useHostRuntimeIsDirectoryLoading. Existing useHostRuntimeIsConnected already followed this pattern. useHostRuntimeSnapshot kept only for settings-screen where probe data is legitimately needed.
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { useQuery } from "@tanstack/react-query";
|
|
import { useHostRuntimeClient, useHostRuntimeIsConnected } from "@/runtime/host-runtime";
|
|
import type { ProjectIcon } from "@server/shared/messages";
|
|
|
|
export function projectIconQueryKey(serverId: string, cwd: string) {
|
|
return ["projectIcon", serverId, cwd] as const;
|
|
}
|
|
|
|
interface UseProjectIconQueryOptions {
|
|
serverId: string;
|
|
cwd: string;
|
|
}
|
|
|
|
export function useProjectIconQuery({ serverId, cwd }: UseProjectIconQueryOptions) {
|
|
const client = useHostRuntimeClient(serverId);
|
|
const isConnected = useHostRuntimeIsConnected(serverId);
|
|
|
|
const query = useQuery({
|
|
queryKey: projectIconQueryKey(serverId, cwd),
|
|
queryFn: async (): Promise<ProjectIcon | null> => {
|
|
if (!client) {
|
|
throw new Error("Daemon client not available");
|
|
}
|
|
const result = await client.requestProjectIcon(cwd);
|
|
return result.icon;
|
|
},
|
|
enabled: !!client && isConnected && !!cwd,
|
|
staleTime: Infinity,
|
|
gcTime: 1000 * 60 * 60,
|
|
refetchOnMount: false,
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
});
|
|
|
|
return {
|
|
icon: query.data ?? null,
|
|
isLoading: query.isLoading,
|
|
isError: query.isError,
|
|
};
|
|
}
|