import { IconChevronDown, IconChevronRight, IconPlus, IconSearch, IconSettings, IconTrash, IconX, } from "@tabler/icons-react"; import CodeEditor from "@uiw/react-textarea-code-editor"; import cn from "classnames"; import { useFormikContext } from "formik"; import { useRef, useState } from "react"; import type { ProxyLocation } from "src/api/backend"; import { intl, T } from "src/locale"; import styles from "./LocationsFields.module.css"; // Below this many locations the list is short enough to scan by eye, and the // filter would only take up space. const FILTER_THRESHOLD = 5; // Locations are identified by a client-side id rather than their array index, // so that expanded/advanced state stays with the right row when one is removed. interface Row { id: number; value: ProxyLocation; } interface Props { initialValues: ProxyLocation[]; name?: string; } export function LocationsFields({ initialValues, name = "locations" }: Props) { const [rows, setRows] = useState(() => (initialValues || []).map((value, id) => ({ id, value }))); const { setFieldValue } = useFormikContext(); const [expanded, setExpanded] = useState([]); const [advVisible, setAdvVisible] = useState([]); const [filter, setFilter] = useState(""); const nextId = useRef(rows.length); const scrollToId = useRef(null); const blankItem: ProxyLocation = { path: "", advancedConfig: "", forwardScheme: "http", forwardHost: "", forwardPort: 80, }; const toggleExpanded = (id: number) => { setExpanded(expanded.includes(id) ? expanded.filter((i) => i !== id) : [...expanded, id]); }; const toggleAdvVisible = (id: number) => { setAdvVisible(advVisible.includes(id) ? advVisible.filter((i) => i !== id) : [...advVisible, id]); }; const handleAdd = () => { const id = nextId.current++; setRows([...rows, { id, value: blankItem }]); // A new location starts empty, so open it and make sure an active filter // doesn't hide the row that was just added. setExpanded([...expanded, id]); setFilter(""); scrollToId.current = id; }; const handleRemove = (id: number) => { const newRows = rows.filter((r: Row) => r.id !== id); setRows(newRows); setExpanded(expanded.filter((i) => i !== id)); setAdvVisible(advVisible.filter((i) => i !== id)); setFormField(newRows); }; const handleChange = (id: number, field: string, fieldValue: string) => { const newRows = rows.map((r: Row) => (r.id === id ? { ...r, value: { ...r.value, [field]: fieldValue } } : r)); setRows(newRows); setFormField(newRows); }; const setFormField = (newRows: Row[]) => { const filtered = newRows.map((r: Row) => r.value).filter((v: ProxyLocation) => v?.path?.trim() !== ""); setFieldValue(name, filtered); }; const forwardSummary = (item: ProxyLocation) => { if (!item.forwardHost) { return ""; } return `${item.forwardScheme}://${item.forwardHost}${item.forwardPort ? `:${item.forwardPort}` : ""}`; }; // Matches the path as well as the destination, so a location can be found by // the host or port it forwards to and not just by its path. const matchesFilter = (item: ProxyLocation, query: string) => [item.path, item.forwardScheme, item.forwardHost, item.forwardPort, forwardSummary(item)] .join(" ") .toLowerCase() .includes(query); const query = filter.trim().toLowerCase(); const visibleRows = query ? rows.filter((r: Row) => matchesFilter(r.value, query)) : rows; if (rows.length === 0) { return (
); } return ( <>
{rows.length >= FILTER_THRESHOLD && (
setFilter(e.target.value)} /> {filter ? ( ) : null}
)}
{visibleRows.length === 0 ? (
) : ( visibleRows.map((row: Row) => { const item = row.value; const isOpen = expanded.includes(row.id); const bodyId = `location-body-${row.id}`; return (
{ if (node && scrollToId.current === row.id) { scrollToId.current = null; node.scrollIntoView({ block: "nearest" }); } }} className={cn("card", "card-active", "mb-2", styles.locationCard)} >
{item.advancedConfig ? ( // Deliberately the same icon as the advanced-config toggle in the // body below, so the marker reads as "this row has that section // filled in" rather than as a decoration of its own. ) : null}
{isOpen && (
Location handleChange(row.id, "path", e.target.value)} />
handleChange(row.id, "forwardHost", e.target.value) } />
handleChange(row.id, "forwardPort", e.target.value) } />
{advVisible.includes(row.id) && (
handleChange(row.id, "advancedConfig", e.target.value)} style={{ fontFamily: "ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace", borderRadius: "0.3rem", minHeight: "170px", }} />
)}
)}
); }) )} ); }