index.tsx 6.9 KB

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