|
1 | | -import { FC, useRef, useEffect } from "preact/compat"; |
2 | | -import { useTimeDispatch } from "../../../../state/time/TimeStateContext"; |
3 | | -import { ArrowDownIcon, QuestionIcon, StorageIcon } from "../../../Main/Icons"; |
| 1 | +import { FC, useCallback, useEffect, useState } from "preact/compat"; |
4 | 2 | import Button from "../../../Main/Button/Button"; |
5 | | -import "./style.scss"; |
6 | | -import "../../TimeRangeSettings/ExecutionControls/style.scss"; |
7 | 3 | import classNames from "classnames"; |
8 | | -import Popper from "../../../Main/Popper/Popper"; |
9 | | -import { getAppModeEnable } from "../../../../utils/app-mode"; |
10 | | -import Tooltip from "../../../Main/Tooltip/Tooltip"; |
11 | 4 | import useDeviceDetect from "../../../../hooks/useDeviceDetect"; |
12 | 5 | import TextField from "../../../Main/TextField/TextField"; |
13 | | -import useBoolean from "../../../../hooks/useBoolean"; |
14 | | -import useStateSearchParams from "../../../../hooks/useStateSearchParams"; |
15 | | -import { useSearchParams } from "react-router-dom"; |
| 6 | +import { TenantType } from "./Tenants"; |
16 | 7 |
|
17 | | -const TenantsFields: FC = () => { |
18 | | - const appModeEnable = getAppModeEnable(); |
19 | | - const { isMobile } = useDeviceDetect(); |
20 | | - const timeDispatch = useTimeDispatch(); |
21 | | - |
22 | | - const [searchParams, setSearchParams] = useSearchParams(); |
23 | | - const [accountID, setAccountID] = useStateSearchParams("0", "accountID"); |
24 | | - const [projectID, setProjectID] = useStateSearchParams("0", "projectID"); |
25 | | - const formattedTenant = `${accountID}:${projectID}`; |
26 | | - |
27 | | - const buttonRef = useRef<HTMLDivElement>(null); |
| 8 | +interface Props extends TenantType { |
| 9 | + accountId: string; |
| 10 | + projectId: string; |
| 11 | + onChange: (tenant: Partial<TenantType>) => void; |
| 12 | +} |
28 | 13 |
|
29 | | - const { |
30 | | - value: openPopup, |
31 | | - toggle: toggleOpenPopup, |
32 | | - setFalse: handleClosePopup, |
33 | | - } = useBoolean(false); |
| 14 | +const TenantsFields: FC<Props> = ({ accountId, projectId, onChange }) => { |
| 15 | + const { isMobile } = useDeviceDetect(); |
34 | 16 |
|
35 | | - const applyChanges = () => { |
36 | | - searchParams.set("accountID", accountID); |
37 | | - searchParams.set("projectID", projectID); |
38 | | - setSearchParams(searchParams); |
39 | | - handleClosePopup(); |
40 | | - timeDispatch({ type: "RUN_QUERY" }); |
41 | | - }; |
| 17 | + const [accountTmp, setAccountTmp] = useState(accountId); |
| 18 | + const [projectTmp, setProjectTmp] = useState(projectId); |
42 | 19 |
|
43 | | - const handleReset = () => { |
44 | | - setAccountID(searchParams.get("accountID") || "0"); |
45 | | - setProjectID(searchParams.get("projectID") || "0"); |
46 | | - }; |
| 20 | + const applyChanges = useCallback(() => { |
| 21 | + onChange({ |
| 22 | + accountId: accountTmp || accountId, |
| 23 | + projectId: projectTmp || projectId |
| 24 | + }); |
| 25 | + }, [accountTmp, accountId, projectTmp, projectId]); |
47 | 26 |
|
48 | 27 | useEffect(() => { |
49 | | - if (openPopup) return; |
50 | | - handleReset(); |
51 | | - }, [openPopup]); |
| 28 | + setAccountTmp(accountId); |
| 29 | + setProjectTmp(projectId); |
| 30 | + }, [accountId, projectId]); |
52 | 31 |
|
53 | 32 | return ( |
54 | | - <div className="vm-tenant-input"> |
55 | | - <Tooltip title="Define Tenant ID if you need request to another storage"> |
56 | | - <div ref={buttonRef}> |
57 | | - {isMobile ? ( |
58 | | - <div |
59 | | - className="vm-mobile-option" |
60 | | - onClick={toggleOpenPopup} |
61 | | - > |
62 | | - <span className="vm-mobile-option__icon"><StorageIcon/></span> |
63 | | - <div className="vm-mobile-option-text"> |
64 | | - <span className="vm-mobile-option-text__label">Tenant ID</span> |
65 | | - <span className="vm-mobile-option-text__value">{formattedTenant}</span> |
66 | | - </div> |
67 | | - <span className="vm-mobile-option__arrow"><ArrowDownIcon/></span> |
68 | | - </div> |
69 | | - ) : ( |
70 | | - <Button |
71 | | - className={appModeEnable ? "" : "vm-header-button"} |
72 | | - variant="contained" |
73 | | - color="primary" |
74 | | - fullWidth |
75 | | - startIcon={<StorageIcon/>} |
76 | | - endIcon={( |
77 | | - <div |
78 | | - className={classNames({ |
79 | | - "vm-execution-controls-buttons__arrow": true, |
80 | | - "vm-execution-controls-buttons__arrow_open": openPopup, |
81 | | - })} |
82 | | - > |
83 | | - <ArrowDownIcon/> |
84 | | - </div> |
85 | | - )} |
86 | | - onClick={toggleOpenPopup} |
87 | | - > |
88 | | - {formattedTenant} |
89 | | - </Button> |
90 | | - )} |
91 | | - </div> |
92 | | - </Tooltip> |
93 | | - <Popper |
94 | | - open={openPopup} |
95 | | - placement="bottom-right" |
96 | | - onClose={handleClosePopup} |
97 | | - buttonRef={buttonRef} |
98 | | - title={isMobile ? "Define Tenant ID" : undefined} |
99 | | - > |
100 | | - <div |
101 | | - className={classNames({ |
102 | | - "vm-list vm-tenant-input-list": true, |
103 | | - "vm-list vm-tenant-input-list_mobile": isMobile, |
104 | | - "vm-tenant-input-list_inline": true, |
105 | | - })} |
| 33 | + <div |
| 34 | + className={classNames({ |
| 35 | + "vm-list vm-tenant-input-list": true, |
| 36 | + "vm-list vm-tenant-input-list_mobile": isMobile, |
| 37 | + })} |
| 38 | + > |
| 39 | + <div className="vm-tenant-input-list__fields"> |
| 40 | + <TextField |
| 41 | + autofocus |
| 42 | + label="accountID" |
| 43 | + value={accountTmp} |
| 44 | + onChange={setAccountTmp} |
| 45 | + type="number" |
| 46 | + /> |
| 47 | + <TextField |
| 48 | + autofocus |
| 49 | + label="projectID" |
| 50 | + value={projectTmp} |
| 51 | + onChange={setProjectTmp} |
| 52 | + type="number" |
| 53 | + /> |
| 54 | + </div> |
| 55 | + <div className="vm-tenant-input-list__buttons"> |
| 56 | + <a |
| 57 | + href="https://docs.victoriametrics.com/victorialogs/#multitenancy" |
| 58 | + target="_blank" |
| 59 | + rel="help noreferrer" |
| 60 | + > |
| 61 | + <Button |
| 62 | + variant="text" |
| 63 | + color="primary" |
| 64 | + > |
| 65 | + Read more |
| 66 | + </Button> |
| 67 | + </a> |
| 68 | + <Button |
| 69 | + variant="contained" |
| 70 | + color="primary" |
| 71 | + onClick={applyChanges} |
106 | 72 | > |
107 | | - <TextField |
108 | | - autofocus |
109 | | - label="accountID" |
110 | | - value={accountID} |
111 | | - onChange={setAccountID} |
112 | | - type="number" |
113 | | - /> |
114 | | - <TextField |
115 | | - autofocus |
116 | | - label="projectID" |
117 | | - value={projectID} |
118 | | - onChange={setProjectID} |
119 | | - type="number" |
120 | | - /> |
121 | | - <div className="vm-tenant-input-list__buttons"> |
122 | | - <Tooltip title="Multitenancy in VictoriaLogs documentation"> |
123 | | - <a |
124 | | - href="https://docs.victoriametrics.com/victorialogs/#multitenancy" |
125 | | - target="_blank" |
126 | | - rel="help noreferrer" |
127 | | - > |
128 | | - <Button |
129 | | - variant="text" |
130 | | - color="gray" |
131 | | - startIcon={<QuestionIcon/>} |
132 | | - /> |
133 | | - </a> |
134 | | - </Tooltip> |
135 | | - <Button |
136 | | - variant="contained" |
137 | | - color="primary" |
138 | | - onClick={applyChanges} |
139 | | - > |
140 | | - Apply |
141 | | - </Button> |
142 | | - </div> |
143 | | - </div> |
144 | | - </Popper> |
| 73 | + Apply |
| 74 | + </Button> |
| 75 | + </div> |
145 | 76 | </div> |
146 | 77 | ); |
147 | 78 | }; |
|
0 commit comments