|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect, useRef } from "react"; |
| 4 | +import Modal from "./shared/Modal"; |
| 5 | +import Button from "./shared/Button"; |
| 6 | +import { getDefaultSession } from "@inrupt/solid-client-authn-browser"; |
| 7 | +import { createContainerAt, getSolidDataset, UrlString } from "@inrupt/solid-client"; |
| 8 | +import toast from "react-hot-toast"; |
| 9 | + |
| 10 | +interface NewFolderDialogProps { |
| 11 | + isOpen: boolean; |
| 12 | + onClose: () => void; |
| 13 | + currentContainerUrl: string | null; |
| 14 | + onFolderCreated?: () => void; |
| 15 | +} |
| 16 | + |
| 17 | +export default function NewFolderDialog({ |
| 18 | + isOpen, |
| 19 | + onClose, |
| 20 | + currentContainerUrl, |
| 21 | + onFolderCreated, |
| 22 | +}: NewFolderDialogProps) { |
| 23 | + const [folderName, setFolderName] = useState("Untitled folder"); |
| 24 | + const [isCreating, setIsCreating] = useState(false); |
| 25 | + const inputRef = useRef<HTMLInputElement>(null); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + if (isOpen) { |
| 29 | + setFolderName("Untitled folder"); |
| 30 | + setIsCreating(false); |
| 31 | + // Focus and select the input text when modal opens |
| 32 | + setTimeout(() => { |
| 33 | + if (inputRef.current) { |
| 34 | + inputRef.current.focus(); |
| 35 | + inputRef.current.select(); |
| 36 | + } |
| 37 | + }, 100); |
| 38 | + } |
| 39 | + }, [isOpen]); |
| 40 | + |
| 41 | + const handleCreate = async () => { |
| 42 | + if (!folderName.trim()) { |
| 43 | + toast.error("Please enter a folder name"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (!currentContainerUrl) { |
| 48 | + toast.error("Please select a storage first"); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + setIsCreating(true); |
| 53 | + |
| 54 | + try { |
| 55 | + const session = getDefaultSession(); |
| 56 | + if (!session.info.isLoggedIn) { |
| 57 | + throw new Error("Not authenticated"); |
| 58 | + } |
| 59 | + |
| 60 | + const fetchFn = session.fetch || fetch; |
| 61 | + |
| 62 | + // Ensure the current container exists |
| 63 | + await getSolidDataset(currentContainerUrl, { fetch: fetchFn }); |
| 64 | + |
| 65 | + // Create the new folder URL |
| 66 | + const sanitizedName = folderName.trim().replace(/[<>:"/\\|?*]/g, ""); |
| 67 | + const newFolderUrl = currentContainerUrl.endsWith("/") |
| 68 | + ? `${currentContainerUrl}${sanitizedName}/` |
| 69 | + : `${currentContainerUrl}/${sanitizedName}/`; |
| 70 | + |
| 71 | + // Create the container |
| 72 | + await createContainerAt(newFolderUrl as UrlString, { fetch: fetchFn }); |
| 73 | + |
| 74 | + // Small delay to ensure server has processed the creation |
| 75 | + await new Promise(resolve => setTimeout(resolve, 200)); |
| 76 | + |
| 77 | + toast.success(`Folder "${sanitizedName}" created successfully`); |
| 78 | + |
| 79 | + // Notify parent to refresh before closing |
| 80 | + if (onFolderCreated) { |
| 81 | + onFolderCreated(); |
| 82 | + } |
| 83 | + |
| 84 | + onClose(); |
| 85 | + } catch (error) { |
| 86 | + console.error("Failed to create folder:", error); |
| 87 | + toast.error( |
| 88 | + error instanceof Error |
| 89 | + ? `Failed to create folder: ${error.message}` |
| 90 | + : "Failed to create folder" |
| 91 | + ); |
| 92 | + } finally { |
| 93 | + setIsCreating(false); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 98 | + if (e.key === "Enter" && !isCreating) { |
| 99 | + handleCreate(); |
| 100 | + } else if (e.key === "Escape") { |
| 101 | + onClose(); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + return ( |
| 106 | + <Modal |
| 107 | + isOpen={isOpen} |
| 108 | + onClose={onClose} |
| 109 | + title="New folder" |
| 110 | + maxWidth="sm" |
| 111 | + footer={ |
| 112 | + <div className="flex justify-end gap-2"> |
| 113 | + <Button |
| 114 | + variant="ghost" |
| 115 | + onClick={onClose} |
| 116 | + disabled={isCreating} |
| 117 | + > |
| 118 | + Cancel |
| 119 | + </Button> |
| 120 | + <Button |
| 121 | + variant="primary" |
| 122 | + onClick={handleCreate} |
| 123 | + isLoading={isCreating} |
| 124 | + disabled={isCreating || !folderName.trim()} |
| 125 | + > |
| 126 | + Create |
| 127 | + </Button> |
| 128 | + </div> |
| 129 | + } |
| 130 | + > |
| 131 | + <div className="py-2"> |
| 132 | + <input |
| 133 | + ref={inputRef} |
| 134 | + type="text" |
| 135 | + value={folderName} |
| 136 | + onChange={(e) => setFolderName(e.target.value)} |
| 137 | + onKeyDown={handleKeyDown} |
| 138 | + placeholder="Untitled folder" |
| 139 | + className="w-full h-10 rounded-md border border-gray-300 bg-white px-3 text-sm text-black placeholder:text-gray-500 focus:outline-none focus:ring-2 focus:ring-[#7B42F6] focus:border-[#7B42F6] transition-colors disabled:bg-gray-50 disabled:text-gray-500" |
| 140 | + disabled={isCreating} |
| 141 | + /> |
| 142 | + </div> |
| 143 | + </Modal> |
| 144 | + ); |
| 145 | +} |
| 146 | + |
0 commit comments