Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Frontend wip

Edward Cuie0a5111fe519
diff --git a/client/src/api/sites.ts b/client/src/api/sites.ts
new file mode 100644
index 000000000..e223f08bf
--- /dev/null
+++ b/client/src/api/sites.ts
@@ -0,0 +1,57 @@
+import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
+
+type SiteImport = {
+ importId: string;
+ source: string;
+ status: string;
+ importedEvents: number | null;
+ errorMessage: string | null;
+ startedAt: string;
+ completedAt: string | null;
+ fileName: string;
+};
+
+async function getSiteImports(siteId: number): Promise<{ imports: SiteImport[] }> {
+ const res = await fetch(`/api/sites/${siteId}/imports`);
+ if (!res.ok) {
+ throw new Error("Failed to fetch site imports");
+ }
+ return res.json();
+}
+
+async function importSiteData(siteId: number, file: File, source: string) {
+ const formData = new FormData();
+ formData.append("file", file);
+ formData.append("source", source);
+
+ const res = await fetch(`/api/sites/${siteId}/import`, {
+ method: "POST",
+ body: formData,
+ });
+
+ if (!res.ok) {
+ const errorData = await res.json();
+ throw new Error(errorData.message || "Failed to import site data");
+ }
+
+ return res.json();
+}
+
+export function useGetSiteImports(siteId: number) {
+ return useQuery({
+ queryKey: ["siteImports", siteId],
+ queryFn: () => getSiteImports(siteId),
+ refetchInterval: 5000, // Refetch every 5 seconds
+ });
+}
+
+export function useImportSiteData(siteId: number) {
+ const queryClient = useQueryClient();
+ return useMutation({
+ mutationFn: (newImport: { file: File; source: string }) =>
+ importSiteData(siteId, newImport.file, newImport.source),
+ onSuccess: () => {
+ queryClient.invalidateQueries({ queryKey: ["siteImports", siteId] });
+ },
+ });
+}
diff --git a/client/src/components/SiteSettings/ImportManager.tsx b/client/src/components/SiteSettings/ImportManager.tsx
new file mode 100644
index 000000000..4f3c49a15
--- /dev/null
+++ b/client/src/components/SiteSettings/ImportManager.tsx
@@ -0,0 +1,105 @@
+"use client";
+
+import { useState } from "react";
+import { Button } from "@/components/ui/button";
+import { Input } from "@/components/ui/input";
+import { Label } from "@/components/ui/label";
+import { Progress } from "@/components/ui/progress";
+import {
+ Table,
+ TableBody,
+ TableCell,
+ TableHead,
+ TableHeader,
+ TableRow,
+} from "@/components/ui/table";
+import { DateTime } from "luxon";
+import { useGetSiteImports, useImportSiteData } from "@/api/sites";
+
+export function ImportManager({ siteId, disabled }: { siteId: number, disabled: boolean }) {
+ const [file, setFile] = useState<File | null>(null);
+ const [source] = useState("umami"); // Default to umami, can be extended later
+
+ const { data, isLoading, error } = useGetSiteImports(siteId);
+ const mutation = useImportSiteData(siteId);
+
+ const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
+ if (event.target.files) {
+ setFile(event.target.files[0]);
+ }
+ };
+
+ const handleImport = () => {
+ if (file) {
+ mutation.mutate({ file, source });
+ setFile(null);
+ }
+ };
+
+ return (
+ <div className="space-y-6">
+ <div>
+ <h3 className="text-lg font-medium">Import Data</h3>
+ <p className="text-sm text-muted-foreground">
+ Import site data from other analytics platforms.
+ </p>
+ </div>
+ <div className="space-y-4">
+ <div className="grid w-full max-w-sm items-center gap-1.5">
+ <Label htmlFor="file">CSV File</Label>
+ <Input id="file" type="file" accept=".csv" onChange={handleFileChange} disabled={disabled}/>
+ </div>
+ <Button onClick={handleImport} disabled={!file || mutation.isPending || disabled}>
+ {mutation.isPending ? "Importing..." : "Import"}
+ </Button>
+ {mutation.isError && (
+ <p className="text-sm text-red-500">
+ {mutation.error.message}
+ </p>
+ )}
+ </div>
+
+ <div>
+ <h3 className="text-lg font-medium">Import History</h3>
+ {isLoading ? (
+ <p>Loading import history...</p>
+ ) : error ? (
+ <p className="text-sm text-red-500">
+ Failed to load import history.
+ </p>
+ ) : (
+ <Table>
+ <TableHeader>
+ <TableRow>
+ <TableHead>File</TableHead>
+ <TableHead>Source</TableHead>
+ <TableHead>Status</TableHead>
+ <TableHead>Imported</TableHead>
+ <TableHead>Started</TableHead>
+ <TableHead>Completed</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ {data?.imports?.map((imp) => (
+ <TableRow key={imp.importId}>
+ <TableCell>{imp.fileName}</TableCell>
+ <TableCell>{imp.source}</TableCell>
+ <TableCell>
+ <div className="flex items-center gap-2">
+ <span>{imp.status}</span>
+ {(imp.status === 'processing' || imp.status === 'pending') && <Progress value={50} className="w-24" />}
+ </div>
+ {imp.errorMessage && <p className="text-xs text-red-500">{imp.errorMessage}</p>}
+ </TableCell>
+ <TableCell>{imp.importedEvents ?? "N/A"}</TableCell>
+ <TableCell>{DateTime.fromISO(imp.startedAt).toRelative()}</TableCell>
+ <TableCell>{imp.completedAt ? DateTime.fromISO(imp.completedAt).toRelative() : "N/A"}</TableCell>
+ </TableRow>
+ ))}
+ </TableBody>
+ </Table>
+ )}
+ </div>
+ </div>
+ );
+}
diff --git a/client/src/components/SiteSettings/SiteSettings.tsx b/client/src/components/SiteSettings/SiteSettings.tsx
index 6358a17a7..daa391b3c 100644
--- a/client/src/components/SiteSettings/SiteSettings.tsx
+++ b/client/src/components/SiteSettings/SiteSettings.tsx
@@ -17,8 +17,9 @@ import {
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { SiteResponse, useGetSite } from "@/api/admin/sites";
-import { useUserOrganizations } from "../../api/admin/organizations";
+import { useUserOrganizations } from "@/api/admin/organizations";
import { ApiKeyManager } from "./ApiKeyManager";
+import { ImportManager } from "./ImportManager";
import { ScriptBuilder } from "./ScriptBuilder";
import { SiteConfiguration } from "./SiteConfiguration";
@@ -66,7 +67,7 @@ function SiteSettingsInner({
</Button>
)}
</DialogTrigger>
- <DialogContent className="sm:max-w-[500px]">
+ <DialogContent className="sm:max-w-[600px]">
<DialogHeader>
<DialogTitle>Site Settings</DialogTitle>
<DialogDescription>
@@ -74,10 +75,11 @@ function SiteSettingsInner({
</DialogDescription>
</DialogHeader>
<Tabs value={activeTab} onValueChange={setActiveTab} className="pb-4">
- <TabsList className="grid w-full grid-cols-3">
+ <TabsList className="grid w-full grid-cols-4">
<TabsTrigger value="script">Tracking Script</TabsTrigger>
<TabsTrigger value="apikey">API Key</TabsTrigger>
<TabsTrigger value="settings">Site Settings</TabsTrigger>
+ <TabsTrigger value="import">Import</TabsTrigger>
</TabsList>
<TabsContent
@@ -101,6 +103,12 @@ function SiteSettingsInner({
onClose={() => setDialogOpen(false)}
/>
</TabsContent>
+ <TabsContent
+ value="import"
+ className="pt-4 space-y-4 max-h-[70vh] overflow-y-auto"
+ >
+ <ImportManager siteId={siteMetadata.siteId} disabled={disabled} />
+ </TabsContent>
</Tabs>
<DialogFooter>
@@ -111,4 +119,4 @@ function SiteSettingsInner({
</DialogContent>
</Dialog>
);
-}
\ No newline at end of file
+}