index.tsx 7.2 KB

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