mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-01 00:40:42 -07:00
Override Gemini CLI trust with VScode workspace trust when in IDE (#7433)
This commit is contained in:
47
packages/cli/src/ui/hooks/useIdeTrustListener.ts
Normal file
47
packages/cli/src/ui/hooks/useIdeTrustListener.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { useCallback, useEffect, useState, useSyncExternalStore } from 'react';
|
||||
import type { Config } from '@google/gemini-cli-core';
|
||||
import { ideContext } from '@google/gemini-cli-core';
|
||||
|
||||
/**
|
||||
* This hook listens for trust status updates from the IDE companion extension.
|
||||
* It provides the current trust status from the IDE and a flag indicating
|
||||
* if a restart is needed because the trust state has changed.
|
||||
*/
|
||||
export function useIdeTrustListener(config: Config) {
|
||||
const subscribe = useCallback(
|
||||
(onStoreChange: () => void) => {
|
||||
const ideClient = config.getIdeClient();
|
||||
ideClient.addTrustChangeListener(onStoreChange);
|
||||
return () => {
|
||||
ideClient.removeTrustChangeListener(onStoreChange);
|
||||
};
|
||||
},
|
||||
[config],
|
||||
);
|
||||
|
||||
const getSnapshot = () =>
|
||||
ideContext.getIdeContext()?.workspaceState?.isTrusted;
|
||||
|
||||
const isIdeTrusted = useSyncExternalStore(subscribe, getSnapshot);
|
||||
|
||||
const [needsRestart, setNeedsRestart] = useState(false);
|
||||
const [initialTrustValue] = useState(isIdeTrusted);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
!needsRestart &&
|
||||
initialTrustValue !== undefined &&
|
||||
initialTrustValue !== isIdeTrusted
|
||||
) {
|
||||
setNeedsRestart(true);
|
||||
}
|
||||
}, [isIdeTrusted, initialTrustValue, needsRestart]);
|
||||
|
||||
return { isIdeTrusted, needsRestart };
|
||||
}
|
||||
Reference in New Issue
Block a user