Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

chore: allow drag files into box projects

Faisal Amirab6687e956eb
diff --git a/web-app/src/containers/ProjectFiles.tsx b/web-app/src/containers/ProjectFiles.tsx
index 005c78f02..0e3f5ab2d 100644
--- a/web-app/src/containers/ProjectFiles.tsx
+++ b/web-app/src/containers/ProjectFiles.tsx
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useState } from 'react'
import { useTranslation } from '@/i18n/react-i18next-compat'
-import { FileText, FilePlusIcon, Trash2 } from 'lucide-react'
+import { FileText, Trash2, PlusIcon } from 'lucide-react'
import { Button } from '@/components/ui/button'
import {
Tooltip,
@@ -113,6 +113,7 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
const [files, setFiles] = useState<ProjectFile[]>([])
const [loading, setLoading] = useState(true)
const [uploading, setUploading] = useState(false)
+ const [isDragging, setIsDragging] = useState(false)
const loadProjectFiles = useCallback(async () => {
setLoading(true)
@@ -137,28 +138,8 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
loadProjectFiles()
}, [loadProjectFiles])
- const handleUpload = async () => {
- if (!attachmentsEnabled) {
- toast.info(
- t('common:toast.attachmentsDisabledInfo.title') ??
- 'Attachments are disabled in Settings'
- )
- return
- }
-
- try {
- const selection = await serviceHub.dialog().open({
- multiple: true,
- directory: false,
- filters: [
- {
- name: 'Documents',
- extensions: SUPPORTED_EXTENSIONS,
- },
- ],
- })
- if (!selection) return
- const paths = Array.isArray(selection) ? selection : [selection]
+ const processFilePaths = useCallback(
+ async (paths: string[]) => {
if (!paths.length) return
// Get files from paths (recursively for directories)
@@ -173,6 +154,19 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
for (const p of filePaths) {
const name = p.split(/[\\/]/).pop() || p
const fileType = name.split('.').pop()?.toLowerCase()
+
+ // Filter unsupported file types
+ if (!fileType || !SUPPORTED_EXTENSIONS.includes(fileType)) {
+ toast.warning(
+ t('common:toast.unsupportedFileType.title') ??
+ 'Unsupported file type',
+ {
+ description: name,
+ }
+ )
+ continue
+ }
+
let size: number | undefined = undefined
try {
const stat = await import('@janhq/core').then((m) => m.fs.fileStat(p))
@@ -251,6 +245,33 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
} finally {
setUploading(false)
}
+ },
+ [files, loadProjectFiles, maxFileSizeMB, projectId, serviceHub, t]
+ )
+
+ const handleUpload = async () => {
+ if (!attachmentsEnabled) {
+ toast.info(
+ t('common:toast.attachmentsDisabledInfo.title') ??
+ 'Attachments are disabled in Settings'
+ )
+ return
+ }
+
+ try {
+ const selection = await serviceHub.dialog().open({
+ multiple: true,
+ directory: false,
+ filters: [
+ {
+ name: 'Documents',
+ extensions: SUPPORTED_EXTENSIONS,
+ },
+ ],
+ })
+ if (!selection) return
+ const paths = Array.isArray(selection) ? selection : [selection]
+ await processFilePaths(paths)
} catch (error) {
console.error('Failed to open file dialog:', error)
const desc =
@@ -264,6 +285,62 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
}
}
+ const handleDragOver = (e: React.DragEvent) => {
+ e.preventDefault()
+ e.stopPropagation()
+ setIsDragging(true)
+ }
+
+ const handleDragLeave = (e: React.DragEvent) => {
+ e.preventDefault()
+ e.stopPropagation()
+ setIsDragging(false)
+ }
+
+ const handleDrop = async (e: React.DragEvent) => {
+ e.preventDefault()
+ e.stopPropagation()
+ setIsDragging(false)
+
+ if (!attachmentsEnabled) {
+ toast.info(
+ t('common:toast.attachmentsDisabledInfo.title') ??
+ 'Attachments are disabled in Settings'
+ )
+ return
+ }
+
+ // Get file paths from the drop event (Tauri provides paths directly)
+ const paths: string[] = []
+ const items = e.dataTransfer.items
+ if (items) {
+ for (let i = 0; i < items.length; i++) {
+ const item = items[i]
+ if (item.kind === 'file') {
+ const file = item.getAsFile()
+ if (file && 'path' in file && typeof file.path === 'string') {
+ paths.push(file.path)
+ }
+ }
+ }
+ }
+
+ if (paths.length === 0) {
+ // Fallback for web: check dataTransfer.files
+ const dtFiles = e.dataTransfer.files
+ for (let i = 0; i < dtFiles.length; i++) {
+ const file = dtFiles[i]
+ if ('path' in file && typeof file.path === 'string') {
+ paths.push(file.path)
+ }
+ }
+ }
+
+ if (paths.length > 0) {
+ await processFilePaths(paths)
+ }
+ }
+
const handleDeleteFile = async (fileId: string) => {
try {
const ext = ExtensionManager.getInstance().get<VectorDBExtension>(
@@ -303,7 +380,7 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
{uploading ? (
<IconLoader2 className="size-4 animate-spin" />
) : (
- <FilePlusIcon className="size-4" />
+ <PlusIcon className="size-4" />
)}
</Button>
</div>
@@ -313,14 +390,33 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
<IconLoader2 className="size-6 animate-spin text-muted-foreground" />
</div>
) : isEmpty ? (
- <div className="flex flex-col items-center justify-center py-8 px-4 rounded-lg bg-secondary/30 border border-dashed border-border">
+ <div
+ className={cn(
+ 'flex flex-col items-center justify-center py-8 px-4 rounded-lg border border-dashed cursor-pointer transition-colors',
+ isDragging
+ ? 'bg-primary/10 border-primary'
+ : 'bg-secondary/30 border-border hover:bg-secondary/50 hover:border-primary/50'
+ )}
+ onClick={handleUpload}
+ onDragOver={handleDragOver}
+ onDragLeave={handleDragLeave}
+ onDrop={handleDrop}
+ >
<FileText className="size-8 text-muted-foreground/50 mb-3" />
<p className="text-sm text-muted-foreground text-center">
{t('common:projects.filesDescription')}
</p>
</div>
) : (
- <div className="space-y-2 max-h-60 overflow-y-auto">
+ <div
+ className={cn(
+ 'space-y-2 max-h-60 overflow-y-auto rounded-lg p-1 -m-1 transition-colors',
+ isDragging && 'bg-primary/10 ring-2 ring-primary ring-dashed'
+ )}
+ onDragOver={handleDragOver}
+ onDragLeave={handleDragLeave}
+ onDrop={handleDrop}
+ >
{files.map((file) => (
<div
key={file.id}
@@ -330,7 +426,7 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
'group hover:bg-secondary/50 transition-colors'
)}
>
- <div className="flex-shrink-0">
+ <div className="shrink-0">
<IconPaperclip className="size-5 text-muted-foreground" />
</div>
<div className="flex-1 min-w-0">
@@ -358,6 +454,24 @@ export default function ProjectFiles({ projectId, lng }: ProjectFilesProps) {
</Button>
</div>
))}
+
+ <div
+ className={cn(
+ 'flex mt-2 flex-col items-center justify-center py-8 px-4 rounded-lg border border-dashed cursor-pointer transition-colors',
+ isDragging
+ ? 'bg-primary/10 border-primary'
+ : 'bg-secondary/30 border-border hover:bg-secondary/50 hover:border-primary/50'
+ )}
+ onClick={handleUpload}
+ onDragOver={handleDragOver}
+ onDragLeave={handleDragLeave}
+ onDrop={handleDrop}
+ >
+ <FileText className="size-8 text-muted-foreground/50 mb-3" />
+ <p className="text-sm text-muted-foreground text-center">
+ {t('common:projects.filesDescription')}
+ </p>
+ </div>
</div>
)}
</div>
diff --git a/web-app/src/main.tsx b/web-app/src/main.tsx
index bcca4f384..8bef9bef7 100644
--- a/web-app/src/main.tsx
+++ b/web-app/src/main.tsx
@@ -46,9 +46,22 @@ const setupMobileViewport = () => {
}
}
+// Prevent browser from opening dropped files
+const preventDefaultFileDrop = () => {
+ document.addEventListener('dragover', (e) => {
+ e.preventDefault()
+ })
+ document.addEventListener('drop', (e) => {
+ e.preventDefault()
+ })
+}
+
// Initialize mobile setup
setupMobileViewport()
+// Prevent files from opening when dropped
+preventDefaultFileDrop()
+
// Create a new router instance
const router = createRouter({ routeTree })
diff --git a/web-app/src/routes/project/$projectId.tsx b/web-app/src/routes/project/$projectId.tsx
index b330a0353..536f0cef1 100644
--- a/web-app/src/routes/project/$projectId.tsx
+++ b/web-app/src/routes/project/$projectId.tsx
@@ -126,6 +126,34 @@ function ProjectPageContent() {
/>
</div>
+ {/* Conversation Section */}
+ {projectThreads.length > 0 && (
+ <div className="flex flex-col mb-6">
+ <h2 className="text-base font-medium mb-4">
+ {t('projects.conversation')}
+ </h2>
+ <SidebarMenu>
+ <ThreadList
+ threads={projectThreads}
+ currentProjectId={projectId}
+ />
+ </SidebarMenu>
+ </div>
+ )}
+
+ {/* Empty State */}
+ {projectThreads.length === 0 && (
+ <div className="flex flex-col items-center justify-center pt-6 pb-12 text-center">
+ <MessageCircle className="size-8 text-muted-foreground/50 mb-3" />
+ <h3 className="text-base font-medium text-foreground mb-1">
+ {t('projects.noConversationsIn', { projectName: project.name })}
+ </h3>
+ <p className="text-sm text-muted-foreground">
+ {t('projects.startNewConversation', { projectName: project.name })}
+ </p>
+ </div>
+ )}
+
{/* Project Settings Card */}
<div className="rounded-xl border border-border overflow-hidden mb-6">
{/* Assistant Section */}
@@ -154,41 +182,13 @@ function ProjectPageContent() {
size="icon-xs"
onClick={() => setEditDialogOpen(true)}
>
- <PencilIcon className="size-4" />
+ <PencilIcon className="size-3" />
</Button>
</div>
{/* Files Section */}
<ProjectFiles projectId={projectId} lng={i18n.language} />
</div>
-
- {/* Conversation Section */}
- {projectThreads.length > 0 && (
- <div className="flex flex-col mb-6">
- <h2 className="text-base font-medium mb-4">
- {t('projects.conversation')}
- </h2>
- <SidebarMenu>
- <ThreadList
- threads={projectThreads}
- currentProjectId={projectId}
- />
- </SidebarMenu>
- </div>
- )}
-
- {/* Empty State */}
- {projectThreads.length === 0 && (
- <div className="flex flex-col items-center justify-center py-12 text-center">
- <MessageCircle className="size-8 text-muted-foreground/50 mb-3" />
- <h3 className="text-base font-medium text-foreground mb-1">
- {t('projects.noConversationsIn', { projectName: project.name })}
- </h3>
- <p className="text-sm text-muted-foreground">
- {t('projects.startNewConversation', { projectName: project.name })}
- </p>
- </div>
- )}
</div>
</div>