"use client"; import { useAtomValue } from "jotai"; import { Upload } from "lucide-react"; import { createContext, type FC, type ReactNode, useCallback, useContext, useRef, useState, } from "react"; import { activeSearchSpaceIdAtom } from "@/atoms/search-spaces/search-space-query.atoms"; import { DocumentUploadTab } from "@/components/sources/DocumentUploadTab"; import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog"; // Context for opening the dialog from anywhere interface DocumentUploadDialogContextType { openDialog: () => void; closeDialog: () => void; } const DocumentUploadDialogContext = createContext(null); export const useDocumentUploadDialog = () => { const context = useContext(DocumentUploadDialogContext); if (!context) { throw new Error("useDocumentUploadDialog must be used within DocumentUploadDialogProvider"); } return context; }; // Provider component export const DocumentUploadDialogProvider: FC<{ children: ReactNode }> = ({ children }) => { const [isOpen, setIsOpen] = useState(false); const isClosingRef = useRef(false); const openDialog = useCallback(() => { // Prevent opening if we just closed (debounce) if (isClosingRef.current) { return; } setIsOpen(true); }, []); const closeDialog = useCallback(() => { isClosingRef.current = true; setIsOpen(false); // Reset the flag after a short delay to allow for file picker to close setTimeout(() => { isClosingRef.current = false; }, 300); }, []); const handleOpenChange = useCallback( (open: boolean) => { if (!open) { // Only close if not already in closing state if (!isClosingRef.current) { closeDialog(); } } else { // Only open if not in the middle of closing if (!isClosingRef.current) { setIsOpen(true); } } }, [closeDialog] ); return ( {children} ); }; // Internal component that renders the dialog const DocumentUploadPopupContent: FC<{ isOpen: boolean; onOpenChange: (open: boolean) => void; }> = ({ isOpen, onOpenChange }) => { const searchSpaceId = useAtomValue(activeSearchSpaceIdAtom); if (!searchSpaceId) return null; const handleSuccess = () => { onOpenChange(false); }; return ( Upload Document {/* Scrollable container for mobile */}
{/* Header - scrolls with content on mobile */}
{/* Upload header */}

Upload Documents

Upload and sync your documents to your search space

{/* Content */}
{/* Bottom fade shadow - hidden on very small screens */}
); };