Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion apps/desktop/src/lib/trpc/routers/projects/projects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from "node:fs";
import { existsSync, statSync } from "node:fs";
import { access } from "node:fs/promises";
import { basename, join } from "node:path";
import {
Expand Down Expand Up @@ -430,6 +430,61 @@ export const createProjectsRouter = (getWindow: () => BrowserWindow | null) => {
};
}),

openFromPath: publicProcedure
.input(z.object({ path: z.string() }))
.mutation(async ({ input }): Promise<OpenNewResult> => {
const selectedPath = input.path;

// Check if path exists
if (!existsSync(selectedPath)) {
return { canceled: false, error: "Path does not exist" };
}

// Check if path is a directory
try {
const stats = statSync(selectedPath);
if (!stats.isDirectory()) {
return {
canceled: false,
error: "Please drop a folder, not a file",
};
}
} catch {
return {
canceled: false,
error: "Could not access the dropped item",
};
}

let mainRepoPath: string;
try {
mainRepoPath = await getGitRoot(selectedPath);
} catch (_error) {
// Return a special response so the UI can offer to initialize git
return {
canceled: false,
needsGitInit: true,
selectedPath,
};
}

const defaultBranch = await getDefaultBranch(mainRepoPath);
const project = upsertProject(mainRepoPath, defaultBranch);

// Auto-create main workspace if it doesn't exist
await ensureMainWorkspace(project);

track("project_opened", {
project_id: project.id,
method: "drop",
});

return {
canceled: false,
project,
};
}),

initGitAndOpen: publicProcedure
.input(z.object({ path: z.string() }))
.mutation(async ({ input }) => {
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/renderer/react-query/projects/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useOpenFromPath } from "./useOpenFromPath";
export { useOpenNew } from "./useOpenNew";
export { useReorderProjects } from "./useReorderProjects";
export { useUpdateProject } from "./useUpdateProject";
24 changes: 24 additions & 0 deletions apps/desktop/src/renderer/react-query/projects/useOpenFromPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { electronTrpc } from "renderer/lib/electron-trpc";

/**
* Mutation hook for opening a project from a given path
* Used when dragging folders into the sidebar
*/
export function useOpenFromPath(
options?: Parameters<
typeof electronTrpc.projects.openFromPath.useMutation
>[0],
) {
const utils = electronTrpc.useUtils();

return electronTrpc.projects.openFromPath.useMutation({
...options,
onSuccess: async (...args) => {
// Auto-invalidate projects query
await utils.projects.getRecents.invalidate();

// Call user's onSuccess if provided
await options?.onSuccess?.(...args);
},
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
import { cn } from "@superset/ui/utils";
import { AnimatePresence, motion } from "framer-motion";
import { type ReactNode, useCallback, useEffect, useState } from "react";
import { LuFolderPlus, LuLoader, LuX } from "react-icons/lu";
import { useOpenFromPath } from "renderer/react-query/projects";
import { useCreateBranchWorkspace } from "renderer/react-query/workspaces";
import { InitGitDialog } from "../../StartView/InitGitDialog";

interface SidebarDropZoneProps {
children: ReactNode;
className?: string;
}

export function SidebarDropZone({ children, className }: SidebarDropZoneProps) {
const [isDragOver, setIsDragOver] = useState(false);
const [error, setError] = useState<string | null>(null);
const [initGitDialog, setInitGitDialog] = useState<{
isOpen: boolean;
selectedPath: string;
}>({ isOpen: false, selectedPath: "" });

const openFromPath = useOpenFromPath();
const createBranchWorkspace = useCreateBranchWorkspace();

const isProcessing =
openFromPath.isPending || createBranchWorkspace.isPending;

// Auto-dismiss error after 5 seconds
useEffect(() => {
if (!error) return;

const timer = setTimeout(() => {
setError(null);
}, 5000);

return () => clearTimeout(timer);
}, [error]);

const handleDragOver = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();

// Check if the drag contains files
if (e.dataTransfer.types.includes("Files")) {
setIsDragOver(true);
e.dataTransfer.dropEffect = "copy";
}
}, []);

const handleDragLeave = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();

// Only set to false if we're leaving the drop zone entirely
// (not just moving to a child element)
const rect = e.currentTarget.getBoundingClientRect();
const { clientX, clientY } = e;

if (
clientX < rect.left ||
clientX > rect.right ||
clientY < rect.top ||
clientY > rect.bottom
) {
setIsDragOver(false);
}
}, []);

const handleDrop = useCallback(
(e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
setIsDragOver(false);

// Prevent multiple drops while processing
if (isProcessing) return;

setError(null);

const files = Array.from(e.dataTransfer.files);

// Get the first dropped item
const firstFile = files[0];
if (!firstFile) return;

// In Electron with contextIsolation, use webUtils.getPathForFile to get the file path
let filePath: string;
try {
filePath = window.webUtils.getPathForFile(firstFile);
} catch {
setError("Could not get path from dropped item");
return;
}

if (!filePath) {
setError("Could not get path from dropped item");
return;
}

openFromPath.mutate(
{ path: filePath },
{
onSuccess: (result) => {
if ("canceled" in result && result.canceled) {
return;
}

if ("error" in result) {
setError(result.error);
return;
}

if ("needsGitInit" in result) {
// Show dialog to offer git initialization
setInitGitDialog({
isOpen: true,
selectedPath: result.selectedPath,
});
return;
}

// Create a main workspace on the current branch
if ("project" in result && result.project) {
createBranchWorkspace.mutate(
{ projectId: result.project.id },
{
onError: (err) => {
setError(
err.message ||
"Project added but failed to create workspace",
);
},
},
);
}
},
onError: (err) => {
setError(err.message || "Failed to open project");
},
},
);
},
[openFromPath, createBranchWorkspace, isProcessing],
);

return (
// biome-ignore lint/a11y/noStaticElementInteractions: Drop zone for external files
<div
className={cn("relative h-full", className)}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
>
{children}

<AnimatePresence>
{/* Drop overlay */}
{isDragOver && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
className="absolute inset-0 z-50 flex flex-col items-center justify-center m-2 rounded-lg border-2 border-dashed border-primary/60 bg-primary/5 backdrop-blur-sm"
>
<motion.div
initial={{ scale: 0.8, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.8, opacity: 0 }}
transition={{ duration: 0.15, delay: 0.05 }}
className="flex flex-col items-center gap-3"
>
<div className="rounded-full bg-primary/10 p-3">
<LuFolderPlus className="h-6 w-6 text-primary" />
</div>
<div className="text-center">
<p className="text-sm font-medium text-primary">
Drop to add project
</p>
<p className="text-xs text-muted-foreground mt-1">
Release to open folder
</p>
</div>
</motion.div>
</motion.div>
)}

{/* Processing indicator when not dragging */}
{isProcessing && !isDragOver && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
className="absolute inset-0 z-40 flex flex-col items-center justify-center bg-background/90 backdrop-blur-sm"
>
<div className="flex flex-col items-center gap-3">
<LuLoader className="h-5 w-5 text-muted-foreground animate-spin" />
<span className="text-sm text-muted-foreground">
Adding project...
</span>
</div>
</motion.div>
)}

