Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/workbench/components/currentRunPicker.tsx
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Button, Dialog, DialogActions, DialogBody, DialogContent, DialogSurface, DialogTitle, Dropdown, Input, Option, OptionOnSelectData, SelectionEvents } from '@fluentui/react-components';
7
import { Edit16Regular } from '@fluentui/react-icons';
8
import * as mobxlite from 'mobx-react-lite';
9
import * as React from 'react';
10
import { SimulationRunsProvider } from '../stores/simulationBaseline';
11
import { useInternalToolbarPickerStyles } from './pickerStyle';
12
13
type Props = {
14
simulationRunsProvider: SimulationRunsProvider;
15
outputFolderName: string;
16
onChange: (selected: string | undefined) => void;
17
disabled?: boolean;
18
};
19
20
export const CurrentRunPicker = mobxlite.observer(({ simulationRunsProvider, onChange, disabled, outputFolderName }: Props) => {
21
const [isRenameDialogOpen, setIsRenameDialogOpen] = React.useState(false);
22
const [newName, setNewName] = React.useState('');
23
const [runToRename, setRunToRename] = React.useState<string>('');
24
25
const id = 'currentRunPicker';
26
const styles = useInternalToolbarPickerStyles();
27
28
const handleRenameClick = (runName: string) => {
29
setRunToRename(runName);
30
setNewName(runName);
31
setIsRenameDialogOpen(true);
32
};
33
34
const handleRenameConfirm = async () => {
35
if (runToRename && newName) {
36
const success = await simulationRunsProvider.renameRun(runToRename, newName);
37
if (success) {
38
onChange(newName);
39
}
40
}
41
setIsRenameDialogOpen(false);
42
};
43
44
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
45
if (e.key === 'Enter') {
46
e.preventDefault();
47
handleRenameConfirm();
48
}
49
};
50
51
return (
52
<div className={styles.root}>
53
<label htmlFor={id}>Current run</label>
54
<div style={{ display: 'flex', alignItems: 'center' }}>
55
<Dropdown
56
aria-labelledby={id}
57
clearable={outputFolderName !== ''}
58
disabled={disabled}
59
placeholder='Select a run'
60
size='small'
61
selectedOptions={[outputFolderName]}
62
value={outputFolderName}
63
onOptionSelect={(_e: SelectionEvents, { optionValue }: OptionOnSelectData) => onChange(optionValue)}
64
>
65
{simulationRunsProvider.runs.map((run) => (
66
<Option key={run.name} value={run.name} text={run.friendlyName}>
67
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', width: '100%' }}>
68
<span>{run.friendlyName}</span>
69
<Button
70
icon={<Edit16Regular />}
71
appearance='subtle'
72
size='small'
73
onClick={(e) => {
74
e.stopPropagation();
75
handleRenameClick(run.name);
76
}}
77
/>
78
</div>
79
</Option>
80
))}
81
</Dropdown>
82
</div>
83
84
<Dialog open={isRenameDialogOpen} onOpenChange={(_, { open }) => setIsRenameDialogOpen(open)}>
85
<DialogSurface>
86
<DialogBody>
87
<DialogTitle>Rename Run</DialogTitle>
88
<DialogContent>
89
<Input
90
value={newName}
91
onChange={(e) => setNewName(e.target.value)}
92
placeholder='Enter new name'
93
onKeyDown={handleKeyDown}
94
/>
95
</DialogContent>
96
<DialogActions>
97
<Button appearance='secondary' onClick={() => setIsRenameDialogOpen(false)}>Cancel</Button>
98
<Button appearance='primary' onClick={handleRenameConfirm}>Rename ↵</Button>
99
</DialogActions>
100
</DialogBody>
101
</DialogSurface>
102
</Dialog>
103
</div>
104
);
105
});
106
107