Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add dashboard paste-to-import component

Richie McIlroyc3c2748579ac
diff --git a/apps/web/app/(org)/dashboard/_components/DashboardPasteImport.tsx b/apps/web/app/(org)/dashboard/_components/DashboardPasteImport.tsx
new file mode 100644
index 000000000..d30793b39
--- /dev/null
+++ b/apps/web/app/(org)/dashboard/_components/DashboardPasteImport.tsx
@@ -0,0 +1,106 @@
+"use client";
+
+import { useStore } from "@tanstack/react-store";
+import { useRouter } from "next/navigation";
+import { useCallback, useEffect, useRef } from "react";
+import { toast } from "sonner";
+import { useDashboardContext } from "../Contexts";
+import { useUploadingContext } from "../caps/UploadingContext";
+import { importMediaFile, isSupportedMediaFile } from "../import/import-media";
+
+function isFile(file: File | null): file is File {
+ return file !== null;
+}
+
+function getClipboardFiles(data: DataTransfer) {
+ const filesFromItems = Array.from(data.items)
+ .filter((item) => item.kind === "file")
+ .map((item) => item.getAsFile())
+ .filter(isFile);
+ const filesFromList = Array.from(data.files);
+ const seen = new Set<string>();
+
+ return [...filesFromItems, ...filesFromList].filter((file) => {
+ const key = `${file.name}:${file.size}:${file.type}:${file.lastModified}`;
+ if (seen.has(key)) return false;
+ seen.add(key);
+ return true;
+ });
+}
+
+function shouldIgnorePasteTarget(target: EventTarget | null) {
+ if (!(target instanceof Element)) return false;
+ return (
+ target.closest(
+ "input, textarea, select, [role='textbox'], [contenteditable]:not([contenteditable='false'])",
+ ) !== null
+ );
+}
+
+export function DashboardPasteImport() {
+ const router = useRouter();
+ const { activeOrganization, setUpgradeModalOpen, user } =
+ useDashboardContext();
+ const { uploadingStore, setUploadStatus } = useUploadingContext();
+ const isUploading = useStore(uploadingStore, (s) => !!s.uploadStatus);
+ const handlingPasteRef = useRef(false);
+
+ const handlePaste = useCallback(
+ (event: ClipboardEvent) => {
+ if (shouldIgnorePasteTarget(event.target)) return;
+
+ const data = event.clipboardData;
+ if (!data) return;
+
+ const file = getClipboardFiles(data).find(isSupportedMediaFile);
+ if (!file) return;
+
+ event.preventDefault();
+
+ if (!user.isPro) {
+ setUpgradeModalOpen(true);
+ return;
+ }
+
+ if (!activeOrganization) {
+ toast.error("Select an organization before importing media.");
+ return;
+ }
+
+ if (isUploading || handlingPasteRef.current) {
+ toast.error(
+ "Wait for the current upload to finish before importing another file.",
+ );
+ return;
+ }
+
+ handlingPasteRef.current = true;
+ void importMediaFile({
+ file,
+ orgId: activeOrganization.organization.id,
+ setUploadStatus,
+ })
+ .then((ok) => {
+ if (ok) router.push("/dashboard/caps");
+ })
+ .finally(() => {
+ handlingPasteRef.current = false;
+ });
+ },
+ [
+ activeOrganization,
+ isUploading,
+ router,
+ setUploadStatus,
+ setUpgradeModalOpen,
+ user.isPro,
+ ],
+ );
+
+ useEffect(() => {
+ window.addEventListener("paste", handlePaste);
+ return () => window.removeEventListener("paste", handlePaste);
+ }, [handlePaste]);
+
+ return null;
+}
diff --git a/apps/web/app/(org)/dashboard/layout.tsx b/apps/web/app/(org)/dashboard/layout.tsx
index c6185eef1..dde0a568b 100644
--- a/apps/web/app/(org)/dashboard/layout.tsx
+++ b/apps/web/app/(org)/dashboard/layout.tsx
@@ -8,6 +8,7 @@ import { AuthContextProvider } from "@/app/Layout/AuthContext";
import { resolveCurrentUser } from "@/app/Layout/current-user";
import { runPromise } from "@/lib/server";
import DashboardInner from "./_components/DashboardInner";
+import { DashboardPasteImport } from "./_components/DashboardPasteImport";
import MobileTab from "./_components/MobileTab";
import DesktopNav from "./_components/Navbar/Desktop";
import MobileNav from "./_components/Navbar/Mobile";
@@ -105,6 +106,7 @@ export default async function DashboardLayout({
userPreferences={userPreferences}
referClicked={referClicked === "true"}
>
+ <DashboardPasteImport />
<div className="bg-gray-2 dashboard-grid">
<DesktopNav />
<div className="flex h-full [grid-area:main] focus:outline-none">