storyTree.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {ComponentProps} from 'react';
  2. import styled from '@emotion/styled';
  3. import Link from 'sentry/components/links/link';
  4. import {space} from 'sentry/styles/space';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import type {StoriesQuery} from 'sentry/views/stories/types';
  7. type DirContent = Record<string, unknown>;
  8. interface Props extends ComponentProps<'div'> {
  9. files: string[];
  10. }
  11. export default function StoryTree({files, style}: Props) {
  12. const tree = toTree(files);
  13. return (
  14. <nav style={style}>
  15. <FolderContent path="" content={tree} />
  16. </nav>
  17. );
  18. }
  19. function FolderContent({path, content}: {content: DirContent; path: string}) {
  20. const location = useLocation<StoriesQuery>();
  21. const currentFile = location.query.name;
  22. return (
  23. <UnorderedList>
  24. {Object.entries(content).map(([name, children]) => {
  25. const childContent = children as DirContent;
  26. const childPath = toPath(path, name);
  27. if (Object.keys(childContent).length === 0) {
  28. const isCurrent = childPath === currentFile ? true : undefined;
  29. const to = `/stories/?name=${childPath}`;
  30. return (
  31. <ListItem key={name} aria-current={isCurrent}>
  32. <FolderLink to={to}>{name}</FolderLink>
  33. </ListItem>
  34. );
  35. }
  36. return (
  37. <ListItem key={name}>
  38. <Folder open>
  39. <FolderName>{name}</FolderName>
  40. <FolderContent path={childPath} content={childContent} />
  41. </Folder>
  42. </ListItem>
  43. );
  44. })}
  45. </UnorderedList>
  46. );
  47. }
  48. function toTree(files: string[]) {
  49. const root = {};
  50. for (const file of files) {
  51. const parts = file.split('/');
  52. let tree = root;
  53. for (const part of parts) {
  54. if (!(part in tree)) {
  55. tree[part] = {};
  56. }
  57. tree = tree[part];
  58. }
  59. }
  60. return root;
  61. }
  62. function toPath(path: string, name: string) {
  63. return [path, name].filter(Boolean).join('/');
  64. }
  65. const UnorderedList = styled('ul')`
  66. margin: 0;
  67. padding: 0;
  68. list-style: none;
  69. `;
  70. const ListItem = styled('li')`
  71. position: relative;
  72. &[aria-current] {
  73. background: ${p => p.theme.blue300};
  74. color: ${p => p.theme.white};
  75. font-weight: bold;
  76. }
  77. &[aria-current] a:before {
  78. background: ${p => p.theme.blue300};
  79. content: '';
  80. left: -100%;
  81. position: absolute;
  82. right: 0;
  83. top: 0;
  84. z-index: -1;
  85. bottom: 0;
  86. }
  87. `;
  88. const Folder = styled('details')`
  89. cursor: pointer;
  90. padding-left: ${space(1.5)};
  91. position: relative;
  92. &:before {
  93. content: '⏵';
  94. position: absolute;
  95. left: ${space(0.25)};
  96. top: ${space(0.25)};
  97. }
  98. &[open]:before {
  99. content: '⏷';
  100. }
  101. `;
  102. const FolderName = styled('summary')`
  103. padding: ${space(0.25)};
  104. color: inherit;
  105. &:hover {
  106. color: inherit;
  107. }
  108. &:hover:before {
  109. background: ${p => p.theme.blue100};
  110. content: '';
  111. left: -100%;
  112. position: absolute;
  113. right: 0;
  114. top: 0;
  115. z-index: -1;
  116. bottom: 0;
  117. }
  118. `;
  119. const FolderLink = styled(Link)`
  120. display: block;
  121. padding: ${space(0.25)};
  122. padding-left: 14px;
  123. color: inherit;
  124. &:hover {
  125. color: inherit;
  126. }
  127. &:hover:before {
  128. background: ${p => p.theme.blue100};
  129. content: '';
  130. left: -100%;
  131. position: absolute;
  132. right: 0;
  133. top: 0;
  134. z-index: -1;
  135. bottom: 0;
  136. }
  137. `;