-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Simulator button to toggle simulation status in database (#41)
* Give simulator toggle button * Reset * Prettier fix * Introduce indigo color schema * New simulator button * Fix toLocalLowerCase usage * Rename simulator warning component * Prettier fix * Prettier fix * Restructured onToggle function for simulator button * Use kebab case --------- Co-authored-by: sbafna1 <sbafna@ip-10-170-33-212>
- Loading branch information
1 parent
e8db6dd
commit 5e33c71
Showing
7 changed files
with
148 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,136 @@ | ||
import { Icon, InfoIcon } from "@chakra-ui/icons"; | ||
import { | ||
Alert, | ||
AlertDescription, | ||
AlertIcon, | ||
AlertTitle, | ||
Box, | ||
Button, | ||
useBoolean, | ||
FormControl, | ||
FormControlProps, | ||
FormLabel, | ||
Switch, | ||
SwitchProps, | ||
Text, | ||
Tooltip, | ||
useColorModeValue, | ||
} from "@chakra-ui/react"; | ||
import * as React from "react"; | ||
import { useRecoilValue, useSetRecoilState } from "recoil"; | ||
import { useRecoilState, useRecoilValue } from "recoil"; | ||
|
||
import { trackAnalyticsEvent } from "@/analytics"; | ||
import { setSessionController } from "@/data/queries"; | ||
import { connectionConfig, simulatorEnabled } from "@/data/recoil"; | ||
import { useConnectionState, useMountedCallback } from "@/view/hooks/hooks"; | ||
import { useConnectionState } from "@/view/hooks/hooks"; | ||
import { useSession } from "@/view/hooks/useSession"; | ||
|
||
export const EnableSimulatorButton = () => { | ||
const setEnabled = useSetRecoilState(simulatorEnabled); | ||
export const EnableSimulatorWarning = () => { | ||
return ( | ||
<Alert status="warning" borderRadius="md"> | ||
<AlertIcon /> | ||
<Box display="flex" flexDirection="column"> | ||
<AlertTitle>The simulator is disabled</AlertTitle> | ||
<AlertDescription> | ||
This application uses a simulator to generate new data like live | ||
notifications and subscribers. To experience the full power of | ||
real-time digital marketing, please enable the simulator in the nav | ||
bar. | ||
</AlertDescription> | ||
</Box> | ||
</Alert> | ||
); | ||
}; | ||
|
||
export const SimulatorToggler = ({ | ||
switchProps, | ||
containerProps, | ||
}: { | ||
switchProps?: SwitchProps; | ||
containerProps?: FormControlProps; | ||
}) => { | ||
const [enabled, setEnabled] = useRecoilState(simulatorEnabled); | ||
const { session, refresh: refreshSession } = useSession(); | ||
const config = useRecoilValue(connectionConfig); | ||
const { connected, initialized } = useConnectionState(); | ||
const [toggling, setToggling] = React.useState(false); | ||
|
||
const [enabling, enablingCtrl] = useBoolean(false); | ||
const stopSpinner = useMountedCallback( | ||
() => enablingCtrl.off, | ||
[enablingCtrl] | ||
); | ||
const onEnableSimulator = React.useCallback(async () => { | ||
enablingCtrl.on(); | ||
trackAnalyticsEvent("enable-simulator"); | ||
|
||
const onToggleSimulator = React.useCallback(async () => { | ||
setToggling(true); | ||
const newState = !enabled; | ||
trackAnalyticsEvent("change-simulator-state", { | ||
enabled: newState, | ||
}); | ||
if (connected && initialized) { | ||
await setSessionController(config, session.sessionID, true); | ||
await setSessionController(config, session.sessionID, newState); | ||
} | ||
setEnabled(true); | ||
|
||
setEnabled(newState); | ||
refreshSession(); | ||
stopSpinner(); | ||
setToggling(false); | ||
}, [ | ||
config, | ||
connected, | ||
enablingCtrl, | ||
initialized, | ||
refreshSession, | ||
session.sessionID, | ||
enabled, | ||
refreshSession, | ||
setEnabled, | ||
stopSpinner, | ||
]); | ||
|
||
return ( | ||
<Alert status="warning" borderRadius="md"> | ||
<AlertIcon /> | ||
<AlertTitle>The simulator is disabled</AlertTitle> | ||
<FormControl {...containerProps} gap={3}> | ||
<FormLabel | ||
htmlFor="simulator-switch" | ||
fontSize="xs" | ||
fontWeight="bold" | ||
display="inline" | ||
padding={0} | ||
margin={0} | ||
> | ||
<Box gap={1} display="flex"> | ||
<Text>Simulator</Text> | ||
<Icon as={InfoIcon} fontSize="1.1em" /> | ||
</Box> | ||
</FormLabel> | ||
<Switch | ||
id="simulator-switch" | ||
disabled={toggling} | ||
isChecked={enabled} | ||
onChange={onToggleSimulator} | ||
{...switchProps} | ||
/> | ||
</FormControl> | ||
); | ||
}; | ||
|
||
export const SimulatorButton = () => { | ||
return ( | ||
<Tooltip | ||
variant="simulator" | ||
label={ | ||
<Text padding={2}> | ||
The simulator generates live notifications and subscribers even if the | ||
application browser window is closed. Toggle off to stop new data | ||
generation or suspend cluster in SingleStoreDB portal. | ||
</Text> | ||
} | ||
hasArrow | ||
textAlign="center" | ||
> | ||
<Button | ||
position="absolute" | ||
right={4} | ||
top={3} | ||
size="xs" | ||
background={useColorModeValue("#ECE8FD", "#2F206E")} | ||
color={useColorModeValue("#553ACF", "#ECE8FD")} | ||
disabled={enabling} | ||
onClick={onEnableSimulator} | ||
style={{ | ||
color: useColorModeValue("black", "white"), | ||
backgroundColor: useColorModeValue("#DCD5FB", "#3A249E"), | ||
border: "none", | ||
boxShadow: "none", | ||
}} | ||
padding="8px 18px 8px 18px" | ||
size="sm" | ||
> | ||
Enable simulator | ||
<SimulatorToggler | ||
switchProps={{ size: "sm", variant: "simulator" }} | ||
containerProps={{ display: "flex", alignItems: "center" }} | ||
/> | ||
</Button> | ||
</Alert> | ||
</Tooltip> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters