File size: 9,122 Bytes
9470652 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | import { Field, Fieldset, Label, Legend } from "@catalyst/fieldset";
import { Radio, RadioField, RadioGroup } from "@catalyst/radio";
import React, { useRef, useState } from "react";
import { Input } from "@catalyst/input";
import { Button } from "@catalyst/button";
import { enqueueSnackbar } from "notistack";
import { useNavigate } from "@tanstack/react-router";
import { CloudArrowUpIcon, DocumentCheckIcon } from "@heroicons/react/24/solid";
import { XMarkIcon } from "@heroicons/react/24/outline";
import { useCreateConnection, useCreateFileConnection } from "@/hooks";
import { DatabaseFileType } from "@components/Library/types";
function classNames(...classes: string[]) {
return classes.filter(Boolean).join(" ");
}
const FileDragAndDrop = ({
currentFile,
setFile,
fileTypeLabel,
}: {
currentFile: File | undefined;
setFile: (file: File | undefined) => void;
fileTypeLabel: string;
}) => {
const [dragActive, setDragActive] = useState<boolean>(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const handleFileClick = () => {
if (fileInputRef.current) {
fileInputRef.current.click();
}
};
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const files = event.target.files;
if (files && files.length > 0) {
setFile(files[0]);
}
};
const handleDrop = (event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault();
event.stopPropagation();
setDragActive(false);
if (event.dataTransfer.files && event.dataTransfer.files[0]) {
setFile(event.dataTransfer.files[0]);
}
};
function handleDragLeave(event: React.DragEvent<HTMLDivElement>) {
event.preventDefault();
event.stopPropagation();
setDragActive(false);
}
function handleDragOver(event: React.DragEvent<HTMLDivElement>) {
event.preventDefault();
event.stopPropagation();
setDragActive(true);
}
function handleDragEnter(event: React.DragEvent<HTMLDivElement>) {
event.preventDefault();
event.stopPropagation();
setDragActive(true);
}
return (
<div
onDrop={handleDrop}
onDragEnter={handleDragEnter}
onDragLeave={handleDragLeave}
onDragOver={handleDragOver}
className={classNames(
"mt-2 flex justify-center rounded-lg border border-dashed border-white/60 px-6 py-10",
dragActive ? "bg-gray-700" : ""
)}
>
<div className={classNames(currentFile ? "" : "hidden", "text-center")}>
<div className="relative inline-block">
<DocumentCheckIcon
className="h-12 w-12 text-gray-300"
aria-hidden="true"
/>
<div
onClick={() => setFile(undefined)}
className="absolute -right-1 -top-1 cursor-pointer block h-3 w-3 rounded-full bg-red-500 ring-4 ring-red-500"
>
<XMarkIcon
className="h-3 w-3 text-white [&>path]:stroke-[4]"
aria-hidden="true"
/>
</div>
</div>
<p className="mt-2 text-sm leading-6 text-gray-400">
{currentFile && currentFile.name}
</p>
</div>
<div className={classNames(currentFile ? "hidden" : "", "text-center")}>
<CloudArrowUpIcon
onClick={handleFileClick}
className="cursor-pointer mx-auto h-12 w-12 text-gray-300"
aria-hidden="true"
/>
<div className="mt-4 flex text-sm leading-6 text-gray-400 justify-center">
<label
htmlFor="file-upload"
className="px-1 relative cursor-pointer rounded-md bg-gray-900 font-semibold text-white focus-within:outline-none focus-within:ring-2 focus-within:ring-indigo-600 focus-within:ring-offset-2 focus-within:ring-offset-gray-900 hover:text-indigo-500"
>
<span>Upload a file</span>
{/** Set a key so that the input is re-rendered and cleared when the file is removed */}
<input
ref={fileInputRef}
id="file-upload"
name="file-upload"
type="file"
className="sr-only"
onChange={handleFileChange}
key={currentFile?.name}
/>
</label>
<p>or drag and drop</p>
</div>
<p className="text-xs leading-5 text-gray-400 px-12 mt-4">
Creates a copy of your {fileTypeLabel} in DataLine. Changes you make
to the file will not be accessible to DataLine as it will work on the
copy you upload.
</p>
</div>
</div>
);
};
type RadioValue = DatabaseFileType | "database" | null;
const fileTypeLabelMap: { [K in DatabaseFileType]: string } = {
sqlite: "SQLite data file",
csv: "CSV file",
sas7bdat: "sas7bdat file",
excel: "Excel file",
};
const ConnectionCreator = ({ name = null }: { name: string | null }) => {
const [selectedRadio, setSelectedRadio] = useState<RadioValue>(null);
const [dsn, setDsn] = useState<string | null>(null);
const [file, setFile] = useState<File>();
const { mutate: createConnection, isPending } = useCreateConnection();
const { mutate: createFileConnection, isPending: isFilePending } =
useCreateFileConnection();
const navigate = useNavigate();
const handleCustomCreate = async () => {
// Call api with name and dsn
if (!name || !dsn) {
enqueueSnackbar({
variant: "info",
message: "Please enter a name and dsn for this connection",
});
return;
}
createConnection(
{ dsn, name, isSample: false },
{
onSuccess: () => {
enqueueSnackbar({
variant: "success",
message: "Connection created",
});
navigate({ to: "/" });
},
}
);
};
const handleFileCreate = async (type: DatabaseFileType) => {
if (!file) {
enqueueSnackbar({
variant: "info",
message: "Please add a file",
});
return;
}
if (!name) {
enqueueSnackbar({
variant: "info",
message: "Please add a name",
});
return;
}
// Limit file size to 500MB
if (file.size > 1024 * 1024 * 500) {
enqueueSnackbar({
variant: "info",
message: "File size exceeds 500MB limit",
});
return;
}
createFileConnection(
{ file, name, type },
{
onSuccess: () => {
enqueueSnackbar({
variant: "success",
message: "Connection created",
});
navigate({ to: "/" });
},
}
);
};
return (
<>
<Fieldset>
<Legend>Create a custom connection</Legend>
<RadioGroup
defaultValue=""
onChange={(selection: string) =>
setSelectedRadio(selection as RadioValue)
}
>
<RadioField>
<Radio value="database" color="white" />
<Label className="cursor-pointer">
Postgres, MySQL, Snowflake, or MS SQL Server connection string
</Label>
</RadioField>
<RadioField>
<Radio value="sqlite" color="white" />
<Label className="cursor-pointer">SQLite file</Label>
</RadioField>
<RadioField>
<Radio value="csv" color="white" />
<Label className="cursor-pointer">CSV file</Label>
</RadioField>
<RadioField>
<Radio value="excel" color="white" />
<Label className="cursor-pointer">Excel file</Label>
</RadioField>
<RadioField>
<Radio value="sas7bdat" color="white" />
<Label className="cursor-pointer">sas7bdat file</Label>
</RadioField>
</RadioGroup>
</Fieldset>
<div className="mt-10 max-w-2xl">
{selectedRadio === "database" ? (
<div>
<Field>
<Label>Connection DSN</Label>
<Input
type="text"
placeholder="postgres://myuser:mypassword@localhost:5432/mydatabase"
onChange={(e) => setDsn(e.target.value)}
/>
</Field>
<Button
className="cursor-pointer mt-4"
onClick={handleCustomCreate}
disabled={isPending}
>
Create connection
</Button>
</div>
) : (
selectedRadio && (
<div>
<Field>
<Label>{fileTypeLabelMap[selectedRadio]}</Label>
<FileDragAndDrop
setFile={setFile}
currentFile={file}
fileTypeLabel={fileTypeLabelMap[selectedRadio]}
/>
</Field>
<Button
className="cursor-pointer mt-4"
onClick={() => handleFileCreate(selectedRadio)}
disabled={isFilePending}
>
Create connection
</Button>
</div>
)
)}
</div>
</>
);
};
export default ConnectionCreator;
|