Skip to content

feat: add port sharing frontend #12119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add mutations
  • Loading branch information
f0ssel committed Feb 13, 2024
commit 7839a2c72bee1f0a3c8aecfc286e67356375153e
24 changes: 24 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,30 @@ export const getWorkspaceAgentSharedPorts = async (
return response.data;
};

export const postWorkspaceAgentSharedPort = async (
workspaceID: string,
req: TypesGen.UpdateWorkspaceAgentPortShareRequest,
): Promise<TypesGen.WorkspaceAgentPortShares> => {
const response = await axios.post(
`/api/v2/workspaces/${workspaceID}/shared-port`,
req
);
return response.data;
};

export const deleteWorkspaceAgentSharedPort = async (
workspaceID: string,
req: TypesGen.DeleteWorkspaceAgentPortShareRequest,
): Promise<TypesGen.WorkspaceAgentPortShares> => {
const response = await axios.delete(
`/api/v2/workspaces/${workspaceID}/shared-port`,
{
data: req,
}
);
return response.data;
};

// getDeploymentSSHConfig is used by the VSCode-Extension.
export const getDeploymentSSHConfig =
async (): Promise<TypesGen.SSHConfigResponse> => {
Expand Down
48 changes: 39 additions & 9 deletions site/src/modules/resources/PortForwardButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import CircularProgress from "@mui/material/CircularProgress";
import OpenInNewOutlined from "@mui/icons-material/OpenInNewOutlined";
import { type Interpolation, type Theme, useTheme } from "@emotion/react";
import type { FC } from "react";
import { useQuery } from "react-query";
import { useQuery, useMutation } from "react-query";
import { docs } from "utils/docs";
import { getAgentListeningPorts, getWorkspaceAgentSharedPorts } from "api/api";
import { deleteWorkspaceAgentSharedPort, getAgentListeningPorts, getWorkspaceAgentSharedPorts, postWorkspaceAgentSharedPort } from "api/api";
import type {
DeleteWorkspaceAgentPortShareRequest,
UpdateWorkspaceAgentPortShareRequest,
WorkspaceAgent,
WorkspaceAgentListeningPort,
WorkspaceAgentListeningPortsResponse,
WorkspaceAgentPortShare,
WorkspaceAgentPortShareLevel,
WorkspaceAgentPortShares,
} from "api/typesGenerated";
import { portForwardURL } from "utils/portForward";
Expand All @@ -37,7 +40,6 @@ import LockIcon from "@mui/icons-material/Lock";
import LockOpenIcon from "@mui/icons-material/LockOpen";
import IconButton from "@mui/material/IconButton";
import CloseIcon from "@mui/icons-material/Close";
import Grid from "@mui/material/Grid";

export interface PortForwardButtonProps {
host: string;
Expand Down Expand Up @@ -68,7 +70,7 @@ export const PortForwardButton: FC<PortForwardButtonProps> = (props) => {
});

const sharedPortsQuery = useQuery({
queryKey: ["sharedPorts", agent.id],
queryKey: ["sharedPorts", workspaceID],
queryFn: () => getWorkspaceAgentSharedPorts(workspaceID),
enabled: !storybook && agent.status === "connected",
});
Expand Down Expand Up @@ -123,13 +125,27 @@ interface PortForwardPopoverViewProps extends PortForwardButtonProps {
export const PortForwardPopoverView: FC<PortForwardPopoverViewProps> = ({
host,
workspaceName,
workspaceID,
agent,
username,
listeningPorts,
sharedPorts,
}) => {
const theme = useTheme();


const createSharedPortMutation = useMutation({
mutationFn: async (options: UpdateWorkspaceAgentPortShareRequest) => {
await postWorkspaceAgentSharedPort(workspaceID, options);
},
});

const deleteSharedPortMutation = useMutation({
mutationFn: async (options: DeleteWorkspaceAgentPortShareRequest) => {
await deleteWorkspaceAgentSharedPort(workspaceID, options);
},
});

// we don't want to show listening ports if it's already a shared port
const filteredListeningPorts = listeningPorts?.filter(
(port) => {
Expand Down Expand Up @@ -258,7 +274,15 @@ export const PortForwardPopoverView: FC<PortForwardPopoverViewProps> = ({
>
<span css={styles.portNumber}>{port.port}</span>
</Link>
<Button size="small" variant="text">
<Button size="small" variant="text" onClick={
() => {
createSharedPortMutation.mutate({
agent_name: agent.name,
port: port.port,
share_level: "authenticated",
});
}
}>
Share
</Button>
</Stack>
Expand Down Expand Up @@ -328,7 +352,12 @@ export const PortForwardPopoverView: FC<PortForwardPopoverViewProps> = ({
<MenuItem value="public">Public</MenuItem>
</Select>
</FormControl>
<IconButton>
<IconButton onClick={() => {
deleteSharedPortMutation.mutate({
agent_name: agent.name,
port: share.port,
});
}}>
<CloseIcon
css={{
width: 14,
Expand All @@ -352,11 +381,12 @@ export const PortForwardPopoverView: FC<PortForwardPopoverViewProps> = ({
>
<TextField label="Port" variant="outlined" size="small" />
<FormControl size="small">
<Select value="Authenticated">
<MenuItem value="Authenticated">Authenticated</MenuItem>
<MenuItem value="Public">Public</MenuItem>
<Select value="authenticated">
<MenuItem value="authenticated">Authenticated</MenuItem>
<MenuItem value="public">Public</MenuItem>
</Select>
</FormControl>
{/* How do I use the value from the select in the mutation? */}
<Button variant="contained">Share Port</Button>
</Stack>
</div>
Expand Down