forked from labring/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
107 lines (96 loc) · 2.95 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {
Button,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
useDisclosure,
} from "@chakra-ui/react";
import { t } from "i18next";
import CommonDiffEditor from "@/components/Editor/CommonDiffEditor";
import { Pages } from "@/constants";
import { useUpdateFunctionMutation } from "../../service";
import useFunctionStore from "../../store";
import useFunctionCache from "@/hooks/useFuncitonCache";
import useHotKey, { DEFAULT_SHORTCUTS } from "@/hooks/useHotKey";
import useGlobalStore from "@/pages/globalStore";
export default function DeployButton() {
const { isOpen, onOpen, onClose } = useDisclosure();
const store = useFunctionStore((state) => state);
const functionCache = useFunctionCache();
const { showSuccess, currentPageId } = useGlobalStore((state) => state);
const updateFunctionMutation = useUpdateFunctionMutation();
const { displayName } = useHotKey(
DEFAULT_SHORTCUTS.deploy,
async () => {
onOpen();
},
{
enabled: currentPageId === Pages.function,
},
);
const deploy = async () => {
const res = await updateFunctionMutation.mutateAsync({
description: store.currentFunction?.desc,
code: functionCache.getCache(store.currentFunction!.id),
methods: store.currentFunction?.methods,
websocket: store.currentFunction?.websocket,
name: store.currentFunction?.name,
tags: store.currentFunction?.tags,
});
if (!res.error) {
store.setCurrentFunction(res.data);
// delete cache after deploy
functionCache.removeCache(store.currentFunction!.id);
onClose();
showSuccess(t("Message.DeploySuccess"));
}
};
return (
<>
<Button
size="sm"
borderRadius={4}
disabled={store.getFunctionUrl() === ""}
colorScheme="blue"
padding="0 12px"
onClick={() => {
onOpen();
}}
>
{t("FunctionPanel.Deploy")} ({displayName.toUpperCase()})
</Button>
{isOpen ? (
<Modal isOpen={isOpen} onClose={onClose} size="6xl" isCentered>
<ModalOverlay />
<ModalContent maxW={"80%"}>
<ModalHeader>Code Diff</ModalHeader>
<ModalCloseButton />
<ModalBody borderBottom={"1px"} borderBottomColor="gray.200">
<CommonDiffEditor
original={store.currentFunction?.source?.code}
modified={functionCache.getCache(store.currentFunction?.id)}
/>
</ModalBody>
<ModalFooter>
<Button mr={3} onClick={onClose}>
{t("Cancel")}
</Button>
<Button
colorScheme={"blue"}
onClick={() => {
deploy();
}}
>
{t("Common.Dialog.ConfirmDeploy")}
</Button>
</ModalFooter>
</ModalContent>
</Modal>
) : null}
</>
);
}