feat: handle port forward subdomain conflict

This commit is contained in:
2024-12-04 00:10:25 +00:00
parent ee857cbbf9
commit 4c41d4ce07
8 changed files with 81 additions and 7 deletions

View File

@@ -165,7 +165,7 @@ function useDeleteWorkspace() {
}
function useAddWorkspacePort() {
const [status, setStatus] = useState<QueryStatus>({ type: "idle" });
const [status, setStatus] = useState<QueryStatus<ApiError>>({ type: "idle" });
const { mutate } = useSWRConfig();
const addWorkspacePort = useCallback(
@@ -191,7 +191,7 @@ function useAddWorkspacePort() {
);
setStatus({ type: "ok" });
} catch (error: unknown) {
setStatus({ type: "error", error });
setStatus({ type: "error", error: error as ApiError });
}
},
[mutate],

View File

@@ -94,6 +94,7 @@ const NewPortMappingForm = object({
function NewPortMappingRow() {
const { addWorkspacePort, status } = useAddWorkspacePort();
const { toast } = useToast();
const workspace = useContext(WorkspaceTableRowContext);
const isAddingPort = useStore((state) => state.isAddingPort);
const setIsAddingPort = useStore((state) => state.setIsAddingPort);
@@ -107,6 +108,42 @@ function NewPortMappingRow() {
},
});
useEffect(() => {
switch (status.type) {
case "error":
switch (status.error.type) {
case "CONFLICT":
toast({
variant: "destructive",
title: "Subdomain already in use!",
description: "Please use another subdomain for this port.",
});
break;
case "NETWORK":
toast({
variant: "destructive",
title: "Failed to forward port.",
description: "Network error.",
});
break;
default:
toast({
variant: "destructive",
title: "Failed to forward port.",
description: "Unkown error.",
});
break;
}
break;
case "ok":
setIsAddingPort(false);
break;
}
}, [status]);
if (!isAddingPort) {
return null;
}
@@ -118,7 +155,6 @@ function NewPortMappingRow() {
port: values.port,
},
]);
setIsAddingPort(false);
}
return (