index.tsx 7.3 KB

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