index.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 { CgArrowsMergeAltH, CgArrowsShrinkH } from "react-icons/cg";
  8. import {
  9. AiOutlineDelete,
  10. AiFillGithub,
  11. AiOutlineTwitter,
  12. AiOutlineSave,
  13. AiOutlineFileAdd,
  14. AiOutlineLink,
  15. AiOutlineEdit,
  16. } from "react-icons/ai";
  17. import { Tooltip } from "src/components/Tooltip";
  18. import { useRouter } from "next/router";
  19. import { ImportModal } from "src/containers/Modals/ImportModal";
  20. import { ClearModal } from "src/containers/Modals/ClearModal";
  21. import { ShareModal } from "src/containers/Modals/ShareModal";
  22. import useConfig from "src/hooks/store/useConfig";
  23. import { getNextLayout } from "src/containers/Editor/LiveEditor/helpers";
  24. import { HiHeart } from "react-icons/hi";
  25. import shallow from "zustand/shallow";
  26. import { MdCenterFocusWeak } from "react-icons/md";
  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.div<{ beta?: boolean }>`
  42. position: relative;
  43. display: flex;
  44. justify-content: center;
  45. text-align: center;
  46. font-size: 26px;
  47. font-weight: 600;
  48. width: 100%;
  49. color: ${({ theme }) => theme.INTERACTIVE_NORMAL};
  50. cursor: pointer;
  51. svg {
  52. padding: 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. `;
  62. const StyledText = styled.span<{ secondary?: boolean }>`
  63. color: ${({ theme, secondary }) =>
  64. secondary ? theme.INTERACTIVE_NORMAL : theme.ORANGE};
  65. `;
  66. const StyledFlowIcon = styled(TiFlowMerge)<{ rotate: number }>`
  67. transform: rotate(${({ rotate }) => `${rotate}deg`});
  68. `;
  69. const StyledTopWrapper = styled.nav`
  70. display: flex;
  71. justify-content: space-between;
  72. flex-direction: column;
  73. align-items: center;
  74. width: 100%;
  75. & > div:nth-child(n + 1) {
  76. border-bottom: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  77. }
  78. .mobile {
  79. display: none;
  80. }
  81. @media only screen and (max-width: 768px) {
  82. flex-direction: row;
  83. & > div:nth-child(n + 1) {
  84. border-bottom: none;
  85. }
  86. .mobile {
  87. display: initial;
  88. }
  89. .desktop {
  90. display: none;
  91. }
  92. }
  93. `;
  94. const StyledBottomWrapper = styled.nav`
  95. display: flex;
  96. justify-content: space-between;
  97. flex-direction: column;
  98. align-items: center;
  99. width: 100%;
  100. & > div,
  101. a:nth-child(0) {
  102. border-top: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  103. }
  104. @media only screen and (max-width: 768px) {
  105. display: none;
  106. }
  107. `;
  108. const StyledLogo = styled.div`
  109. color: ${({ theme }) => theme.FULL_WHITE};
  110. `;
  111. function rotateLayout(layout: CanvasDirection) {
  112. if (layout === "LEFT") return 90;
  113. if (layout === "UP") return 180;
  114. if (layout === "RIGHT") return 270;
  115. return 360;
  116. }
  117. export const Sidebar: React.FC = () => {
  118. const getJson = useConfig((state) => state.getJson);
  119. const setConfig = useConfig((state) => state.setConfig);
  120. const centerView = useConfig((state) => state.centerView);
  121. const [uploadVisible, setUploadVisible] = React.useState(false);
  122. const [clearVisible, setClearVisible] = React.useState(false);
  123. const [shareVisible, setShareVisible] = React.useState(false);
  124. const { push } = useRouter();
  125. const [expand, layout, hideEditor] = useConfig(
  126. (state) => [state.expand, state.layout, state.hideEditor],
  127. shallow
  128. );
  129. const handleSave = () => {
  130. const a = document.createElement("a");
  131. const file = new Blob([getJson()], { type: "text/plain" });
  132. a.href = window.URL.createObjectURL(file);
  133. a.download = "jsoncrack.json";
  134. a.click();
  135. };
  136. const toggleExpandCollapse = () => {
  137. setConfig("expand", !expand);
  138. toast(`${expand ? "Collapsed" : "Expanded"} nodes.`);
  139. };
  140. const toggleLayout = () => {
  141. const nextLayout = getNextLayout(layout);
  142. setConfig("layout", nextLayout);
  143. };
  144. return (
  145. <StyledSidebar>
  146. <StyledTopWrapper>
  147. <Link passHref href="/">
  148. <StyledElement onClick={() => push("/")}>
  149. <StyledLogo>
  150. <StyledText>J</StyledText>
  151. <StyledText secondary>C</StyledText>
  152. </StyledLogo>
  153. </StyledElement>
  154. </Link>
  155. <Tooltip className="mobile" title="Edit JSON">
  156. <StyledElement onClick={() => setConfig("hideEditor", !hideEditor)}>
  157. <AiOutlineEdit />
  158. </StyledElement>
  159. </Tooltip>
  160. <Tooltip title="Import File">
  161. <StyledElement onClick={() => setUploadVisible(true)}>
  162. <AiOutlineFileAdd />
  163. </StyledElement>
  164. </Tooltip>
  165. <Tooltip title="Rotate Layout">
  166. <StyledElement onClick={toggleLayout}>
  167. <StyledFlowIcon rotate={rotateLayout(layout)} />
  168. </StyledElement>
  169. </Tooltip>
  170. <Tooltip className="mobile" title="Center View">
  171. <StyledElement onClick={centerView}>
  172. <MdCenterFocusWeak />
  173. </StyledElement>
  174. </Tooltip>
  175. <Tooltip
  176. className="desktop"
  177. title={expand ? "Shrink Nodes" : "Expand Nodes"}
  178. >
  179. <StyledElement
  180. title="Toggle Expand/Collapse"
  181. onClick={toggleExpandCollapse}
  182. >
  183. {expand ? <CgArrowsMergeAltH /> : <CgArrowsShrinkH />}
  184. </StyledElement>
  185. </Tooltip>
  186. <Tooltip className="desktop" title="Save JSON">
  187. <StyledElement onClick={handleSave}>
  188. <AiOutlineSave />
  189. </StyledElement>
  190. </Tooltip>
  191. <Tooltip title="Clear JSON">
  192. <StyledElement onClick={() => setClearVisible(true)}>
  193. <AiOutlineDelete />
  194. </StyledElement>
  195. </Tooltip>
  196. <Tooltip className="desktop" title="Share">
  197. <StyledElement onClick={() => setShareVisible(true)}>
  198. <AiOutlineLink />
  199. </StyledElement>
  200. </Tooltip>
  201. </StyledTopWrapper>
  202. <StyledBottomWrapper>
  203. <StyledElement>
  204. <Link href="https://twitter.com/aykutsarach">
  205. <a aria-label="Twitter" rel="me" target="_blank">
  206. <AiOutlineTwitter />
  207. </a>
  208. </Link>
  209. </StyledElement>
  210. <StyledElement>
  211. <Link href="https://github.com/AykutSarac/jsoncrack.com">
  212. <a aria-label="GitHub" rel="me" target="_blank">
  213. <AiFillGithub />
  214. </a>
  215. </Link>
  216. </StyledElement>
  217. <StyledElement>
  218. <Link href="https://github.com/sponsors/AykutSarac">
  219. <a aria-label="GitHub Sponsors" rel="me" target="_blank">
  220. <HiHeart />
  221. </a>
  222. </Link>
  223. </StyledElement>
  224. </StyledBottomWrapper>
  225. <ImportModal visible={uploadVisible} setVisible={setUploadVisible} />
  226. <ClearModal visible={clearVisible} setVisible={setClearVisible} />
  227. <ShareModal visible={shareVisible} setVisible={setShareVisible} />
  228. </StyledSidebar>
  229. );
  230. };