|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (C) 2025 Posit Software, PBC. All rights reserved. |
| 3 | + * Licensed under the Elastic License 2.0. See LICENSE.txt for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +// React. |
| 7 | +import React, { useEffect, useState } from 'react'; |
| 8 | + |
| 9 | +// Other dependencies. |
| 10 | +import { localize } from '../../../nls.js'; |
| 11 | +import { VerticalStack } from '../positronComponents/positronModalDialog/components/verticalStack.js'; |
| 12 | +import { OKCancelModalDialog } from '../positronComponents/positronModalDialog/positronOKCancelModalDialog.js'; |
| 13 | +import { IPositronDataExplorerInstance } from '../../services/positronDataExplorer/browser/interfaces/positronDataExplorerInstance.js'; |
| 14 | +import { PositronDataExplorerCommandId } from '../../contrib/positronDataExplorerEditor/browser/positronDataExplorerActions.js'; |
| 15 | +import { DropDownListBox } from '../positronComponents/dropDownListBox/dropDownListBox.js'; |
| 16 | +import { DropDownListBoxItem } from '../positronComponents/dropDownListBox/dropDownListBoxItem.js'; |
| 17 | +import { DropdownEntry } from './components/dropdownEntry.js'; |
| 18 | +import { CodeSyntaxName } from '../../services/languageRuntime/common/positronDataExplorerComm.js'; |
| 19 | +import { PositronModalReactRenderer } from '../../../base/browser/positronModalReactRenderer.js'; |
| 20 | +import { usePositronReactServicesContext } from '../../../base/browser/positronReactRendererContext.js'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Shows the convert to code modal dialog. |
| 24 | + * @param dataExplorerClientInstance The data explorer client instance. |
| 25 | + * @returns A promise that resolves when the dialog is closed. |
| 26 | + */ |
| 27 | +export const showConvertToCodeModalDialog = async ( |
| 28 | + dataExplorerClientInstance: IPositronDataExplorerInstance, |
| 29 | +): Promise<void> => { |
| 30 | + // Create the renderer. |
| 31 | + const renderer = new PositronModalReactRenderer() |
| 32 | + |
| 33 | + // Show the copy as code dialog. |
| 34 | + renderer.render( |
| 35 | + <ConvertToCodeModalDialog |
| 36 | + dataExplorerClientInstance={dataExplorerClientInstance} |
| 37 | + renderer={renderer} |
| 38 | + /> |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * ConvertToCodeDialogProps interface. |
| 44 | + */ |
| 45 | +interface ConvertToCodeDialogProps { |
| 46 | + dataExplorerClientInstance: IPositronDataExplorerInstance |
| 47 | + renderer: PositronModalReactRenderer; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +/** |
| 52 | + * ConvertToCodeModalDialog component. |
| 53 | + * @param props The component properties. |
| 54 | + * @returns The rendered component. |
| 55 | + */ |
| 56 | +export const ConvertToCodeModalDialog = (props: ConvertToCodeDialogProps) => { |
| 57 | + // Service hooks. |
| 58 | + const services = usePositronReactServicesContext(); |
| 59 | + |
| 60 | + // State hooks. |
| 61 | + const instance = props.dataExplorerClientInstance.dataExplorerClientInstance; |
| 62 | + const codeSyntaxOptions = instance.cachedBackendState?.supported_features?.convert_to_code?.code_syntaxes ?? []; |
| 63 | + |
| 64 | + const [selectedSyntax, setSelectedSyntax] = useState<CodeSyntaxName | undefined>(instance.suggestedSyntax); |
| 65 | + |
| 66 | + const [codeString, setCodeString] = useState<string | undefined>(undefined); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + const getCodeString = async () => { |
| 70 | + try { |
| 71 | + // Execute the command to get the code string based on the selected syntax. |
| 72 | + const result = await services.commandService.executeCommand(PositronDataExplorerCommandId.ConvertToCodeAction, selectedSyntax); |
| 73 | + setCodeString(result); |
| 74 | + } catch (error) { |
| 75 | + if (selectedSyntax) { |
| 76 | + setCodeString(localize( |
| 77 | + 'positron.dataExplorer.getCodeStringWithSyntax', |
| 78 | + "Cannot generate code for type {0}", |
| 79 | + selectedSyntax.code_syntax_name |
| 80 | + )); |
| 81 | + } else { |
| 82 | + setCodeString(localize( |
| 83 | + 'positron.dataExplorer.getCodeStringNoSyntax', |
| 84 | + "Cannot generate code" |
| 85 | + )); |
| 86 | + } |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + getCodeString(); // Call the async function |
| 91 | + }, [selectedSyntax, services.commandService]); |
| 92 | + |
| 93 | + // Construct the syntax options dropdown entries |
| 94 | + const syntaxDropdownEntries = () => { |
| 95 | + return syntaxInfoToDropDownItems(codeSyntaxOptions); |
| 96 | + }; |
| 97 | + |
| 98 | + const syntaxInfoToDropDownItems = ( |
| 99 | + syntaxes: CodeSyntaxName[] |
| 100 | + ): DropDownListBoxItem<string, CodeSyntaxName>[] => { |
| 101 | + return syntaxes.map( |
| 102 | + (syntax) => |
| 103 | + new DropDownListBoxItem<string, CodeSyntaxName>({ |
| 104 | + identifier: syntax.code_syntax_name, |
| 105 | + value: syntax, |
| 106 | + }) |
| 107 | + ); |
| 108 | + }; |
| 109 | + |
| 110 | + const syntaxDropdownTitle = (): string => { |
| 111 | + // if selectedSyntax is an object with code_syntax_name, return that name |
| 112 | + if (typeof selectedSyntax === 'object' && 'code_syntax_name' in selectedSyntax) { |
| 113 | + return (selectedSyntax as CodeSyntaxName).code_syntax_name; |
| 114 | + } |
| 115 | + return localize('positron.dataExplorer.selectCodeSyntax', 'Select Code Syntax'); |
| 116 | + } |
| 117 | + |
| 118 | + const onSelectionChanged = async (item: DropDownListBoxItem<string, CodeSyntaxName>) => { |
| 119 | + const typedItem = item as DropDownListBoxItem<string, CodeSyntaxName>; |
| 120 | + setSelectedSyntax(typedItem.options.value); |
| 121 | + |
| 122 | + // Execute the command to get the code string based on the selected syntax. |
| 123 | + try { |
| 124 | + const exc = await services.commandService.executeCommand(PositronDataExplorerCommandId.ConvertToCodeAction, typedItem.options.value); |
| 125 | + setCodeString(exc); |
| 126 | + } catch (error) { |
| 127 | + setCodeString(localize( |
| 128 | + 'positron.dataExplorer.cannotGenerateCodeForType', |
| 129 | + "Cannot generate code for type {0}", |
| 130 | + typedItem.options.value.code_syntax_name |
| 131 | + )); |
| 132 | + } |
| 133 | + }; |
| 134 | + |
| 135 | + // Render. |
| 136 | + return ( |
| 137 | + <OKCancelModalDialog |
| 138 | + catchErrors |
| 139 | + height={300} |
| 140 | + renderer={props.renderer} |
| 141 | + title={(() => localize( |
| 142 | + 'positronConvertToCodeModalDialogTitle', |
| 143 | + "Convert to Code" |
| 144 | + ))()} |
| 145 | + width={400} |
| 146 | + onAccept={() => props.renderer.dispose()} |
| 147 | + onCancel={() => props.renderer.dispose()} |
| 148 | + > |
| 149 | + <VerticalStack> |
| 150 | + <DropDownListBox |
| 151 | + createItem={(item) => ( |
| 152 | + <DropdownEntry |
| 153 | + title={item.options.identifier} |
| 154 | + /> |
| 155 | + )} |
| 156 | + entries={syntaxDropdownEntries()} |
| 157 | + title={syntaxDropdownTitle()} |
| 158 | + onSelectionChanged={onSelectionChanged} |
| 159 | + /> |
| 160 | + <pre> |
| 161 | + {codeString} |
| 162 | + </pre> |
| 163 | + </VerticalStack> |
| 164 | + |
| 165 | + </OKCancelModalDialog> |
| 166 | + ); |
| 167 | +}; |
0 commit comments