2025-08-08 11:02:27 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-20 12:49:15 -07:00
|
|
|
import { useState, useCallback, useEffect } from 'react';
|
2025-09-06 01:39:02 -04:00
|
|
|
import { type Config } from '@google/gemini-cli-core';
|
|
|
|
|
import type { LoadedSettings } from '../../config/settings.js';
|
2025-08-08 11:02:27 -07:00
|
|
|
import { FolderTrustChoice } from '../components/FolderTrustDialog.js';
|
2025-08-14 11:15:48 -07:00
|
|
|
import {
|
|
|
|
|
loadTrustedFolders,
|
|
|
|
|
TrustLevel,
|
|
|
|
|
isWorkspaceTrusted,
|
|
|
|
|
} from '../../config/trustedFolders.js';
|
2025-08-25 22:11:27 +02:00
|
|
|
import * as process from 'node:process';
|
2025-08-08 11:02:27 -07:00
|
|
|
|
2025-08-14 11:15:48 -07:00
|
|
|
export const useFolderTrust = (
|
|
|
|
|
settings: LoadedSettings,
|
2025-09-06 01:39:02 -04:00
|
|
|
config: Config,
|
2025-08-14 11:15:48 -07:00
|
|
|
onTrustChange: (isTrusted: boolean | undefined) => void,
|
2025-09-08 16:41:39 +00:00
|
|
|
refreshStatic: () => void,
|
2025-08-14 11:15:48 -07:00
|
|
|
) => {
|
|
|
|
|
const [isTrusted, setIsTrusted] = useState<boolean | undefined>(undefined);
|
|
|
|
|
const [isFolderTrustDialogOpen, setIsFolderTrustDialogOpen] = useState(false);
|
2025-09-06 01:39:02 -04:00
|
|
|
const [isRestarting] = useState(false);
|
2025-08-14 11:15:48 -07:00
|
|
|
|
2025-08-27 18:39:45 -07:00
|
|
|
const folderTrust = settings.merged.security?.folderTrust?.enabled;
|
|
|
|
|
|
2025-08-14 11:15:48 -07:00
|
|
|
useEffect(() => {
|
2025-09-06 01:39:02 -04:00
|
|
|
const trusted = isWorkspaceTrusted(settings.merged);
|
2025-08-14 11:15:48 -07:00
|
|
|
setIsTrusted(trusted);
|
|
|
|
|
setIsFolderTrustDialogOpen(trusted === undefined);
|
|
|
|
|
onTrustChange(trusted);
|
2025-09-06 01:39:02 -04:00
|
|
|
}, [folderTrust, onTrustChange, settings.merged]);
|
2025-08-08 11:02:27 -07:00
|
|
|
|
2025-09-08 16:41:39 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
// When the folder trust dialog is about to open/close, we need to force a refresh
|
|
|
|
|
// of the static content to ensure the Tips are hidden/shown correctly.
|
|
|
|
|
refreshStatic();
|
|
|
|
|
}, [isFolderTrustDialogOpen, refreshStatic]);
|
|
|
|
|
|
2025-08-14 11:15:48 -07:00
|
|
|
const handleFolderTrustSelect = useCallback(
|
|
|
|
|
(choice: FolderTrustChoice) => {
|
|
|
|
|
const trustedFolders = loadTrustedFolders();
|
|
|
|
|
const cwd = process.cwd();
|
|
|
|
|
let trustLevel: TrustLevel;
|
2025-08-13 11:06:31 -07:00
|
|
|
|
2025-08-14 11:15:48 -07:00
|
|
|
switch (choice) {
|
|
|
|
|
case FolderTrustChoice.TRUST_FOLDER:
|
|
|
|
|
trustLevel = TrustLevel.TRUST_FOLDER;
|
|
|
|
|
break;
|
|
|
|
|
case FolderTrustChoice.TRUST_PARENT:
|
|
|
|
|
trustLevel = TrustLevel.TRUST_PARENT;
|
|
|
|
|
break;
|
|
|
|
|
case FolderTrustChoice.DO_NOT_TRUST:
|
|
|
|
|
trustLevel = TrustLevel.DO_NOT_TRUST;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-13 11:06:31 -07:00
|
|
|
|
2025-08-14 11:15:48 -07:00
|
|
|
trustedFolders.setValue(cwd, trustLevel);
|
2025-09-06 01:39:02 -04:00
|
|
|
const trusted = isWorkspaceTrusted(settings.merged);
|
|
|
|
|
setIsTrusted(trusted);
|
|
|
|
|
setIsFolderTrustDialogOpen(false);
|
|
|
|
|
onTrustChange(trusted);
|
2025-08-14 11:15:48 -07:00
|
|
|
},
|
2025-09-06 01:39:02 -04:00
|
|
|
[settings.merged, onTrustChange],
|
2025-08-14 11:15:48 -07:00
|
|
|
);
|
2025-08-08 11:02:27 -07:00
|
|
|
|
|
|
|
|
return {
|
2025-08-14 11:15:48 -07:00
|
|
|
isTrusted,
|
2025-08-08 11:02:27 -07:00
|
|
|
isFolderTrustDialogOpen,
|
|
|
|
|
handleFolderTrustSelect,
|
2025-08-21 00:38:12 -07:00
|
|
|
isRestarting,
|
2025-08-08 11:02:27 -07:00
|
|
|
};
|
|
|
|
|
};
|