Skip to content

Commit

Permalink
feat: add clipboard as output target
Browse files Browse the repository at this point in the history
  • Loading branch information
avevotsira committed Jul 17, 2024
1 parent 79e343b commit dfd25e7
Showing 1 changed file with 59 additions and 10 deletions.
69 changes: 59 additions & 10 deletions src/components/gui/export/export-result-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { Label } from "@/components/ui/label";
import { cn } from "@/lib/utils";
import { Input } from "@/components/ui/input";
import { toast } from "sonner";

export default function ExportResultButton({
data,
Expand All @@ -28,6 +29,14 @@ export default function ExportResultButton({
}) {
const [format, setFormat] = useState<string>("sql");

const OutputTargetType = {
File: "file",
Clipboard: "clipboard",
} as const;

type OutputTargetType =
(typeof OutputTargetType)[keyof typeof OutputTargetType];

const ExportSelectionType = {
Complete: "complete",
Selected: "selected",
Expand All @@ -40,6 +49,10 @@ export default function ExportResultButton({
ExportSelectionType.Complete
);

const [outputTarget, setOutputTarget] = useState<OutputTargetType>(
OutputTargetType.File
);

const [tableName, setTableName] = useState<string>("UnknownTable");

const onExportClicked = useCallback(() => {
Expand Down Expand Up @@ -68,14 +81,34 @@ export default function ExportResultButton({
content = handler();
}

const blob = new Blob([content], { type: "text/plain;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `export.${format}`;
a.click();
URL.revokeObjectURL(url);
}, [format, data, exportSelection, ExportSelectionType.Complete, tableName]);
if (outputTarget === OutputTargetType.File) {
const blob = new Blob([content], { type: "text/plain;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `export.${format}`;
a.click();
URL.revokeObjectURL(url);
} else {
navigator.clipboard.writeText(content).then(
() => {
toast.success("Content copied to clipboard");
},
(err) => {
toast.error("Failed to copy content to clipboard");
console.error("Failed to copy content: ", err);
}
);
}
}, [
format,
data,
exportSelection,
ExportSelectionType.Complete,
tableName,
outputTarget,
OutputTargetType.File,
]);

return (
<Dialog open>
Expand Down Expand Up @@ -195,8 +228,24 @@ export default function ExportResultButton({
<div className="px-4 flex flex-col gap-4 border-t pt-3">
<Label>Output Target</Label>
<div className="flex gap-2">
<Button>File</Button>
<Button variant={"outline"}>Clipboard</Button>
<Button
variant={
outputTarget === OutputTargetType.File ? "default" : "outline"
}
onClick={() => setOutputTarget(OutputTargetType.File)}
>
File
</Button>
<Button
variant={
outputTarget === OutputTargetType.Clipboard
? "default"
: "outline"
}
onClick={() => setOutputTarget(OutputTargetType.Clipboard)}
>
Clipboard
</Button>
</div>
</div>
</div>
Expand Down

0 comments on commit dfd25e7

Please sign in to comment.