storyTree.tsx 3.6 KB

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