Skip to content

Commit

Permalink
feat(web): support setting custom domain for application (labring#1338)
Browse files Browse the repository at this point in the history
* feat(web): add custom domain

* fix(web): fix darkmode
  • Loading branch information
newfish-cmyk authored Jun 30, 2023
1 parent 124b919 commit f3d6845
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 18 deletions.
4 changes: 3 additions & 1 deletion web/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@
"RemoveHost": "Remove Host",
"CancelHost": "Cancel Host",
"InstantUpload": "Instant Upload",
"parsing": "parsing"
"parsing": "parsing",
"DomainUpdateSuccess": "Domain Update Success",
"DomainDeleteSuccess": "Domain Delete Success"
},
"TriggerPanel": {
"AddTrigger": "Add Trigger",
Expand Down
6 changes: 4 additions & 2 deletions web/public/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"DeleteConfirm": "确定删除该行数据吗?",
"DeleteSuccess": "删除成功",
"DeleteTip": "无法撤销",
"Edit": "编辑名称",
"Edit": "编辑",
"Empty": "清空",
"Generate": "生成",
"InputTip": "请输入",
Expand Down Expand Up @@ -240,7 +240,9 @@
"CancelHost": "取消托管",
"Inaccessible": "无法访问",
"InstantUpload": "立即上传",
"RemoveHost": "取消静态托管"
"RemoveHost": "取消静态托管",
"DomainUpdateSuccess": "域名修改成功",
"DomainDeleteSuccess": "域名删除成功"
},
"TriggerPanel": {
"AddTrigger": "新建触发器",
Expand Down
6 changes: 4 additions & 2 deletions web/public/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"DeleteConfirm": "确定删除该行数据吗?",
"DeleteSuccess": "删除成功",
"DeleteTip": "无法撤销",
"Edit": "编辑名称",
"Edit": "编辑",
"Empty": "清空",
"Generate": "生成",
"InputTip": "请输入",
Expand Down Expand Up @@ -240,7 +240,9 @@
"CancelHost": "取消托管",
"Inaccessible": "无法访问",
"InstantUpload": "立即上传",
"RemoveHost": "取消静态托管"
"RemoveHost": "取消静态托管",
"DomainUpdateSuccess": "域名修改成功",
"DomainDeleteSuccess": "域名删除成功"
},
"TriggerPanel": {
"AddTrigger": "新建触发器",
Expand Down
1 change: 1 addition & 0 deletions web/src/apis/typing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export type TDomain = {
_id: string;
appid: string;
domain: string;
customDomain: string;
state: string;
phase: string;
lockedAt: string;
Expand Down
14 changes: 14 additions & 0 deletions web/src/apis/v1/api-auto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,20 @@ declare namespace Definitions {
}

declare namespace Paths {
namespace ApplicationControllerCheckResolved {
export type QueryParameters = any;

export type BodyParameters = any;

export type Responses = any;
}
namespace ApplicationControllerRemove {
export type QueryParameters = any;

export type BodyParameters = any;

export type Responses = any;
}
namespace AuthControllerGetProfile {
export type QueryParameters = any;

Expand Down
60 changes: 60 additions & 0 deletions web/src/apis/v1/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,63 @@ export async function ApplicationControllerUpdateBundle(
data: params,
});
}

/**
* Bind custom domain to application
*/
export async function ApplicationControllerBindDomain(
params: Definitions.BindCustomDomainDto,
): Promise<{
error: string;
data: Definitions.RuntimeDomain;
}> {
// /v1/applications/{appid}/domain
let _params: { [key: string]: any } = {
appid: useGlobalStore.getState().currentApp?.appid || "",
...params,
};
return request(`/v1/applications/${_params.appid}/domain`, {
method: "PATCH",
data: params,
});
}

/**
* Remove custom domain of application
*/
export async function ApplicationControllerRemove(
params: Paths.ApplicationControllerRemove.BodyParameters,
): Promise<{
error: string;
data: Definitions.RuntimeDomain;
}> {
// /v1/applications/{appid}/domain
let _params: { [key: string]: any } = {
appid: useGlobalStore.getState().currentApp?.appid || "",
...params,
};
return request(`/v1/applications/${_params.appid}/domain`, {
method: "DELETE",
data: params,
});
}

/**
* Check if domain is resolved
*/
export async function ApplicationControllerCheckResolved(
params: Definitions.BindCustomDomainDto,
): Promise<{
error: string;
data: Paths.ApplicationControllerCheckResolved.Responses;
}> {
// /v1/applications/{appid}/domain/resolved
let _params: { [key: string]: any } = {
appid: useGlobalStore.getState().currentApp?.appid || "",
...params,
};
return request(`/v1/applications/${_params.appid}/domain/resolved`, {
method: "POST",
data: params,
});
}
8 changes: 8 additions & 0 deletions web/src/chakraTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ const Button = defineStyleConfig({
},
},

warn: {
bg: "error.100",
color: "error.500",
_hover: {
bg: "error.200",
},
},

warnText: {
color: "error.500",
_hover: {
Expand Down
116 changes: 116 additions & 0 deletions web/src/pages/app/functions/mods/EditorPanel/EditDomain.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from "react";
import { useForm } from "react-hook-form";
import {
Button,
FormControl,
FormLabel,
Input,
InputGroup,
InputRightAddon,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
Tooltip,
useDisclosure,
} from "@chakra-ui/react";
import { t } from "i18next";

import CopyText from "@/components/CopyText";

import { useBindDomainMutation, useRemoveApplicationMutation } from "../../service";

import useGlobalStore from "@/pages/globalStore";

export default function EditDomain(props: { children: any }) {
const { children } = props;
const { isOpen, onOpen, onClose } = useDisclosure();
const { currentApp, showSuccess, setCurrentApp } = useGlobalStore();

const { register, handleSubmit, reset } = useForm<{ domain: string }>({
defaultValues: { domain: currentApp?.domain.customDomain || "" },
});

const bindDomainMutation = useBindDomainMutation();
const removeDomainMutation = useRemoveApplicationMutation();

return (
<>
<Tooltip label={t("Edit")}>
{React.cloneElement(children, {
onClick: (event?: any) => {
event?.preventDefault();
reset({ domain: currentApp?.domain.customDomain || "" });
onOpen();
},
})}
</Tooltip>

<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>{t("StoragePanel.CustomDomain")}</ModalHeader>
<ModalCloseButton />
<ModalBody>
<FormControl>
<FormLabel>CNAME</FormLabel>
<InputGroup>
<Input variant="filled" value={currentApp?.domain.domain} readOnly />
<InputRightAddon
children={
<CopyText text={currentApp?.domain.domain} className="cursor-pointer" />
}
/>
</InputGroup>
</FormControl>
<FormControl>
<FormLabel htmlFor="domain">{t("StoragePanel.domain")}</FormLabel>
<Input
{...register("domain", {
required: true,
})}
variant="filled"
placeholder={String(t("StoragePanel.domainTip"))}
/>
</FormControl>
</ModalBody>
<ModalFooter>
<Button
variant={"warn"}
onClick={async () => {
const res: any = await removeDomainMutation.mutateAsync({});
if (res.data) {
onClose();
setCurrentApp({ ...currentApp, domain: res.data });
showSuccess(t("StoragePanel.DomainDeleteSuccess"));
}
}}
className="mr-4"
>
{t("Delete")}
</Button>
<Button
type="submit"
isLoading={bindDomainMutation.isLoading}
onClick={handleSubmit(async (value) => {
const res: any = await bindDomainMutation.mutateAsync({
domain: value.domain,
});
if (res.data) {
onClose();
setCurrentApp({ ...currentApp, domain: res.data });
showSuccess(t("StoragePanel.DomainUpdateSuccess"));
}
})}
>
{t("Confirm")}
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
}
20 changes: 15 additions & 5 deletions web/src/pages/app/functions/mods/EditorPanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useTranslation } from "react-i18next";
import { CopyIcon, EditIcon } from "@chakra-ui/icons";
import { HStack, Input, useColorMode } from "@chakra-ui/react";
import clsx from "clsx";

Expand All @@ -14,6 +15,7 @@ import DeployButton from "../DeployButton";
import CreateModal from "../FunctionPanel/CreateModal";
import PromptModal from "../FunctionPanel/CreateModal/PromptModal";

import EditDomain from "./EditDomain";
import FunctionDetailPopOver from "./FunctionDetailPopOver";

import useFunctionCache from "@/hooks/useFunctionCache";
Expand All @@ -32,7 +34,7 @@ function EditorPanel() {
{currentFunction?.name ? (
<Panel.Header
className={clsx("!mb-0 h-[50px] px-2", {
"border-b-2": !darkMode,
"border-b-2 ": !darkMode,
"border-lafWhite-400": !darkMode,
})}
>
Expand All @@ -57,10 +59,18 @@ function EditorPanel() {
</HStack>

<HStack spacing={1}>
<CopyText text={getFunctionUrl()}>
<Input w={"220px"} size="xs" readOnly value={getFunctionUrl()} />
</CopyText>

<div className={clsx("flex items-center", !darkMode && "bg-[#F6F8F9]")}>
<Input w={"200px"} size="xs" readOnly value={getFunctionUrl()} />
<EditDomain>
<EditIcon className="mx-2 cursor-pointer !text-grayModern-300" />
</EditDomain>
<CopyText
text={getFunctionUrl()}
className="mr-3 cursor-pointer !text-grayModern-300"
>
<CopyIcon />
</CopyText>
</div>
<DeployButton />
</HStack>
</Panel.Header>
Expand Down
14 changes: 7 additions & 7 deletions web/src/pages/app/functions/mods/FunctionPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
***************************/

import { useEffect, useState } from "react";
import { TbBrandGithubCopilot } from "react-icons/tb";
// import { TbBrandGithubCopilot } from "react-icons/tb";
import { useNavigate, useParams } from "react-router-dom";
import { AddIcon, DeleteIcon, EditIcon, Search2Icon } from "@chakra-ui/icons";
import { Badge, HStack, Input, InputGroup, InputLeftElement, useColorMode } from "@chakra-ui/react";
Expand All @@ -25,7 +25,7 @@ import { useDeleteFunctionMutation, useFunctionListQuery } from "../../service";
import useFunctionStore from "../../store";
import TriggerModal from "../TriggerModal";

import PromptModal from "./CreateModal/PromptModal";
// import PromptModal from "./CreateModal/PromptModal";
import CreateModal from "./CreateModal";

import { TFunction } from "@/apis/typing";
Expand Down Expand Up @@ -145,11 +145,11 @@ export default function FunctionList() {
<TriggerIcon fontSize={13} />
</IconWrap>
</TriggerModal>,
<PromptModal key="create_prompt_modal">
<IconWrap size={20} tooltip={t("FunctionPanel.CreateWithAITip").toString()}>
<TbBrandGithubCopilot fontSize={12} />
</IconWrap>
</PromptModal>,
// <PromptModal key="create_prompt_modal">
// <IconWrap size={20} tooltip={t("FunctionPanel.CreateWithAITip").toString()}>
// <TbBrandGithubCopilot fontSize={12} />
// </IconWrap>
// </PromptModal>,
<CreateModal key="create_modal" tagList={tagsList}>
<IconWrap size={20} tooltip={t("FunctionPanel.AddFunction").toString()}>
<AddIcon fontSize={12} />
Expand Down
Loading

0 comments on commit f3d6845

Please sign in to comment.