diff --git a/src/features/http-client/endpoints.ts b/src/features/http-client/endpoints.ts index 9a5653517c..247a4d907e 100644 --- a/src/features/http-client/endpoints.ts +++ b/src/features/http-client/endpoints.ts @@ -32,13 +32,18 @@ const DEFAULT_HOSTS: Record = { Symfony: "http://127.0.0.1:8000", }; +/** The endpoint as an HTTP Client request block, with the development server's address resolved. */ +export async function endpointRequest(endpoint: EndpointEntry): Promise { + // The server address of the endpoint's module (`server.port`, context path), as for Spring mappings. + const host = + (!endpoint.pluginId && DEFAULT_HOSTS[endpoint.framework ?? ""]) || + (await findHost(endpoint.path, useProject.getState().project?.root ?? null, sourceReader)); + return renderEndpoint(endpoint, host); +} + export async function openEndpoint(endpoint: EndpointEntry): Promise { try { - // The server address of the endpoint's module (`server.port`, context path), as for Spring mappings. - const host = - (!endpoint.pluginId && DEFAULT_HOSTS[endpoint.framework ?? ""]) || - (await findHost(endpoint.path, useProject.getState().project?.root ?? null, sourceReader)); - await appendGeneratedRequests([renderEndpoint(endpoint, host)]); + await appendGeneratedRequests([await endpointRequest(endpoint)]); } catch (e) { notifications().notify({ level: "error", title: tx("Cannot generate the HTTP request"), body: String(e) }); } diff --git a/src/features/http-client/endpointsWindow/EndpointsView.module.css b/src/features/http-client/endpointsWindow/EndpointsView.module.css new file mode 100644 index 0000000000..d73525b3ff --- /dev/null +++ b/src/features/http-client/endpointsWindow/EndpointsView.module.css @@ -0,0 +1,272 @@ +.view { + height: 100%; + display: flex; + flex-direction: column; + min-height: 0; +} +.body { + flex: 1; + min-height: 0; +} +.body > * { + height: 100%; +} +.panel { + min-height: 0; + display: flex; + flex-direction: column; +} +.panel > * { + flex: 1; + min-height: 0; +} +.spacer { + flex: 1; +} + +/* Module / Type / Framework filters and details toggle. */ +.filterBar { + flex: none; + display: flex; + flex-wrap: wrap; + align-items: center; + gap: var(--space-0-5); + min-height: 32px; + padding: 4px 6px 4px 8px; + border-bottom: 1px solid var(--border); +} +.filterButton { + display: inline-flex; + align-items: center; + gap: var(--space-0-5); + height: 24px; + padding: 0 4px 0 6px; + border: none; + border-radius: var(--radius-small); + background: transparent; + color: var(--fg-secondary); + font: inherit; + white-space: nowrap; + cursor: pointer; +} +.filterButton:hover:not(:disabled) { + background: var(--bg-hover); +} +.filterButton:disabled { + opacity: 0.5; + cursor: default; +} +.filterActive { + color: var(--accent, var(--info)); +} +.filterChevron { + color: var(--fg-muted); +} + +/* Search field. */ +.searchBar { + flex: none; + display: flex; + align-items: center; + gap: var(--space-1-5); + height: 30px; + padding: 0 8px; + border-bottom: 1px solid var(--border); +} +.searchIcon { + flex: none; + color: var(--fg-muted); +} +.search { + flex: 1; + min-width: 0; +} + +/* Module / file rows. */ +.groupRow { + display: flex; + align-items: center; + gap: var(--space-1-5); + min-width: 0; + white-space: nowrap; +} +.groupIcon { + flex: none; + color: var(--fg-muted); +} +.groupName { + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + color: var(--fg-emphasis); +} +.count { + flex: none; + color: var(--fg-subtle); + font-size: var(--fs-small); + margin-left: 4px; +} + +/* `/photos/:id [GET]` … PhotosController */ +.endpoint { + display: flex; + align-items: center; + gap: var(--space-1-5); + flex: 1; + min-width: 0; + white-space: nowrap; +} +.endpointIcon { + flex: none; + color: var(--fg-muted); +} +.path { + flex: 0 1 auto; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; +} +.verb { + flex: none; + font-family: var(--font-mono); + font-size: var(--fs-small); + font-weight: 600; +} +.get { + color: var(--info); +} +.post { + color: var(--success, var(--info)); +} +.put { + color: var(--warning); +} +.delete { + color: var(--danger); +} +.controller { + flex: 0 1 auto; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + color: var(--fg-muted); +} + +/* Details pane. */ +.details { + height: 100%; + display: flex; + flex-direction: column; + min-height: 0; + border-top: 1px solid var(--border); +} +.detailTabs { + flex: none; + border: 0; + background: none; + border-bottom: 1px solid var(--border); +} +.detailEmpty { + flex: 1; + display: flex; + align-items: center; + justify-content: center; + color: var(--fg-muted); +} +.httpTab { + flex: 1; + min-height: 0; + display: flex; + flex-direction: column; +} +.request { + flex: 1; + min-height: 0; + overflow: auto; + padding: 6px 0; + font-family: var(--font-mono); + font-size: var(--fs-code, var(--fs-ui)); + background: var(--bg-editor); +} +.requestLine { + display: flex; + align-items: center; + min-height: 22px; + white-space: pre; +} +.requestMain { + background: var(--bg-tree-hover, color-mix(in srgb, var(--fg-strong) 4%, transparent)); +} +.requestGutter { + flex: none; + width: 34px; + display: flex; + justify-content: center; +} +.requestText { + flex: 1; + min-width: 0; + padding-right: 8px; + color: var(--fg-muted); +} +.requestMain .requestText { + color: var(--fg); +} +.detailButtons { + flex: none; + display: flex; + flex-wrap: wrap; + gap: var(--space-2); + padding: 8px; + border-top: 1px solid var(--border); +} + +/* Documentation tab. */ +.docs { + flex: 1; + min-height: 0; + overflow: auto; + padding: 8px 12px; +} +.docList { + margin: 0; + display: grid; + grid-template-columns: max-content 1fr; + gap: var(--space-1) var(--space-4); +} +.docRow { + display: contents; +} +.docRow dt { + color: var(--fg-muted); +} +.docRow dd { + margin: 0; + font-family: var(--font-mono); + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.link { + display: inline-block; + max-width: 100%; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + vertical-align: bottom; + padding: 0; + border: none; + background: none; + color: var(--link, var(--info)); + font: inherit; + cursor: pointer; + text-decoration: underline; +} + +.loading { + display: flex; + align-items: center; + gap: var(--space-1-5); + padding: 12px; + color: var(--fg-muted); +} diff --git a/src/features/http-client/endpointsWindow/EndpointsView.tsx b/src/features/http-client/endpointsWindow/EndpointsView.tsx new file mode 100644 index 0000000000..aca0c376c2 --- /dev/null +++ b/src/features/http-client/endpointsWindow/EndpointsView.tsx @@ -0,0 +1,572 @@ +/** + * Endpoints tool window: Module / Type / Framework filters, search, endpoints + * grouped by module, and details with the generated HTTP Client request. + */ +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; +import { create } from "zustand"; +import clsx from "clsx"; +import { tx } from "@/platform/i18n"; +import { basename, useProject } from "@/platform/project"; +import { editorService, notifications, useOccurrenceNavigator } from "@/workbench/contributions"; +import { + Button, + EmptyState, + Icon, + IconButton, + Input, + Menu, + Spinner, + SplitGroup, + SplitHandle, + SplitPanel, + Tabs, + Tree, + T, + type MenuItem, + type TreeHandle, + type TreeRowState, +} from "@/ui"; +import { endpointRequest, renderEndpoint } from "../endpoints"; +import { appendGeneratedRequests } from "../generatedRequests"; +import { runRequest } from "../store"; +import { + distinct, + filterEndpoints, + groupBy, + navigationTarget, + relativeTo, + type EndpointGroup, + type EndpointRow, +} from "./model"; +import { refreshEndpoints, useEndpoints } from "./store"; +import styles from "./EndpointsView.module.css"; + +export const ENDPOINTS_WINDOW = "endpoints"; +const OPTIONS_KEY = "openidea.endpoints.view.v1"; + +interface ViewOptions { + groupByFile: boolean; + autoscroll: boolean; + details: boolean; +} + +function loadOptions(): Partial { + try { + return JSON.parse(globalThis.localStorage?.getItem(OPTIONS_KEY) ?? "{}") as Partial; + } catch { + return {}; + } +} + +export const useEndpointsViewOptions = create(() => ({ + groupByFile: false, + autoscroll: false, + details: true, + ...loadOptions(), +})); + +useEndpointsViewOptions.subscribe((s) => { + try { + globalThis.localStorage?.setItem(OPTIONS_KEY, JSON.stringify(s)); + } catch { + /* storage unavailable */ + } +}); + +type DetailsTab = "http" | "docs"; + +interface ViewState { + query: string; + hiddenModules: Set; + hiddenTypes: Set; + hiddenFrameworks: Set; + collapsed: Set; + tab: DetailsTab; + currentId: string | null; +} + +export const useEndpointsView = create(() => ({ + query: "", + hiddenModules: new Set(), + hiddenTypes: new Set(), + hiddenFrameworks: new Set(), + collapsed: new Set(), + tab: "http", + currentId: null, +})); + +export function resetEndpointsView(): void { + useEndpointsView.setState({ + query: "", + hiddenModules: new Set(), + hiddenTypes: new Set(), + hiddenFrameworks: new Set(), + collapsed: new Set(), + currentId: null, + }); +} + +export function useEndpointsHeaderMenu(): MenuItem[] { + const o = useEndpointsViewOptions(); + const toggle = (key: keyof ViewOptions) => () => useEndpointsViewOptions.setState({ [key]: !o[key] }); + return [ + { id: "group", label: "Group by File", checked: o.groupByFile, run: toggle("groupByFile") }, + { id: "details", label: "Show Details", checked: o.details, run: toggle("details") }, + { id: "autoscroll", label: "Autoscroll to Source", checked: o.autoscroll, run: toggle("autoscroll") }, + ]; +} + +type Node = + | { kind: "module"; key: string; name: string; endpoints: EndpointRow[] } + | { kind: "file"; key: string; group: EndpointGroup } + | { kind: "endpoint"; key: string; endpoint: EndpointRow }; + +export function openEndpointSource(e: EndpointRow, focus: boolean) { + const { path, line, col } = navigationTarget(e); + void editorService().openFile(path, { selection: { line, col }, focus, preview: !focus }); +} + +export function useVisibleEndpoints(): EndpointRow[] { + const endpoints = useEndpoints((s) => s.endpoints); + const query = useEndpointsView((s) => s.query); + const hiddenModules = useEndpointsView((s) => s.hiddenModules); + const hiddenTypes = useEndpointsView((s) => s.hiddenTypes); + const hiddenFrameworks = useEndpointsView((s) => s.hiddenFrameworks); + return useMemo( + () => + filterEndpoints( + endpoints.filter( + (e) => !hiddenModules.has(e.module) && !hiddenTypes.has(e.type) && !hiddenFrameworks.has(e.framework), + ), + query, + ), + [endpoints, query, hiddenModules, hiddenTypes, hiddenFrameworks], + ); +} + +export function EndpointsHeaderActions() { + const loading = useEndpoints((s) => s.loading); + return ( + void refreshEndpoints()} + /> + ); +} + +function FilterMenu({ + label, + entries, + hidden, + onToggle, +}: { + label: string; + entries: string[]; + hidden: ReadonlySet; + onToggle: (entry: string) => void; +}) { + const [anchor, setAnchor] = useState(null); + const active = entries.some((e) => hidden.has(e)); + const items: MenuItem[] = entries.map((e) => ({ + id: e, + label: e, + verbatim: true, + checked: !hidden.has(e), + run: () => onToggle(e), + })); + return ( + <> + + setAnchor(null)} aria-label={label} /> + + ); +} + +const toggleIn = (set: Set, entry: string): Set => { + const next = new Set(set); + if (!next.delete(entry)) next.add(entry); + return next; +}; + +function FilterBar() { + const endpoints = useEndpoints((s) => s.endpoints); + const project = useProject((s) => s.project?.name ?? ""); + const { hiddenModules, hiddenTypes, hiddenFrameworks } = useEndpointsView(); + const details = useEndpointsViewOptions((o) => o.details); + const modules = distinct([project, ...endpoints.map((e) => e.module)].filter(Boolean)); + const types = distinct(endpoints.map((e) => e.type)); + const frameworks = distinct(endpoints.map((e) => e.framework)); + const filter = ( + label: string, + entries: string[], + hidden: ReadonlySet, + key: "hiddenModules" | "hiddenTypes" | "hiddenFrameworks", + ) => ( +