index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 { RiPatreonFill } from "react-icons/ri";
  8. import {
  9. CgArrowsMergeAltH,
  10. CgArrowsShrinkH,
  11. CgPerformance,
  12. } from "react-icons/cg";
  13. import {
  14. AiOutlineDelete,
  15. AiFillGithub,
  16. AiOutlineTwitter,
  17. AiOutlineSave,
  18. AiOutlineFileAdd,
  19. AiOutlineLink,
  20. } from "react-icons/ai";
  21. import { Tooltip } from "src/components/Tooltip";
  22. import { useRouter } from "next/router";
  23. import { ImportModal } from "src/containers/Modals/ImportModal";
  24. import { ClearModal } from "src/containers/Modals/ClearModal";
  25. import { ShareModal } from "src/containers/Modals/ShareModal";
  26. import { IoAlertCircleSharp } from "react-icons/io5";
  27. import useConfig from "src/hooks/store/useConfig";
  28. import { getNextLayout } from "src/containers/Editor/LiveEditor/helpers";
  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: 28px;
  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 json = useConfig((state) => state.json);
  123. const updateSetting = useConfig((state) => state.updateSetting);
  124. const { expand, performance, layout } = useConfig((state) => state.settings);
  125. const router = useRouter();
  126. const [uploadVisible, setUploadVisible] = React.useState(false);
  127. const [clearVisible, setClearVisible] = React.useState(false);
  128. const [shareVisible, setShareVisible] = React.useState(false);
  129. const handleSave = () => {
  130. const a = document.createElement("a");
  131. const file = new Blob([json], { type: "text/plain" });
  132. a.href = window.URL.createObjectURL(file);
  133. a.download = "jsonvisio.json";
  134. a.click();
  135. };
  136. const toggleExpandCollapse = () => {
  137. updateSetting("expand", !expand);
  138. toast(`${expand ? "Collapsed" : "Expanded"} nodes.`);
  139. };
  140. const togglePerformance = () => {
  141. const toastMsg = performance
  142. ? "Disabled Performance Mode\nSearch Node & Save Image enabled."
  143. : "Enabled Performance Mode\nSearch Node & Save Image disabled.";
  144. toast(toastMsg, {
  145. icon: <StyledAlertIcon size={36} />,
  146. duration: 3000,
  147. });
  148. updateSetting("performance", !performance);
  149. };
  150. const toggleLayout = () => {
  151. const nextLayout = getNextLayout(layout);
  152. updateSetting("layout", nextLayout);
  153. };
  154. return (
  155. <StyledSidebar>
  156. <StyledTopWrapper>
  157. <Link passHref href="/">
  158. <StyledElement onClick={() => router.push("/")}>
  159. <StyledLogo>
  160. <StyledText>J</StyledText>
  161. <StyledText secondary>V</StyledText>
  162. </StyledLogo>
  163. </StyledElement>
  164. </Link>
  165. <Tooltip title="Import File">
  166. <StyledElement onClick={() => setUploadVisible(true)}>
  167. <AiOutlineFileAdd />
  168. </StyledElement>
  169. </Tooltip>
  170. <Tooltip title="Rotate Layout">
  171. <StyledElement onClick={toggleLayout}>
  172. <StyledFlowIcon rotate={rotateLayout(layout)} />
  173. </StyledElement>
  174. </Tooltip>
  175. <Tooltip title={expand ? "Shrink Nodes" : "Expand Nodes"}>
  176. <StyledElement
  177. title="Toggle Expand/Collapse"
  178. onClick={toggleExpandCollapse}
  179. >
  180. {expand ? <CgArrowsMergeAltH /> : <CgArrowsShrinkH />}
  181. </StyledElement>
  182. </Tooltip>
  183. <Tooltip
  184. title={`${
  185. performance ? "Disable" : "Enable"
  186. } Performance Mode (Beta)`}
  187. >
  188. <StyledElement onClick={togglePerformance} beta>
  189. <CgPerformance color={performance ? "#0073FF" : undefined} />
  190. </StyledElement>
  191. </Tooltip>
  192. <Tooltip title="Save JSON">
  193. <StyledElement onClick={handleSave}>
  194. <AiOutlineSave />
  195. </StyledElement>
  196. </Tooltip>
  197. <Tooltip title="Clear JSON">
  198. <StyledElement onClick={() => setClearVisible(true)}>
  199. <AiOutlineDelete />
  200. </StyledElement>
  201. </Tooltip>
  202. <Tooltip title="Share">
  203. <StyledElement onClick={() => setShareVisible(true)}>
  204. <AiOutlineLink />
  205. </StyledElement>
  206. </Tooltip>
  207. </StyledTopWrapper>
  208. <StyledBottomWrapper>
  209. <StyledElement>
  210. <Link href="https://twitter.com/aykutsarach">
  211. <a aria-label="Twitter" rel="me" target="_blank">
  212. <AiOutlineTwitter />
  213. </a>
  214. </Link>
  215. </StyledElement>
  216. <StyledElement>
  217. <Link href="https://github.com/AykutSarac/jsonvisio.com">
  218. <a aria-label="GitHub" rel="me" target="_blank">
  219. <AiFillGithub />
  220. </a>
  221. </Link>
  222. </StyledElement>
  223. <StyledElement>
  224. <Link href="https://www.patreon.com/aykutsarac">
  225. <a aria-label="Patreon" rel="me" target="_blank">
  226. <RiPatreonFill />
  227. </a>
  228. </Link>
  229. </StyledElement>
  230. </StyledBottomWrapper>
  231. <ImportModal visible={uploadVisible} setVisible={setUploadVisible} />
  232. <ClearModal visible={clearVisible} setVisible={setClearVisible} />
  233. <ShareModal visible={shareVisible} setVisible={setShareVisible} />
  234. </StyledSidebar>
  235. );
  236. };