|
| 1 | +import { useGuideContext } from "@knocklabs/react-core"; |
| 2 | +import { useStore } from "@tanstack/react-store"; |
| 3 | +import { Button } from "@telegraph/button"; |
| 4 | +import { Stack } from "@telegraph/layout"; |
| 5 | +import { Tag } from "@telegraph/tag"; |
| 6 | +import { Text } from "@telegraph/typography"; |
| 7 | +import { Minimize2, Undo2, Wrench } from "lucide-react"; |
| 8 | +import { useState } from "react"; |
| 9 | + |
| 10 | +import { checkForWindow } from "../../../core"; |
| 11 | + |
| 12 | +import "./styles.css"; |
| 13 | + |
| 14 | +export const GuideToolbar = () => { |
| 15 | + const [isCollapsed, setIsCollapsed] = useState(false); |
| 16 | + |
| 17 | + const { client } = useGuideContext(); |
| 18 | + const debugState = useStore(client.store, (state) => state.debug); |
| 19 | + |
| 20 | + if (!debugState?.forcedGuideKey) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + const handleExit = () => { |
| 25 | + const window = checkForWindow(); |
| 26 | + if (!window) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const url = new URL(window.location.href); |
| 31 | + url.searchParams.delete("knock_guide_key"); |
| 32 | + window.location.href = url.toString(); |
| 33 | + }; |
| 34 | + |
| 35 | + const handleToggleCollapse = () => { |
| 36 | + setIsCollapsed(!isCollapsed); |
| 37 | + }; |
| 38 | + |
| 39 | + if (isCollapsed) { |
| 40 | + return ( |
| 41 | + <Button |
| 42 | + onClick={handleToggleCollapse} |
| 43 | + position="fixed" |
| 44 | + top="4" |
| 45 | + right="4" |
| 46 | + zIndex="sticky" |
| 47 | + bg="surface-2" |
| 48 | + shadow="3" |
| 49 | + rounded="3" |
| 50 | + w="10" |
| 51 | + h="10" |
| 52 | + variant="soft" |
| 53 | + data-tgph-appearance="dark" |
| 54 | + aria-label="Expand guide toolbar" |
| 55 | + > |
| 56 | + <svg |
| 57 | + width="40" |
| 58 | + height="40" |
| 59 | + viewBox="0 0 40 40" |
| 60 | + fill="none" |
| 61 | + xmlns="http://www.w3.org/2000/svg" |
| 62 | + style={{ |
| 63 | + position: "absolute", |
| 64 | + top: "50%", |
| 65 | + left: "50%", |
| 66 | + transform: "translate(-50%, -50%)", |
| 67 | + }} |
| 68 | + > |
| 69 | + <path |
| 70 | + d="M11.6001 32.4V7.59998H16.6365V21.8219H16.7774L22.3067 14.8525H27.9418L21.8138 22.0696L28.4001 32.4H22.7996L18.8555 25.572L16.6365 28.0839V32.4H11.6001Z" |
| 71 | + fill="#EDEEEF" |
| 72 | + /> |
| 73 | + <path |
| 74 | + d="M28.4 10.4C28.4 11.9464 27.1467 13.2 25.6 13.2C24.0534 13.2 22.8 11.9464 22.8 10.4C22.8 8.85358 24.0534 7.59998 25.6 7.59998C27.1467 7.59998 28.4 8.85358 28.4 10.4Z" |
| 75 | + fill="#FF573A" |
| 76 | + /> |
| 77 | + </svg> |
| 78 | + </Button> |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <Stack |
| 84 | + gap="2" |
| 85 | + align="center" |
| 86 | + position="fixed" |
| 87 | + top="4" |
| 88 | + right="4" |
| 89 | + zIndex="sticky" |
| 90 | + backgroundColor="surface-2" |
| 91 | + bg="surface-2" |
| 92 | + shadow="3" |
| 93 | + rounded="3" |
| 94 | + py="2" |
| 95 | + px="3" |
| 96 | + data-tgph-appearance="dark" |
| 97 | + > |
| 98 | + <Stack gap="2" align="center" direction="row"> |
| 99 | + <Tag |
| 100 | + color="green" |
| 101 | + variant="soft" |
| 102 | + icon={{ icon: Wrench, "aria-hidden": true }} |
| 103 | + > |
| 104 | + Debug |
| 105 | + </Tag> |
| 106 | + |
| 107 | + <Text |
| 108 | + as="div" |
| 109 | + size="1" |
| 110 | + weight="medium" |
| 111 | + w="full" |
| 112 | + maxWidth="40" |
| 113 | + style={{ |
| 114 | + overflow: "hidden", |
| 115 | + textOverflow: "ellipsis", |
| 116 | + whiteSpace: "nowrap", |
| 117 | + }} |
| 118 | + > |
| 119 | + {debugState.forcedGuideKey} |
| 120 | + </Text> |
| 121 | + |
| 122 | + <Button |
| 123 | + onClick={handleExit} |
| 124 | + size="1" |
| 125 | + variant="soft" |
| 126 | + trailingIcon={{ icon: Undo2, "aria-hidden": true }} |
| 127 | + > |
| 128 | + Exit |
| 129 | + </Button> |
| 130 | + |
| 131 | + <Button |
| 132 | + onClick={handleToggleCollapse} |
| 133 | + size="1" |
| 134 | + variant="soft" |
| 135 | + leadingIcon={{ icon: Minimize2, alt: "Collapse guide toolbar" }} |
| 136 | + /> |
| 137 | + </Stack> |
| 138 | + </Stack> |
| 139 | + ); |
| 140 | +}; |
0 commit comments