Skip to content

Commit 6e2c25b

Browse files
committed
Add way to save pipeline to clipboard
Addresses #10
1 parent fc1ee2f commit 6e2c25b

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

app/components/ui/Overlay.tsx

+27-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function OverlayUI() {
7676
const [isHelpOpen, setIsHelpOpen] = useState(false);
7777
const [isSettingsOpen, setIsSettingsOpen] = useState(false);
7878
const [isHelpFirstTime, setHelpFirstTime] = useAtom(helpAtom);
79+
const [error, setError] = useState<Error>();
7980

8081
useEffect(() => {
8182
if (isHelpFirstTime) {
@@ -103,13 +104,19 @@ export function OverlayUI() {
103104
setIsOpen(false);
104105
};
105106

106-
const savePipeline = () => {
107+
const pipelineToJSON = () => {
107108
const pipelineToSave = structuredClone(pipeline);
108109
// set model and provider
109110
pipelineToSave.provider = litlyticsConfig.provider;
110111
pipelineToSave.model = litlyticsConfig.model;
111112
// Convert the object to a JSON string
112113
const jsonString = JSON.stringify(pipeline, null, 2);
114+
return jsonString;
115+
};
116+
117+
const savePipeline = () => {
118+
setError(undefined);
119+
const jsonString = pipelineToJSON();
113120
// Create a Blob from the JSON string
114121
const blob = new Blob([jsonString], { type: 'application/json' });
115122
// Create a temporary URL for the Blob
@@ -126,6 +133,18 @@ export function OverlayUI() {
126133
setIsSaveOpen(false);
127134
};
128135

136+
const savePipelineToClipboard = async () => {
137+
setError(undefined);
138+
try {
139+
const jsonString = pipelineToJSON();
140+
await navigator.clipboard.writeText(jsonString);
141+
// close modal
142+
setIsSaveOpen(false);
143+
} catch (err) {
144+
setError(err as Error);
145+
}
146+
};
147+
129148
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
130149
const file = event.target.files?.[0];
131150
if (file) {
@@ -272,13 +291,19 @@ export function OverlayUI() {
272291
/>
273292
</Field>
274293
</FieldGroup>
294+
{error && (
295+
<div className="flex items-center justify-between bg-red-400 dark:bg-red-700 rounded-xl py-1 px-2 my-2">
296+
Error saving pipleine: {error.message}
297+
</div>
298+
)}
275299
</DialogBody>
276300
<DialogActions className="flex justify-between">
277301
<Button plain onClick={() => setIsSaveOpen(false)}>
278302
Close
279303
</Button>
304+
<Button onClick={savePipelineToClipboard}>Copy to clipboard</Button>
280305
<Button color="green" onClick={savePipeline}>
281-
Save
306+
Save to file
282307
</Button>
283308
</DialogActions>
284309
</Dialog>

0 commit comments

Comments
 (0)