aboutsummaryrefslogtreecommitdiff
path: root/apps/web/components/new/add-document/file.tsx
blob: bb605f039823daf97bccc3339c6f2c7ee35ac573 (plain) (blame)
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
"use client"

import { useState, useEffect } from "react"
import { cn } from "@lib/utils"
import { dmSansClassName } from "@/lib/fonts"
import { FileIcon } from "lucide-react"
import { useHotkeys } from "react-hotkeys-hook"

export interface FileData {
	file: File | null
	title: string
	description: string
}

interface FileContentProps {
	onSubmit?: (data: { file: File; title: string; description: string }) => void
	onDataChange?: (data: FileData) => void
	isSubmitting?: boolean
	isOpen?: boolean
}

export function FileContent({
	onSubmit,
	onDataChange,
	isSubmitting,
	isOpen,
}: FileContentProps) {
	const [isDragging, setIsDragging] = useState(false)
	const [selectedFile, setSelectedFile] = useState<File | null>(null)
	const [title, setTitle] = useState("")
	const [description, setDescription] = useState("")

	const canSubmit = selectedFile !== null && !isSubmitting

	const handleSubmit = () => {
		if (canSubmit && onSubmit && selectedFile) {
			onSubmit({ file: selectedFile, title, description })
		}
	}

	const updateData = (
		newFile: File | null,
		newTitle: string,
		newDescription: string,
	) => {
		onDataChange?.({
			file: newFile,
			title: newTitle,
			description: newDescription,
		})
	}

	const handleFileChange = (file: File | null) => {
		setSelectedFile(file)
		updateData(file, title, description)
	}

	const handleTitleChange = (newTitle: string) => {
		setTitle(newTitle)
		updateData(selectedFile, newTitle, description)
	}

	const handleDescriptionChange = (newDescription: string) => {
		setDescription(newDescription)
		updateData(selectedFile, title, newDescription)
	}

	useHotkeys("mod+enter", handleSubmit, {
		enabled: isOpen && canSubmit,
		enableOnFormTags: ["INPUT", "TEXTAREA"],
	})

	// Reset content when modal closes
	useEffect(() => {
		if (!isOpen) {
			setSelectedFile(null)
			setTitle("")
			setDescription("")
			onDataChange?.({ file: null, title: "", description: "" })
		}
	}, [isOpen, onDataChange])

	const handleDragOver = (e: React.DragEvent) => {
		e.preventDefault()
		setIsDragging(true)
	}

	const handleDragLeave = (e: React.DragEvent) => {
		e.preventDefault()
		setIsDragging(false)
	}

	const handleDrop = (e: React.DragEvent) => {
		e.preventDefault()
		setIsDragging(false)
		const file = e.dataTransfer.files[0]
		if (file) {
			handleFileChange(file)
		}
	}

	const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
		const file = e.target.files?.[0]
		if (file) {
			handleFileChange(file)
		}
	}

	return (
		<div className={cn("h-full flex flex-col gap-6 pt-4", dmSansClassName())}>
			<div className="flex flex-col gap-2">
				<p className="text-[16px] font-medium pl-2">
					Upload a file (image, pdf, document, sheet)
				</p>
				<label
					onDragOver={handleDragOver}
					onDragLeave={handleDragLeave}
					onDrop={handleDrop}
					className={cn(
						"relative flex flex-col items-center justify-center gap-3 p-8 rounded-[14px] bg-[#14161A] shadow-inside-out border-2 border-dashed cursor-pointer transition-all",
						isDragging
							? "border-[#4BA0FA] bg-[#4BA0FA]/10"
							: "border-[#737373]/30 hover:border-[#737373]/50",
						isSubmitting && "opacity-50 pointer-events-none",
					)}
				>
					<input
						type="file"
						onChange={handleFileSelect}
						disabled={isSubmitting}
						className="absolute inset-0 w-full h-full opacity-0 cursor-pointer disabled:cursor-not-allowed"
						accept="image/*,.pdf,.doc,.docx,.xls,.xlsx,.csv,.txt"
					/>
					<div className="flex items-center justify-center w-12 h-12 rounded-full bg-[#0F1217]">
						<FileIcon className="size-6 text-[#737373]" />
					</div>
					{selectedFile ? (
						<div className="text-center">
							<p className="text-white font-medium">{selectedFile.name}</p>
							<p className="text-[#737373] text-sm">
								{(selectedFile.size / 1024 / 1024).toFixed(2)} MB
							</p>
						</div>
					) : (
						<div className="text-center">
							<p className="text-white">
								<span className="text-[#4BA0FA]">Click to upload</span> or drag
								and drop
							</p>
						</div>
					)}
				</label>
			</div>
			<div className="flex flex-col gap-2">
				<p className="text-[14px] font-semibold pl-2">Title (optional)</p>
				<input
					type="text"
					value={title}
					onChange={(e) => handleTitleChange(e.target.value)}
					placeholder="Give this file a title"
					disabled={isSubmitting}
					className="w-full p-4 rounded-[14px] bg-[#14161A] shadow-inside-out disabled:opacity-50"
				/>
			</div>
			<div className="flex flex-col gap-2">
				<p className="text-[14px] font-semibold pl-2">Description (optional)</p>
				<textarea
					value={description}
					onChange={(e) => handleDescriptionChange(e.target.value)}
					placeholder="Add notes or context about this file"
					disabled={isSubmitting}
					className="w-full p-4 rounded-[14px] bg-[#14161A] shadow-inside-out disabled:opacity-50"
				/>
			</div>
		</div>
	)
}