Skip to content

Commit

Permalink
feat(console): improve endpoints UI (#6779)
Browse files Browse the repository at this point in the history
Resolves #6796
  • Loading branch information
polamoros authored Jul 1, 2024
1 parent 7fe1e21 commit 90b70db
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 50 deletions.
2 changes: 1 addition & 1 deletion apps/wing-console/console/design-system/src/tree-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const TreeItem = memo(

{icon && <div className="shrink-0">{icon}</div>}

<div className="grow flex items-center gap-1 justify-around min-w-0">
<div className="grow flex items-center gap-1.5 justify-around min-w-0">
<span className={classNames(theme.text1, "grow truncate")}>
{typeof label === "function" ? label() : label}
</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {
GlobeAltIcon,
LinkIcon,
ShareIcon,
EyeSlashIcon,
ArrowTopRightOnSquareIcon,
} from "@heroicons/react/24/outline";
import {
Button,
ScrollableArea,
SpinnerLoader,
Toolbar,
TreeItem,
TreeView,
useTheme,
} from "@wingconsole/design-system";
import classNames from "classnames";
import { useCallback, useEffect, useState } from "react";

import type { EndpointItem } from "./endpoint-item.js";
import { NoEndpoints } from "./no-endpoints.js";
Expand All @@ -22,13 +23,65 @@ export interface EndpointTreeProps {
hideEndpoint: (resourcePath: string) => void;
}

const getEndpointTitle = (exposeStatus: EndpointItem["exposeStatus"]) => {
switch (exposeStatus) {
case "disconnected": {
return "Endpoint is not exposed";
}
case "connecting": {
return "Connecting";
}
case "connected": {
return "Endpoint is exposed";
}
}
};

export const EndpointTree = ({
endpointList,
exposeEndpoint,
hideEndpoint,
}: EndpointTreeProps) => {
const { theme } = useTheme();

// list or map of endpoint ids that are currently being hidden
const [hidingEndpoint, setHidingEndpoint] = useState<string[]>();

const onHideEndpoint = useCallback(
(endpointId: string) => {
setHidingEndpoint((previousHidingEndpoint) => {
return previousHidingEndpoint?.includes(endpointId)
? previousHidingEndpoint.filter((id) => id !== endpointId)
: [...(previousHidingEndpoint || []), endpointId];
});
hideEndpoint(endpointId);
},
[hideEndpoint],
);

useEffect(() => {
const isHidingEndpointDisconnected =
hidingEndpoint &&
endpointList.find(
(endpoint) =>
endpoint.exposeStatus === "disconnected" &&
hidingEndpoint.includes(endpoint.id),
);
if (isHidingEndpointDisconnected) {
setHidingEndpoint(undefined);
}
}, [endpointList, hidingEndpoint]);

const isLoading = useCallback(
(endpoint: EndpointItem) => {
return (
endpoint.exposeStatus === "connecting" ||
hidingEndpoint?.includes(endpoint.id)
);
},
[hidingEndpoint],
);

return (
<div
className={classNames("w-full h-full flex flex-col", theme.bg3)}
Expand All @@ -54,61 +107,94 @@ export const EndpointTree = ({
key={endpoint.id}
itemId={endpoint.id}
selectable={false}
title={getEndpointTitle(endpoint.exposeStatus)}
icon={
<>
{isLoading(endpoint) && <SpinnerLoader size="xs" />}
{!isLoading(endpoint) && (
<>
{endpoint.exposeStatus === "disconnected" && (
<div
className={classNames("size-4", theme.text2)}
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth="1.5"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 21a9.004 9.004 0 0 0 8.716-6.747M12 21a9.004 9.004 0 0 1-8.716-6.747M12 21c2.485 0 4.5-4.03 4.5-9S14.485 3 12 3m0 18c-2.485 0-4.5-4.03-4.5-9S9.515 3 12 3m0 0a8.997 8.997 0 0 1 7.843 4.582M12 3a8.997 8.997 0 0 0-7.843 4.582m15.686 0A11.953 11.953 0 0 1 12 10.5c-2.998 0-5.74-1.1-7.843-2.918m15.686 0A8.959 8.959 0 0 1 21 12c0 .778-.099 1.533-.284 2.253m0 0A17.919 17.919 0 0 1 12 16.5c-3.162 0-6.133-.815-8.716-2.247m0 0A9.015 9.015 0 0 1 3 12c0-1.605.42-3.113 1.157-4.418"
/>

<path
strokeLinecap="round"
strokeLinejoin="round"
d="M3 3l18 18"
/>
</svg>
</div>
)}

{endpoint.exposeStatus === "connected" && (
<GlobeAltIcon
className={classNames("size-4", theme.text1)}
/>
)}
</>
)}
</>
}
label={
<a
href={endpoint.url}
target="_blank"
rel="noreferrer"
className="hover:underline text-sky-500 hover:text-sky-600"
title={endpoint.url}
aria-disabled={isLoading(endpoint)}
className={classNames(
"flex gap-1 items-center",
"justify-between group/endpoint-tree-item",
!isLoading(endpoint) &&
"hover:underline text-sky-500 hover:text-sky-600",
isLoading(endpoint) &&
"text-slate-400 cursor-not-allowed",
)}
>
{endpoint.label}
<span className="truncate">{endpoint.label}</span>
<ArrowTopRightOnSquareIcon
className={classNames(
"size-4 shrink-0 hidden",
"group-hover/endpoint-tree-item:block",
)}
/>
</a>
}
title={endpoint.url}
icon={
<>
{endpoint.browserSupport && (
<GlobeAltIcon
className={classNames("w-4 h-4", theme.text1)}
/>
)}
{!endpoint.browserSupport && (
<LinkIcon
className={classNames("w-4 h-4", theme.text1)}
/>
)}
</>
}
secondaryLabel={
<>
{endpoint.exposeStatus !== "connected" && (
<ShareIcon
className={classNames(
"w-4 h-4",
theme.text1,
endpoint.exposeStatus === "connecting"
? "cursor-not-allowed"
: "cursor-pointer",
)}
title="Open a tunnel for this endpoint"
onClick={() => {
exposeEndpoint(endpoint.id);
}}
/>
)}
<Button
small
disabled={isLoading(endpoint)}
className="min-w-[5rem] justify-center"
onClick={() => {
if (endpoint.exposeStatus === "connected") {
onHideEndpoint(endpoint.id);
} else {
exposeEndpoint(endpoint.id);
}
}}
>
{endpoint.exposeStatus === "connected" && (
<EyeSlashIcon
className={classNames(
"w-4 h-4 cursor-pointer",
theme.text1,
)}
title="Close the tunnel for this endpoint"
onClick={() => {
hideEndpoint(endpoint.id);
}}
/>
<>
{isLoading(endpoint) && "Closing"}
{!isLoading(endpoint) && "Close"}
</>
)}
</>
{endpoint.exposeStatus === "disconnected" && "Expose"}
{endpoint.exposeStatus === "connecting" && "Exposing"}
</Button>
}
/>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import {
import classNames from "classnames";
import { useMemo } from "react";

import type { TestItem } from "../shared/test-item.js";

import { NoTests } from "./no-tests.js";
import type { TestItem } from "./test-item.js";

export interface TestTreeProps {
testList: TestItem[];
Expand Down

0 comments on commit 90b70db

Please sign in to comment.