storyTree.tsx 3.5 KB

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