index.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import React from "react";
  2. import toast from "react-hot-toast";
  3. import Link from "next/link";
  4. import styled from "styled-components";
  5. import { CanvasDirection } from "reaflow";
  6. import { TiFlowMerge } from "react-icons/ti";
  7. import {
  8. CgArrowsMergeAltH,
  9. CgArrowsShrinkH,
  10. CgPerformance,
  11. } from "react-icons/cg";
  12. import {
  13. AiOutlineDelete,
  14. AiFillGithub,
  15. AiOutlineTwitter,
  16. AiOutlineSave,
  17. AiOutlineFileAdd,
  18. AiOutlineLink,
  19. } from "react-icons/ai";
  20. import { Tooltip } from "src/components/Tooltip";
  21. import { useRouter } from "next/router";
  22. import { ImportModal } from "src/containers/Modals/ImportModal";
  23. import { ClearModal } from "src/containers/Modals/ClearModal";
  24. import { ShareModal } from "src/containers/Modals/ShareModal";
  25. import useConfig from "src/hooks/store/useConfig";
  26. import { getNextLayout } from "src/containers/Editor/LiveEditor/helpers";
  27. import { HiHeart } from "react-icons/hi";
  28. import shallow from "zustand/shallow";
  29. const StyledSidebar = styled.div`
  30. display: flex;
  31. justify-content: space-between;
  32. flex-direction: column;
  33. align-items: center;
  34. width: fit-content;
  35. background: ${({ theme }) => theme.BACKGROUND_TERTIARY};
  36. padding: 4px;
  37. border-right: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  38. `;
  39. const StyledElement = styled.div<{ beta?: boolean }>`
  40. position: relative;
  41. display: flex;
  42. justify-content: center;
  43. text-align: center;
  44. font-size: 26px;
  45. font-weight: 600;
  46. width: 100%;
  47. color: ${({ theme }) => theme.INTERACTIVE_NORMAL};
  48. cursor: pointer;
  49. ${({ theme, beta }) =>
  50. beta &&
  51. `
  52. &::after {
  53. position: absolute;
  54. display: flex;
  55. justify-content: center;
  56. align-items: center;
  57. bottom: 0;
  58. right: 0;
  59. content: 'Beta';
  60. font-size: 10px;
  61. font-weight: 500;
  62. background: ${theme.BLURPLE};
  63. border-radius: 4px;
  64. color: ${theme.FULL_WHITE};
  65. padding: 2px;
  66. height: 14px;
  67. z-index: 0;
  68. }
  69. `};
  70. svg {
  71. padding: 8px;
  72. vertical-align: middle;
  73. }
  74. a {
  75. display: flex;
  76. }
  77. &:hover :is(a, svg) {
  78. color: ${({ theme }) => theme.INTERACTIVE_HOVER};
  79. }
  80. `;
  81. const StyledText = styled.span<{ secondary?: boolean }>`
  82. color: ${({ theme, secondary }) =>
  83. secondary ? theme.INTERACTIVE_NORMAL : theme.ORANGE};
  84. `;
  85. const StyledFlowIcon = styled(TiFlowMerge)<{ rotate: number }>`
  86. transform: rotate(${({ rotate }) => `${rotate}deg`});
  87. `;
  88. const StyledTopWrapper = styled.nav`
  89. display: flex;
  90. justify-content: space-between;
  91. flex-direction: column;
  92. align-items: center;
  93. width: 100%;
  94. & > div:nth-child(n + 1) {
  95. border-bottom: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  96. }
  97. `;
  98. const StyledBottomWrapper = styled.nav`
  99. display: flex;
  100. justify-content: space-between;
  101. flex-direction: column;
  102. align-items: center;
  103. width: 100%;
  104. & > div,
  105. a:nth-child(0) {
  106. border-top: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  107. }
  108. `;
  109. const StyledLogo = styled.div`
  110. color: ${({ theme }) => theme.FULL_WHITE};
  111. `;
  112. function rotateLayout(layout: CanvasDirection) {
  113. if (layout === "LEFT") return 90;
  114. if (layout === "UP") return 180;
  115. if (layout === "RIGHT") return 270;
  116. return 360;
  117. }
  118. const StyledAlertIcon = styled(IoAlertCircleSharp)`
  119. color: ${({ theme }) => theme.ORANGE};
  120. `;
  121. export const Sidebar: React.FC = () => {
  122. const getJson = useConfig((state) => state.getJson);
  123. const setConfig = useConfig((state) => state.setConfig);
  124. const [uploadVisible, setUploadVisible] = React.useState(false);
  125. const [clearVisible, setClearVisible] = React.useState(false);
  126. const [shareVisible, setShareVisible] = React.useState(false);
  127. const { push } = useRouter();
  128. const [expand, performanceMode, layout] = useConfig(
  129. (state) => [state.expand, state.performanceMode, state.layout],
  130. shallow
  131. );
  132. const handleSave = () => {
  133. const a = document.createElement("a");
  134. const file = new Blob([getJson()], { type: "text/plain" });
  135. a.href = window.URL.createObjectURL(file);
  136. a.download = "jsonvisio.json";
  137. a.click();
  138. };
  139. const toggleExpandCollapse = () => {
  140. setConfig("expand", !expand);
  141. toast(`${expand ? "Collapsed" : "Expanded"} nodes.`);
  142. };
  143. const togglePerformance = () => {
  144. const toastMsg = performanceMode
  145. ? "Disabled Performance Mode\nSearch Node & Save Image enabled."
  146. : "Enabled Performance Mode\nSearch Node & Save Image disabled.";
  147. toast(toastMsg, {
  148. icon: <StyledAlertIcon size={36} />,
  149. duration: 3000,
  150. });
  151. setConfig("performanceMode", !performanceMode);
  152. };
  153. const toggleLayout = () => {
  154. const nextLayout = getNextLayout(layout);
  155. setConfig("layout", nextLayout);
  156. };
  157. return (
  158. <StyledSidebar>
  159. <StyledTopWrapper>
  160. <Link passHref href="/">
  161. <StyledElement onClick={() => push("/")}>
  162. <StyledLogo>
  163. <StyledText>J</StyledText>
  164. <StyledText secondary>V</StyledText>
  165. </StyledLogo>
  166. </StyledElement>
  167. </Link>
  168. <Tooltip title="Import File">
  169. <StyledElement onClick={() => setUploadVisible(true)}>
  170. <AiOutlineFileAdd />
  171. </StyledElement>
  172. </Tooltip>
  173. <Tooltip title="Rotate Layout">
  174. <StyledElement onClick={toggleLayout}>
  175. <StyledFlowIcon rotate={rotateLayout(layout)} />
  176. </StyledElement>
  177. </Tooltip>
  178. <Tooltip title={expand ? "Shrink Nodes" : "Expand Nodes"}>
  179. <StyledElement
  180. title="Toggle Expand/Collapse"
  181. onClick={toggleExpandCollapse}
  182. >
  183. {expand ? <CgArrowsMergeAltH /> : <CgArrowsShrinkH />}
  184. </StyledElement>
  185. </Tooltip>
  186. <Tooltip
  187. title={`${
  188. performanceMode ? "Disable" : "Enable"
  189. } Performance Mode (Beta)`}
  190. >
  191. <StyledElement onClick={togglePerformance} beta>
  192. <CgPerformance color={performanceMode ? "#0073FF" : undefined} />
  193. </StyledElement>
  194. </Tooltip>
  195. <Tooltip title="Save JSON">
  196. <StyledElement onClick={handleSave}>
  197. <AiOutlineSave />
  198. </StyledElement>
  199. </Tooltip>
  200. <Tooltip title="Clear JSON">
  201. <StyledElement onClick={() => setClearVisible(true)}>
  202. <AiOutlineDelete />
  203. </StyledElement>
  204. </Tooltip>
  205. <Tooltip title="Share">
  206. <StyledElement onClick={() => setShareVisible(true)}>
  207. <AiOutlineLink />
  208. </StyledElement>
  209. </Tooltip>
  210. </StyledTopWrapper>
  211. <StyledBottomWrapper>
  212. <StyledElement>
  213. <Link href="https://twitter.com/aykutsarach">
  214. <a aria-label="Twitter" rel="me" target="_blank">
  215. <AiOutlineTwitter />
  216. </a>
  217. </Link>
  218. </StyledElement>
  219. <StyledElement>
  220. <Link href="https://github.com/AykutSarac/jsonvisio.com">
  221. <a aria-label="GitHub" rel="me" target="_blank">
  222. <AiFillGithub />
  223. </a>
  224. </Link>
  225. </StyledElement>
  226. <StyledElement>
  227. <Link href="https://github.com/sponsors/AykutSarac">
  228. <a aria-label="GitHub Sponsors" rel="me" target="_blank">
  229. <HiHeart />
  230. </a>
  231. </Link>
  232. </StyledElement>
  233. </StyledBottomWrapper>
  234. <ImportModal visible={uploadVisible} setVisible={setUploadVisible} />
  235. <ClearModal visible={clearVisible} setVisible={setClearVisible} />
  236. <ShareModal visible={shareVisible} setVisible={setShareVisible} />
  237. </StyledSidebar>
  238. );
  239. };