{/* Error toast - auto-dismisses after 5s or can be manually dismissed */}
{error && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 10 }}
transition={{ duration: 0.2 }}
className="absolute bottom-3 left-3 right-3 z-50 flex items-start gap-2 rounded-md border border-destructive/20 bg-destructive/10 px-3 py-2 text-destructive shadow-sm"
>
<span className="flex-1 text-xs">{error}</span>
<button
type="button"
onClick={() => setError(null)}
className="shrink-0 rounded p-0.5 hover:bg-destructive/20 transition-colors"
aria-label="Dismiss error"
>
<LuX className="h-3.5 w-3.5" />
</button>
</motion.div>
)}
</AnimatePresence>

<InitGitDialog
isOpen={initGitDialog.isOpen}
selectedPath={initGitDialog.selectedPath}
onClose={() => setInitGitDialog({ isOpen: false, selectedPath: "" })}
onError={setError}
/>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SidebarDropZone } from "./SidebarDropZone";
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMemo } from "react";
import { useWorkspaceShortcuts } from "renderer/hooks/useWorkspaceShortcuts";
import { PortsList } from "./PortsList";
import { ProjectSection } from "./ProjectSection";
import { SidebarDropZone } from "./SidebarDropZone";
import { WorkspaceSidebarFooter } from "./WorkspaceSidebarFooter";
import { WorkspaceSidebarHeader } from "./WorkspaceSidebarHeader";

Expand All @@ -28,7 +29,7 @@ export function WorkspaceSidebar({
);

return (
<div className="flex flex-col h-full bg-background">
<SidebarDropZone className="flex flex-col h-full bg-background">
<WorkspaceSidebarHeader isCollapsed={isCollapsed} />

<div className="flex-1 overflow-y-auto hide-scrollbar">
Expand All @@ -50,14 +51,16 @@ export function WorkspaceSidebar({
{groups.length === 0 && !isCollapsed && (
<div className="flex flex-col items-center justify-center h-32 text-muted-foreground text-sm">
<span>No workspaces yet</span>
<span className="text-xs mt-1">Add a project to get started</span>
<span className="text-xs mt-1">
Add a project or drag a folder here
</span>
</div>
)}
</div>

{!isCollapsed && <PortsList />}

<WorkspaceSidebarFooter isCollapsed={isCollapsed} />
</div>
</SidebarDropZone>
);
}
Loading