import { platform } from "@yaakapp-internal/platform"; import { Icon, LoadingIcon } from "@yaakapp-internal/ui"; import classNames from "classnames"; import { type KeyboardEvent, type ReactNode, useEffect, useRef, useState } from "react"; import { Button } from "./core/Button"; import { IconButton } from "./core/IconButton"; import { PathList, splitPath } from "./core/PathList"; import { PlainInput } from "./core/PlainInput"; export interface ImportSourcePath { path: string; kind?: "file" | "folder" | "url"; } export type ImportSourceDetection = | { status: "loading" } | { status: "ok"; importer: string } | { status: "error"; message: string }; interface Props { sources: ImportSourcePath[]; detections: Record; disabled?: boolean; onAdd: (sources: ImportSourcePath[]) => void; onRemove: (path: string) => void; onError: (err: unknown) => void; } /** A bare hostname is a URL; local paths need an absolute or explicit relative prefix. */ export function isImportFilePath(value: string): boolean { return ( value.startsWith("/") || value.startsWith("./") || value.startsWith("../") || value.startsWith("~/") || value.startsWith("\\\\") || /^[a-zA-Z]:[\\/]/.test(value) ); } /** A typed or prefilled path is a URL unless it looks like a local path; kind is left for the host */ export function toSourcePath(path: string): ImportSourcePath { return { path, kind: isImportFilePath(path) ? undefined : "url" }; } export function ImportSourceList({ sources, detections, disabled, onAdd, onRemove, onError, }: Props) { const ref = useRef(null); const [isHovering, setIsHovering] = useState(false); const [pathInput, setPathInput] = useState(""); const [showPathInput, setShowPathInput] = useState(false); const pathInputRef = useRef<{ focus: () => void }>(null); const trimmedPath = pathInput.trim(); const openPathInput = () => { setShowPathInput(true); // The add-row menu hands focus back to its trigger as it closes, on a timer, so focus after it setTimeout(() => pathInputRef.current?.focus(), 50); }; const closePathInput = () => { setPathInput(""); setShowPathInput(false); }; const addPath = () => { if (!trimmedPath || disabled) return; onAdd([toSourcePath(trimmedPath)]); closePathInput(); }; useEffect(() => { if (disabled) return; return platform.window.onDragDrop((event) => { if (event.type === "leave") { setIsHovering(false); return; } const rect = ref.current?.getBoundingClientRect(); const { x, y } = event.position; const isOver = rect != null && x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom; if (event.type === "drop") { if (isOver) onAdd(event.paths.map((path) => ({ path }))); setIsHovering(false); } else { setIsHovering(isOver); } }); }, [disabled, onAdd]); const pick = async (directory: boolean) => { try { const selected = await platform.dialog.open({ title: directory ? "Select Import Folder" : "Select Import Files", multiple: true, directory, }); if (selected == null) return; onAdd(selected.map((path) => ({ path, kind: directory ? "folder" : "file" }))); } catch (err) { onError(err); } }; const handlePathKeyDown = (event: KeyboardEvent) => { if (event.key === "Enter") { event.preventDefault(); addPath(); } else if (event.key === "Escape") { event.stopPropagation(); closePathInput(); } }; const pathInputEl = ( {trimmedPath !== "" && ( )} } /> ); if (sources.length === 0) { return (
{isHovering ? ( Drop to add ) : ( Choose{" "} pick(false)}> files , a{" "} pick(true)}> folder , or a{" "} URL , or drag them here )} Supports OpenAPI, Swagger, Postman, Insomnia, Bruno, curl, and Yaak exports
{showPathInput && pathInputEl}
); } return ( pick(false) }, { label: "Folder", icon: "folder", onSelect: () => pick(true) }, { label: "URL or file path", icon: "globe", onSelect: openPathInput }, ]} items={sources.map((source) => { const detection = detections[source.path]; return { path: source.path, icon: sourceIcon(source), failed: detection?.status === "error", subtitle: , }; })} > {showPathInput && pathInputEl} ); } function DetectionSubtitle({ path, detection, }: { path: string; detection: ImportSourceDetection | undefined; }) { const { parent } = splitPath(path); if (detection?.status === "loading") { return ( <> Detecting format ); } if (detection?.status === "error") return detection.message; return ( <> {detection?.status === "ok" && {detection.importer}} {detection?.status === "ok" && parent !== "" && ยท} {parent !== "" && {parent}} ); } function sourceIcon({ kind }: ImportSourcePath) { switch (kind) { case "url": return "globe"; case "folder": return "folder"; case "file": return "file"; case undefined: return "import"; } } export function InlineButton({ children, disabled, onClick, }: { children: ReactNode; disabled?: boolean; onClick: () => void; }) { return ( ); }