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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
"use client"
import { useState, useEffect, useCallback } from "react"
import { Dialog, DialogContent, DialogTitle } from "@repo/ui/components/dialog"
import { cn } from "@lib/utils"
import { dmSansClassName } from "@/lib/fonts"
import { FileTextIcon, GlobeIcon, ZapIcon, Loader2 } from "lucide-react"
import { Button } from "@ui/components/button"
import { ConnectContent } from "./connections"
import { NoteContent } from "./note"
import { LinkContent, type LinkData } from "./link"
import { FileContent, type FileData } from "./file"
import { useProject } from "@/stores"
import { toast } from "sonner"
import { useDocumentMutations } from "../../../hooks/use-document-mutations"
import { useCustomer } from "autumn-js/react"
import { useMemoriesUsage } from "@/hooks/use-memories-usage"
import { SpaceSelector } from "../space-selector"
import { useIsMobile } from "@hooks/use-mobile"
type TabType = "note" | "link" | "file" | "connect"
interface AddDocumentModalProps {
isOpen: boolean
onClose: () => void
defaultTab?: TabType
}
export function AddDocumentModal({
isOpen,
onClose,
defaultTab,
}: AddDocumentModalProps) {
const isMobile = useIsMobile()
return (
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent
className={cn(
"border-none bg-[#1B1F24] flex flex-col p-3 md:p-4 gap-3",
isMobile
? "w-[calc(100vw-1rem)]! h-[calc(100dvh-1rem)]! max-w-none! max-h-none! rounded-xl"
: "w-[80%]! max-w-[1000px]! h-[80%]! max-h-[800px]! rounded-[22px]",
dmSansClassName(),
)}
style={{
boxShadow:
"0 2.842px 14.211px 0 rgba(0, 0, 0, 0.25), 0.711px 0.711px 0.711px 0 rgba(255, 255, 255, 0.10) inset",
}}
showCloseButton={false}
>
<DialogTitle className="sr-only">Add Document</DialogTitle>
<div className="flex-1 overflow-hidden">
<AddDocument
defaultTab={defaultTab}
onClose={onClose}
isOpen={isOpen}
/>
</div>
</DialogContent>
</Dialog>
)
}
const tabs = [
{
id: "note" as const,
icon: FileTextIcon,
title: "Write a note",
description: "Save your thoughts, notes and summaries, as memories",
},
{
id: "link" as const,
icon: GlobeIcon,
title: "Save a link",
description: "Add any webpage into your searchable knowledge base",
},
{
id: "file" as const,
icon: FileTextIcon,
title: "Upload a file",
description: "Turn any image, PDF or document into contextual memories",
},
{
id: "connect" as const,
icon: ZapIcon,
title: "Connect knowledge bases",
description: "Sync with Google Drive, Notion and OneDrive and import data",
isPro: true,
},
]
export function AddDocument({
defaultTab,
onClose,
isOpen,
}: {
defaultTab?: TabType
onClose: () => void
isOpen?: boolean
}) {
const isMobile = useIsMobile()
const [activeTab, setActiveTab] = useState<TabType>(defaultTab ?? "note")
const { selectedProject: globalSelectedProject } = useProject()
const [localSelectedProject, setLocalSelectedProject] = useState<string>(
globalSelectedProject,
)
// Form data state for button click handling
const [noteContent, setNoteContent] = useState("")
const [linkData, setLinkData] = useState<LinkData>({
url: "",
title: "",
description: "",
})
const [fileData, setFileData] = useState<FileData>({
file: null,
title: "",
description: "",
})
const { noteMutation, linkMutation, fileMutation } = useDocumentMutations({
onClose,
})
const autumn = useCustomer()
const {
memoriesUsed,
memoriesLimit,
hasProProduct,
isLoading: isLoadingMemories,
usagePercent,
} = useMemoriesUsage(autumn)
useEffect(() => {
setLocalSelectedProject(globalSelectedProject)
}, [globalSelectedProject])
useEffect(() => {
if (defaultTab) {
setActiveTab(defaultTab)
}
}, [defaultTab])
// Submit handlers
const handleNoteSubmit = useCallback(
(content: string) => {
if (!content.trim()) {
toast.error("Please enter some content")
return
}
noteMutation.mutate({ content, project: localSelectedProject })
},
[noteMutation, localSelectedProject],
)
const handleLinkSubmit = useCallback(
(data: LinkData) => {
if (!data.url.trim()) {
toast.error("Please enter a URL")
return
}
linkMutation.mutate({ url: data.url, project: localSelectedProject })
},
[linkMutation, localSelectedProject],
)
const handleFileSubmit = useCallback(
(data: { file: File; title: string; description: string }) => {
if (!data.file) {
toast.error("Please select a file")
return
}
fileMutation.mutate({
file: data.file,
title: data.title || undefined,
description: data.description || undefined,
project: localSelectedProject,
})
},
[fileMutation, localSelectedProject],
)
// Data change handlers
const handleNoteContentChange = useCallback((content: string) => {
setNoteContent(content)
}, [])
const handleLinkDataChange = useCallback((data: LinkData) => {
setLinkData(data)
}, [])
const handleFileDataChange = useCallback((data: FileData) => {
setFileData(data)
}, [])
// Button click handler
const handleButtonClick = () => {
if (activeTab === "note") {
handleNoteSubmit(noteContent)
} else if (activeTab === "link") {
handleLinkSubmit(linkData)
} else if (activeTab === "file") {
if (fileData.file) {
handleFileSubmit(
fileData as { file: File; title: string; description: string },
)
} else {
toast.error("Please select a file")
}
}
}
const isSubmitting =
noteMutation.isPending || linkMutation.isPending || fileMutation.isPending
return (
<div className="h-full flex flex-col md:flex-row text-white md:space-x-5 space-y-3 md:space-y-0">
<div
className={cn(
"flex flex-col justify-between",
isMobile ? "w-full" : "w-1/3",
)}
>
<div
className={cn(
"flex gap-1",
isMobile
? "flex-row overflow-x-auto pb-2 scrollbar-thin"
: "flex-col",
)}
>
{tabs.map((tab) => (
<TabButton
key={tab.id}
active={activeTab === tab.id}
onClick={() => setActiveTab(tab.id)}
icon={tab.icon}
title={tab.title}
description={tab.description}
isPro={tab.isPro}
compact={isMobile}
/>
))}
</div>
{!isMobile && (
<div
data-testid="memories-counter"
className="bg-[#1B1F24] rounded-2xl p-4 mr-4"
style={{
boxShadow:
"0 2.842px 14.211px 0 rgba(0, 0, 0, 0.25), 0.711px 0.711px 0.711px 0 rgba(255, 255, 255, 0.10) inset",
}}
>
<div className="flex justify-between items-center">
<span
className={cn(
"text-white text-[16px] font-medium",
dmSansClassName(),
)}
>
Memories
</span>
<span className={cn("text-[#737373] text-sm", dmSansClassName())}>
{isLoadingMemories
? "…"
: hasProProduct
? "Unlimited"
: `${memoriesUsed}/${memoriesLimit}`}
</span>
</div>
{!hasProProduct && (
<div className="h-1.5 bg-[#0D121A] rounded-full overflow-hidden mt-2">
<div
className="h-full bg-[#2261CA] rounded-full"
style={{ width: `${usagePercent}%` }}
/>
</div>
)}
</div>
)}
</div>
<div
className={cn(
"overflow-auto flex flex-col justify-between px-1 scrollbar-thin flex-1",
isMobile ? "w-full" : "w-2/3",
)}
>
{activeTab === "note" && (
<NoteContent
onSubmit={handleNoteSubmit}
onContentChange={handleNoteContentChange}
isSubmitting={noteMutation.isPending}
isOpen={isOpen}
/>
)}
{activeTab === "link" && (
<LinkContent
onSubmit={handleLinkSubmit}
onDataChange={handleLinkDataChange}
isSubmitting={linkMutation.isPending}
isOpen={isOpen}
/>
)}
{activeTab === "file" && (
<FileContent
onSubmit={handleFileSubmit}
onDataChange={handleFileDataChange}
isSubmitting={fileMutation.isPending}
isOpen={isOpen}
/>
)}
{activeTab === "connect" && (
<ConnectContent selectedProject={localSelectedProject} />
)}
<div
className={cn(
"flex gap-2 pt-3",
isMobile ? "flex-col" : "justify-between",
)}
>
{!isMobile && (
<SpaceSelector
value={localSelectedProject}
onValueChange={setLocalSelectedProject}
variant="insideOut"
/>
)}
<div
className={cn("flex items-center gap-2", isMobile && "justify-end")}
>
<Button
variant="ghost"
onClick={onClose}
disabled={isSubmitting}
className="text-[#737373] cursor-pointer rounded-full"
>
Cancel
</Button>
{activeTab !== "connect" && (
<Button
variant="insideOut"
onClick={handleButtonClick}
disabled={isSubmitting}
>
{isSubmitting ? (
<>
<Loader2 className="size-4 animate-spin mr-2" />
Adding...
</>
) : (
<>
+ Add {activeTab}{" "}
{!isMobile && (
<span
className={cn(
"bg-[#21212180] border border-[#73737333] text-[#737373] rounded-sm px-1 py-0.5 text-[10px] flex items-center justify-center",
dmSansClassName(),
)}
>
⌘+Enter
</span>
)}
</>
)}
</Button>
)}
</div>
</div>
</div>
</div>
)
}
function TabButton({
active,
onClick,
icon: Icon,
title,
description,
isPro,
compact,
}: {
active: boolean
onClick: () => void
icon: React.ComponentType<{ className?: string }>
title: string
description: string
isPro?: boolean
compact?: boolean
}) {
if (compact) {
return (
<button
type="button"
onClick={onClick}
className={cn(
"flex items-center gap-2 px-3 py-2 rounded-full text-left transition-colors whitespace-nowrap focus:outline-none focus:ring-0 shrink-0",
active ? "bg-[#14161A] shadow-inside-out" : "hover:bg-[#14161A]/50",
dmSansClassName(),
)}
>
<Icon className={cn("size-4 shrink-0 text-white")} />
<span
className={cn("font-medium text-white text-sm", dmSansClassName())}
>
{title.split(" ")[0]}
</span>
{isPro && (
<span className="bg-[#4BA0FA] text-black text-[8px] font-semibold px-1 py-0.5 rounded">
PRO
</span>
)}
</button>
)
}
return (
<button
type="button"
onClick={onClick}
className={cn(
"flex items-start gap-3 p-4 rounded-[16px] text-left transition-colors w-full focus:outline-none focus:ring-0",
active
? "bg-[#14161A] shadow-inside-out"
: "hover:bg-[#14161A]/50 hover:shadow-[inset_0_2px_4px_rgba(0,0,0,0.3),inset_0_1px_2px_rgba(0,0,0,0.1)]",
dmSansClassName(),
)}
>
<Icon className={cn("size-5 mt-0.5 shrink-0 text-white")} />
<div className="flex flex-col gap-0.5 text-[16px]">
<div className="flex items-center justify-between gap-2">
<span className={cn("font-medium text-white", dmSansClassName())}>
{title}
</span>
{isPro && (
<span className="bg-[#4BA0FA] text-black text-[10px] font-semibold px-1.5 py-0.5 rounded">
PRO
</span>
)}
</div>
<span className="text-[#737373]">{description}</span>
</div>
</button>
)
}
|