index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import React from "react";
  2. import Link from "next/link";
  3. import toast from "react-hot-toast";
  4. import {
  5. AiOutlineDelete,
  6. AiFillGithub,
  7. AiOutlineTwitter,
  8. AiOutlineSave,
  9. AiOutlineFileAdd,
  10. AiOutlineLink,
  11. AiOutlineEdit,
  12. } from "react-icons/ai";
  13. import { CgArrowsMergeAltH, CgArrowsShrinkH } from "react-icons/cg";
  14. import { FiDownload } from "react-icons/fi";
  15. import { HiHeart } from "react-icons/hi";
  16. import { MdCenterFocusWeak } from "react-icons/md";
  17. import { TiFlowMerge } from "react-icons/ti";
  18. import { VscCollapseAll, VscExpandAll } from "react-icons/vsc";
  19. import { Tooltip } from "src/components/Tooltip";
  20. import { ClearModal } from "src/containers/Modals/ClearModal";
  21. import { DownloadModal } from "src/containers/Modals/DownloadModal";
  22. import { ImportModal } from "src/containers/Modals/ImportModal";
  23. import { ShareModal } from "src/containers/Modals/ShareModal";
  24. import useConfig from "src/hooks/store/useConfig";
  25. import useGraph from "src/hooks/store/useGraph";
  26. import { getNextLayout } from "src/utils/getNextLayout";
  27. import styled from "styled-components";
  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. @media only screen and (max-width: 768px) {
  39. flex-direction: row;
  40. width: 100%;
  41. }
  42. `;
  43. const StyledElement = styled.button`
  44. position: relative;
  45. display: flex;
  46. justify-content: center;
  47. text-align: center;
  48. font-size: 26px;
  49. font-weight: 600;
  50. width: fit-content;
  51. color: ${({ theme }) => theme.SIDEBAR_ICONS};
  52. cursor: pointer;
  53. svg {
  54. padding: 12px 8px;
  55. vertical-align: middle;
  56. }
  57. a {
  58. display: flex;
  59. }
  60. &:hover :is(a, svg) {
  61. color: ${({ theme }) => theme.INTERACTIVE_HOVER};
  62. }
  63. @media only screen and (max-width: 768px) {
  64. font-size: 22px;
  65. svg {
  66. padding: 8px 4px;
  67. vertical-align: middle;
  68. }
  69. }
  70. `;
  71. const StyledText = styled.span<{ secondary?: boolean }>`
  72. color: ${({ theme, secondary }) =>
  73. secondary ? theme.INTERACTIVE_HOVER : theme.ORANGE};
  74. `;
  75. const StyledFlowIcon = styled(TiFlowMerge)<{ rotate: number }>`
  76. transform: rotate(${({ rotate }) => `${rotate}deg`});
  77. `;
  78. const StyledTopWrapper = styled.nav`
  79. display: flex;
  80. justify-content: space-between;
  81. flex-direction: column;
  82. align-items: center;
  83. width: 100%;
  84. .mobile {
  85. display: none;
  86. }
  87. @media only screen and (max-width: 768px) {
  88. justify-content: space-evenly;
  89. flex-direction: row;
  90. .mobile {
  91. display: initial;
  92. }
  93. .desktop {
  94. display: none;
  95. }
  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. @media only screen and (max-width: 768px) {
  105. display: none;
  106. }
  107. `;
  108. const StyledLogo = styled.a`
  109. color: ${({ theme }) => theme.FULL_WHITE};
  110. padding: 8px 4px;
  111. border-bottom: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  112. @media only screen and (max-width: 768px) {
  113. border-bottom: 0;
  114. }
  115. `;
  116. function rotateLayout(layout: "LEFT" | "RIGHT" | "DOWN" | "UP") {
  117. if (layout === "LEFT") return 90;
  118. if (layout === "UP") return 180;
  119. if (layout === "RIGHT") return 270;
  120. return 360;
  121. }
  122. export const Sidebar: React.FC = () => {
  123. const getJson = useConfig(state => state.getJson);
  124. const setConfig = useConfig(state => state.setConfig);
  125. const centerView = useConfig(state => state.centerView);
  126. const collapseGraph = useGraph(state => state.collapseGraph);
  127. const expandGraph = useGraph(state => state.expandGraph);
  128. const graphCollapsed = useGraph(state => state.graphCollapsed);
  129. const [uploadVisible, setUploadVisible] = React.useState(false);
  130. const [clearVisible, setClearVisible] = React.useState(false);
  131. const [shareVisible, setShareVisible] = React.useState(false);
  132. const [isDownloadVisible, setDownloadVisible] = React.useState(false);
  133. const [foldNodes, layout, hideEditor] = useConfig(
  134. state => [state.foldNodes, state.layout, state.hideEditor],
  135. shallow
  136. );
  137. const handleSave = () => {
  138. const a = document.createElement("a");
  139. const file = new Blob([getJson()], { type: "text/plain" });
  140. a.href = window.URL.createObjectURL(file);
  141. a.download = "jsoncrack.json";
  142. a.click();
  143. };
  144. const toggleFoldNodes = () => {
  145. setConfig("foldNodes", !foldNodes);
  146. toast(`${foldNodes ? "Fold" : "Unfold"} nodes.`);
  147. };
  148. const toggleLayout = () => {
  149. const nextLayout = getNextLayout(layout);
  150. setConfig("layout", nextLayout);
  151. };
  152. const toggleExpandCollapseGraph = () => {
  153. if (graphCollapsed) expandGraph();
  154. else collapseGraph();
  155. toast(`${graphCollapsed ? "Expanded" : "Collapsed"} graph.`);
  156. };
  157. return (
  158. <StyledSidebar>
  159. <StyledTopWrapper>
  160. <Link passHref href="/">
  161. <StyledElement as={StyledLogo}>
  162. <StyledText>J</StyledText>
  163. <StyledText secondary>C</StyledText>
  164. </StyledElement>
  165. </Link>
  166. <Tooltip className="mobile" title="Edit JSON">
  167. <StyledElement onClick={() => setConfig("hideEditor", !hideEditor)}>
  168. <AiOutlineEdit />
  169. </StyledElement>
  170. </Tooltip>
  171. <Tooltip title="Import File">
  172. <StyledElement onClick={() => setUploadVisible(true)}>
  173. <AiOutlineFileAdd />
  174. </StyledElement>
  175. </Tooltip>
  176. <Tooltip title="Rotate Layout">
  177. <StyledElement onClick={toggleLayout}>
  178. <StyledFlowIcon rotate={rotateLayout(layout)} />
  179. </StyledElement>
  180. </Tooltip>
  181. <Tooltip className="mobile" title="Center View">
  182. <StyledElement onClick={centerView}>
  183. <MdCenterFocusWeak />
  184. </StyledElement>
  185. </Tooltip>
  186. <Tooltip
  187. className="desktop"
  188. title={foldNodes ? "Fold Nodes" : "Unfold Nodes"}
  189. >
  190. <StyledElement onClick={toggleFoldNodes}>
  191. {foldNodes ? <CgArrowsMergeAltH /> : <CgArrowsShrinkH />}
  192. </StyledElement>
  193. </Tooltip>
  194. <Tooltip
  195. className="desktop"
  196. title={graphCollapsed ? "Expand Graph" : "Collapse Graph"}
  197. >
  198. <StyledElement onClick={toggleExpandCollapseGraph}>
  199. {graphCollapsed ? <VscExpandAll /> : <VscCollapseAll />}
  200. </StyledElement>
  201. </Tooltip>
  202. <Tooltip className="desktop" title="Save JSON">
  203. <StyledElement onClick={handleSave}>
  204. <AiOutlineSave />
  205. </StyledElement>
  206. </Tooltip>
  207. <Tooltip className="mobile" title="Download Image">
  208. <StyledElement onClick={() => setDownloadVisible(true)}>
  209. <FiDownload />
  210. </StyledElement>
  211. </Tooltip>
  212. <Tooltip title="Clear JSON">
  213. <StyledElement onClick={() => setClearVisible(true)}>
  214. <AiOutlineDelete />
  215. </StyledElement>
  216. </Tooltip>
  217. <Tooltip className="desktop" title="Share">
  218. <StyledElement onClick={() => setShareVisible(true)}>
  219. <AiOutlineLink />
  220. </StyledElement>
  221. </Tooltip>
  222. </StyledTopWrapper>
  223. <StyledBottomWrapper>
  224. <StyledElement>
  225. <Link href="https://twitter.com/jsoncrack">
  226. <a aria-label="Twitter" rel="me" target="_blank">
  227. <AiOutlineTwitter />
  228. </a>
  229. </Link>
  230. </StyledElement>
  231. <StyledElement>
  232. <Link href="https://github.com/AykutSarac/jsoncrack.com">
  233. <a aria-label="GitHub" rel="me" target="_blank">
  234. <AiFillGithub />
  235. </a>
  236. </Link>
  237. </StyledElement>
  238. <StyledElement>
  239. <Link href="https://github.com/sponsors/AykutSarac">
  240. <a aria-label="GitHub Sponsors" rel="me" target="_blank">
  241. <HiHeart />
  242. </a>
  243. </Link>
  244. </StyledElement>
  245. </StyledBottomWrapper>
  246. <ImportModal visible={uploadVisible} setVisible={setUploadVisible} />
  247. <ClearModal visible={clearVisible} setVisible={setClearVisible} />
  248. <ShareModal visible={shareVisible} setVisible={setShareVisible} />
  249. <DownloadModal visible={isDownloadVisible} setVisible={setDownloadVisible} />
  250. </StyledSidebar>
  251. );
  252. };