index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import React from "react";
  2. import Link from "next/link";
  3. import toast from "react-hot-toast";
  4. import {
  5. AiOutlineDelete,
  6. AiOutlineSave,
  7. AiOutlineFileAdd,
  8. AiOutlineEdit,
  9. } from "react-icons/ai";
  10. import { CgArrowsMergeAltH, CgArrowsShrinkH } from "react-icons/cg";
  11. import { FiDownload } from "react-icons/fi";
  12. import { MdCenterFocusWeak } from "react-icons/md";
  13. import { TiFlowMerge } from "react-icons/ti";
  14. import {
  15. VscAccount,
  16. VscCloud,
  17. VscCollapseAll,
  18. VscExpandAll,
  19. VscSettingsGear,
  20. } from "react-icons/vsc";
  21. import { Tooltip } from "src/components/Tooltip";
  22. import useGraph from "src/store/useGraph";
  23. import useJson from "src/store/useJson";
  24. import useModal from "src/store/useModal";
  25. import { getNextDirection } from "src/utils/getNextDirection";
  26. import styled from "styled-components";
  27. const StyledSidebar = styled.div`
  28. display: flex;
  29. justify-content: space-between;
  30. flex-direction: column;
  31. align-items: center;
  32. width: fit-content;
  33. background: ${({ theme }) => theme.BACKGROUND_TERTIARY};
  34. padding: 4px;
  35. border-right: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  36. @media only screen and (max-width: 768px) {
  37. flex-direction: row;
  38. width: 100%;
  39. }
  40. `;
  41. const StyledElement = styled.button`
  42. position: relative;
  43. display: flex;
  44. justify-content: center;
  45. text-align: center;
  46. font-size: 24px;
  47. font-weight: 600;
  48. width: fit-content;
  49. color: ${({ theme }) => theme.SIDEBAR_ICONS};
  50. cursor: pointer;
  51. svg {
  52. padding: 12px 8px;
  53. vertical-align: middle;
  54. }
  55. a {
  56. display: flex;
  57. }
  58. &:hover :is(a, svg) {
  59. color: ${({ theme }) => theme.INTERACTIVE_HOVER};
  60. }
  61. @media only screen and (max-width: 768px) {
  62. font-size: 22px;
  63. svg {
  64. padding: 8px 4px;
  65. vertical-align: middle;
  66. }
  67. }
  68. `;
  69. const StyledText = styled.span<{ secondary?: boolean }>`
  70. color: ${({ theme, secondary }) =>
  71. secondary ? theme.INTERACTIVE_HOVER : theme.ORANGE};
  72. `;
  73. const StyledFlowIcon = styled(TiFlowMerge)<{ rotate: number }>`
  74. transform: rotate(${({ rotate }) => `${rotate}deg`});
  75. `;
  76. const StyledTopWrapper = styled.nav`
  77. display: flex;
  78. justify-content: space-between;
  79. flex-direction: column;
  80. align-items: center;
  81. width: 100%;
  82. .mobile {
  83. display: none;
  84. }
  85. @media only screen and (max-width: 768px) {
  86. justify-content: space-evenly;
  87. flex-direction: row;
  88. .mobile {
  89. display: initial;
  90. }
  91. .desktop {
  92. display: none;
  93. }
  94. }
  95. `;
  96. const StyledBottomWrapper = styled.nav`
  97. display: flex;
  98. justify-content: space-between;
  99. flex-direction: column;
  100. align-items: center;
  101. width: 100%;
  102. @media only screen and (max-width: 768px) {
  103. display: none;
  104. }
  105. `;
  106. const StyledLogo = styled.a`
  107. color: ${({ theme }) => theme.FULL_WHITE};
  108. padding: 8px 4px;
  109. border-bottom: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  110. @media only screen and (max-width: 768px) {
  111. border-bottom: 0;
  112. }
  113. `;
  114. function rotateLayout(direction: "LEFT" | "RIGHT" | "DOWN" | "UP") {
  115. if (direction === "LEFT") return 90;
  116. if (direction === "UP") return 180;
  117. if (direction === "RIGHT") return 270;
  118. return 360;
  119. }
  120. export const Sidebar: React.FC = () => {
  121. const setVisible = useModal(state => state.setVisible);
  122. const setDirection = useGraph(state => state.setDirection);
  123. const getJson = useJson(state => state.getJson);
  124. const collapseGraph = useGraph(state => state.collapseGraph);
  125. const expandGraph = useGraph(state => state.expandGraph);
  126. const centerView = useGraph(state => state.centerView);
  127. const toggleFold = useGraph(state => state.toggleFold);
  128. const toggleFullscreen = useGraph(state => state.toggleFullscreen);
  129. const direction = useGraph(state => state.direction);
  130. const foldNodes = useGraph(state => state.foldNodes);
  131. const fullscreen = useGraph(state => state.fullscreen);
  132. const graphCollapsed = useGraph(state => state.graphCollapsed);
  133. const handleSave = () => {
  134. const a = document.createElement("a");
  135. const file = new Blob([getJson()], { type: "text/plain" });
  136. a.href = window.URL.createObjectURL(file);
  137. a.download = "jsoncrack.json";
  138. a.click();
  139. };
  140. const toggleFoldNodes = () => {
  141. toggleFold(!foldNodes);
  142. toast(`${foldNodes ? "Unfolded" : "Folded"} nodes`);
  143. };
  144. const toggleDirection = () => {
  145. const nextDirection = getNextDirection(direction);
  146. setDirection(nextDirection);
  147. };
  148. const toggleExpandCollapseGraph = () => {
  149. if (graphCollapsed) expandGraph();
  150. else collapseGraph();
  151. toast(`${graphCollapsed ? "Expanded" : "Collapsed"} graph.`);
  152. };
  153. return (
  154. <StyledSidebar>
  155. <StyledTopWrapper>
  156. <Link passHref href="/">
  157. <StyledElement as={StyledLogo}>
  158. <StyledText>J</StyledText>
  159. <StyledText secondary>C</StyledText>
  160. </StyledElement>
  161. </Link>
  162. <Tooltip className="mobile" title="Edit JSON">
  163. <StyledElement onClick={() => toggleFullscreen(!fullscreen)}>
  164. <AiOutlineEdit />
  165. </StyledElement>
  166. </Tooltip>
  167. <Tooltip title="Import File">
  168. <StyledElement onClick={() => setVisible("import")(true)}>
  169. <AiOutlineFileAdd />
  170. </StyledElement>
  171. </Tooltip>
  172. <Tooltip title="Rotate Layout">
  173. <StyledElement onClick={toggleDirection}>
  174. <StyledFlowIcon rotate={rotateLayout(direction)} />
  175. </StyledElement>
  176. </Tooltip>
  177. <Tooltip className="mobile" title="Center View">
  178. <StyledElement onClick={centerView}>
  179. <MdCenterFocusWeak />
  180. </StyledElement>
  181. </Tooltip>
  182. <Tooltip
  183. className="desktop"
  184. title={foldNodes ? "Unfold Nodes" : "Fold Nodes"}
  185. >
  186. <StyledElement onClick={toggleFoldNodes}>
  187. {foldNodes ? <CgArrowsShrinkH /> : <CgArrowsMergeAltH />}
  188. </StyledElement>
  189. </Tooltip>
  190. <Tooltip
  191. className="desktop"
  192. title={graphCollapsed ? "Expand Graph" : "Collapse Graph"}
  193. >
  194. <StyledElement onClick={toggleExpandCollapseGraph}>
  195. {graphCollapsed ? <VscExpandAll /> : <VscCollapseAll />}
  196. </StyledElement>
  197. </Tooltip>
  198. <Tooltip className="desktop" title="Download JSON">
  199. <StyledElement onClick={handleSave}>
  200. <AiOutlineSave />
  201. </StyledElement>
  202. </Tooltip>
  203. <Tooltip className="mobile" title="Download Image">
  204. <StyledElement onClick={() => setVisible("download")(true)}>
  205. <FiDownload />
  206. </StyledElement>
  207. </Tooltip>
  208. <Tooltip title="Delete JSON">
  209. <StyledElement onClick={() => setVisible("clear")(true)}>
  210. <AiOutlineDelete />
  211. </StyledElement>
  212. </Tooltip>
  213. <Tooltip className="desktop" title="View Cloud">
  214. <StyledElement onClick={() => setVisible("cloud")(true)}>
  215. <VscCloud />
  216. </StyledElement>
  217. </Tooltip>
  218. </StyledTopWrapper>
  219. <StyledBottomWrapper>
  220. <Tooltip title="Account">
  221. <StyledElement onClick={() => setVisible("account")(true)}>
  222. <VscAccount />
  223. </StyledElement>
  224. </Tooltip>
  225. <Tooltip title="Settings">
  226. <StyledElement onClick={() => setVisible("settings")(true)}>
  227. <VscSettingsGear />
  228. </StyledElement>
  229. </Tooltip>
  230. </StyledBottomWrapper>
  231. </StyledSidebar>
  232. );
  233. };