forked from creatorcluster/renderdragon.org
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFolderPickerPopover.tsx
More file actions
212 lines (189 loc) · 8.16 KB
/
FolderPickerPopover.tsx
File metadata and controls
212 lines (189 loc) · 8.16 KB
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
import React, { useState, useRef, useEffect } from 'react';
import { IconFolder, IconPlus, IconAlertTriangle } from '@tabler/icons-react';
import { cn } from '@/lib/utils';
import { FavoriteFolder } from '@/hooks/useFavoriteFolders';
import { MAX_FOLDER_ITEMS } from '@/hooks/useUserFavorites';
import { ScrollArea } from '@/components/ui/scroll-area';
import FolderDialog from './FolderDialog';
import { useFavoriteFolders } from '@/hooks/useFavoriteFolders';
interface FolderPickerPopoverProps {
isOpen: boolean;
onClose: () => void;
/** Called when user picks a folder (null = no folder / unsorted) */
onSelectFolder: (folderId: string | null) => void;
folders: FavoriteFolder[];
/** Returns how many items are in a folder */
getFolderItemCount: (folderId: string | null) => number;
/** Position anchor element */
anchorRef: React.RefObject<HTMLElement | null>;
}
const FolderPickerPopover = ({
isOpen,
onClose,
onSelectFolder,
folders,
getFolderItemCount,
anchorRef,
}: FolderPickerPopoverProps) => {
const popoverRef = useRef<HTMLDivElement>(null);
const [position, setPosition] = useState({ top: 0, left: 0 });
const [showCreateDialog, setShowCreateDialog] = useState(false);
const { createFolder } = useFavoriteFolders();
// Position the popover relative to the anchor
useEffect(() => {
if (!isOpen || !anchorRef.current) return;
const rect = anchorRef.current.getBoundingClientRect();
const popoverWidth = 220;
const popoverHeight = 300;
let top = rect.bottom + 8;
let left = rect.left + rect.width / 2 - popoverWidth / 2;
// Keep within viewport
if (left < 8) left = 8;
if (left + popoverWidth > window.innerWidth - 8) {
left = window.innerWidth - popoverWidth - 8;
}
if (top + popoverHeight > window.innerHeight - 8) {
top = rect.top - popoverHeight - 8;
}
setPosition({ top, left });
}, [isOpen, anchorRef]);
// Close on outside click
useEffect(() => {
if (!isOpen) return;
const handleClickOutside = (e: MouseEvent) => {
if (
popoverRef.current &&
!popoverRef.current.contains(e.target as Node) &&
anchorRef.current &&
!anchorRef.current.contains(e.target as Node)
) {
onClose();
}
};
// Delay to avoid the current click event closing it
const timer = setTimeout(() => {
document.addEventListener('mousedown', handleClickOutside);
}, 0);
return () => {
clearTimeout(timer);
document.removeEventListener('mousedown', handleClickOutside);
};
}, [isOpen, onClose, anchorRef]);
// Close on Escape
useEffect(() => {
if (!isOpen) return;
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape') onClose();
};
document.addEventListener('keydown', handleEscape);
return () => document.removeEventListener('keydown', handleEscape);
}, [isOpen, onClose]);
if (!isOpen) return null;
const rootFolders = folders.filter(f => !f.parent_id);
const handleFolderClick = (folderId: string | null) => {
if (folderId) {
const count = getFolderItemCount(folderId);
if (count >= MAX_FOLDER_ITEMS) {
// Don't close, just don't select
return;
}
}
onSelectFolder(folderId);
onClose();
};
const handleCreateFolder = async (name: string, color: string | null) => {
try {
const newFolder = await createFolder(name, null, color);
setShowCreateDialog(false);
if (newFolder?.id) {
onSelectFolder(String(newFolder.id));
onClose();
}
} catch {
setShowCreateDialog(false);
}
};
return (
<>
<div
ref={popoverRef}
className="fixed z-[100] bg-card border border-border/60 rounded-xl shadow-2xl backdrop-blur-xl animate-in fade-in-0 zoom-in-95 duration-150"
style={{
top: position.top,
left: position.left,
width: 220,
maxHeight: 340,
}}
>
<div className="px-3 py-2.5 border-b border-border/40">
<p className="text-xs font-medium text-muted-foreground uppercase tracking-wide">
Save to folder
</p>
</div>
<ScrollArea className="max-h-[230px]">
<div className="p-1.5 space-y-0.5">
{/* No folder / unsorted option */}
<button
onClick={() => handleFolderClick(null)}
className="w-full flex items-center gap-2 px-2.5 py-2 rounded-lg text-sm text-left hover:bg-muted/50 transition-colors"
>
<IconFolder size={15} className="text-muted-foreground flex-shrink-0" />
<span className="truncate text-muted-foreground">No folder</span>
</button>
{/* Folder list */}
{rootFolders.map(folder => {
const count = getFolderItemCount(folder.id);
const isFull = count >= MAX_FOLDER_ITEMS;
return (
<button
key={folder.id}
onClick={() => handleFolderClick(folder.id)}
disabled={isFull}
className={cn(
"w-full flex items-center gap-2 px-2.5 py-2 rounded-lg text-sm text-left transition-colors",
isFull
? "opacity-50 cursor-not-allowed"
: "hover:bg-muted/50 cursor-pointer"
)}
>
<div
className="w-3 h-3 rounded-full flex-shrink-0"
style={{ backgroundColor: folder.color || 'var(--primary)' }}
/>
<span className="truncate flex-1">{folder.name}</span>
<span className={cn(
"text-[10px] font-mono flex-shrink-0",
isFull ? "text-amber-500" : "text-muted-foreground/60"
)}>
{count}/{MAX_FOLDER_ITEMS}
</span>
{isFull && (
<IconAlertTriangle size={12} className="text-amber-500 flex-shrink-0" />
)}
</button>
);
})}
</div>
</ScrollArea>
{/* Create folder */}
<div className="border-t border-border/40 p-1.5">
<button
onClick={() => setShowCreateDialog(true)}
className="w-full flex items-center gap-2 px-2.5 py-2 rounded-lg text-sm hover:bg-primary/10 text-primary transition-colors"
>
<IconPlus size={15} className="flex-shrink-0" />
<span>Create Folder</span>
</button>
</div>
</div>
<FolderDialog
isOpen={showCreateDialog}
onClose={() => setShowCreateDialog(false)}
onSave={handleCreateFolder}
initialData={null}
mode="create"
/>
</>
);
};
export default FolderPickerPopover;