( settings: LoadedSettings, onTrustChange: (isTrusted: boolean | undefined) => void, )
| 16 | import * as process from 'process'; |
| 17 | |
| 18 | export const useFolderTrust = ( |
| 19 | settings: LoadedSettings, |
| 20 | onTrustChange: (isTrusted: boolean | undefined) => void, |
| 21 | ) => { |
| 22 | const [isTrusted, setIsTrusted] = useState<boolean | undefined>(undefined); |
| 23 | const [isFolderTrustDialogOpen, setIsFolderTrustDialogOpen] = useState(false); |
| 24 | |
| 25 | const { folderTrust, folderTrustFeature } = settings.merged; |
| 26 | useEffect(() => { |
| 27 | const trusted = isWorkspaceTrusted({ |
| 28 | folderTrust, |
| 29 | folderTrustFeature, |
| 30 | } as Settings); |
| 31 | setIsTrusted(trusted); |
| 32 | setIsFolderTrustDialogOpen(trusted === undefined); |
| 33 | onTrustChange(trusted); |
| 34 | }, [onTrustChange, folderTrust, folderTrustFeature]); |
| 35 | |
| 36 | const handleFolderTrustSelect = useCallback( |
| 37 | (choice: FolderTrustChoice) => { |
| 38 | const trustedFolders = loadTrustedFolders(); |
| 39 | const cwd = process.cwd(); |
| 40 | let trustLevel: TrustLevel; |
| 41 | |
| 42 | switch (choice) { |
| 43 | case FolderTrustChoice.TRUST_FOLDER: |
| 44 | trustLevel = TrustLevel.TRUST_FOLDER; |
| 45 | break; |
| 46 | case FolderTrustChoice.TRUST_PARENT: |
| 47 | trustLevel = TrustLevel.TRUST_PARENT; |
| 48 | break; |
| 49 | case FolderTrustChoice.DO_NOT_TRUST: |
| 50 | trustLevel = TrustLevel.DO_NOT_TRUST; |
| 51 | break; |
| 52 | default: |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | trustedFolders.setValue(cwd, trustLevel); |
| 57 | const trusted = isWorkspaceTrusted({ |
| 58 | folderTrust, |
| 59 | folderTrustFeature, |
| 60 | } as Settings); |
| 61 | setIsTrusted(trusted); |
| 62 | setIsFolderTrustDialogOpen(false); |
| 63 | onTrustChange(trusted); |
| 64 | }, |
| 65 | [onTrustChange, folderTrust, folderTrustFeature], |
| 66 | ); |
| 67 | |
| 68 | return { |
| 69 | isTrusted, |
| 70 | isFolderTrustDialogOpen, |
| 71 | handleFolderTrustSelect, |
| 72 | }; |
| 73 | }; |
no test coverage detected