index.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {useRef, useState} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Input from 'sentry/components/input';
  5. import {space} from 'sentry/styles/space';
  6. import {useHotkeys} from 'sentry/utils/useHotkeys';
  7. import OrganizationContainer from 'sentry/views/organizationContainer';
  8. import EmptyStory from 'sentry/views/stories/emptyStory';
  9. import ErrorStory from 'sentry/views/stories/errorStory';
  10. import storiesContext from 'sentry/views/stories/storiesContext';
  11. import StoryFile from 'sentry/views/stories/storyFile';
  12. import StoryHeader from 'sentry/views/stories/storyHeader';
  13. import StoryTree from 'sentry/views/stories/storyTree';
  14. import type {StoriesQuery} from 'sentry/views/stories/types';
  15. import useStoriesLoader from 'sentry/views/stories/useStoriesLoader';
  16. type Props = RouteComponentProps<{}, {}, any, StoriesQuery>;
  17. export default function Stories({location}: Props) {
  18. const story = useStoriesLoader({filename: location.query.name});
  19. const [searchTerm, setSearchTerm] = useState('');
  20. const searchInput = useRef<HTMLInputElement>(null);
  21. useHotkeys([{match: '/', callback: () => searchInput.current?.focus()}], []);
  22. return (
  23. <OrganizationContainer>
  24. <Layout>
  25. <StoryHeader style={{gridArea: 'head'}} />
  26. <Sidebar style={{gridArea: 'aside'}}>
  27. <Input
  28. ref={searchInput}
  29. placeholder="Search files by name"
  30. onChange={e => setSearchTerm(e.target.value.toLowerCase())}
  31. />
  32. <TreeContainer>
  33. <StoryTree
  34. files={storiesContext()
  35. .files()
  36. .filter(s => s.toLowerCase().includes(searchTerm))}
  37. />
  38. </TreeContainer>
  39. </Sidebar>
  40. {story.error ? (
  41. <VerticalScroll style={{gridArea: 'body'}}>
  42. <ErrorStory error={story.error} />
  43. </VerticalScroll>
  44. ) : story.resolved ? (
  45. <Main style={{gridArea: 'body'}}>
  46. <StoryFile filename={story.filename} resolved={story.resolved} />
  47. </Main>
  48. ) : (
  49. <VerticalScroll style={{gridArea: 'body'}}>
  50. <EmptyStory />
  51. </VerticalScroll>
  52. )}
  53. </Layout>
  54. </OrganizationContainer>
  55. );
  56. }
  57. const Layout = styled('div')`
  58. --stories-grid-space: ${space(2)};
  59. display: grid;
  60. grid-template:
  61. 'head head' max-content
  62. 'aside body' auto/ ${p => p.theme.settings.sidebarWidth} 1fr;
  63. gap: var(--stories-grid-space);
  64. place-items: stretch;
  65. height: 100vh;
  66. padding: var(--stories-grid-space);
  67. `;
  68. const Sidebar = styled('aside')`
  69. display: flex;
  70. gap: ${space(2)};
  71. flex-direction: column;
  72. min-height: 0;
  73. `;
  74. const TreeContainer = styled('div')`
  75. overflow: scroll;
  76. flex-grow: 1;
  77. `;
  78. const VerticalScroll = styled('main')`
  79. overflow-x: hidden;
  80. overflow-y: scroll;
  81. `;
  82. /**
  83. * Avoid <Panel> here because nested panels will have a modified theme.
  84. * Therefore stories will look different in prod.
  85. */
  86. const Main = styled(VerticalScroll)`
  87. background: ${p => p.theme.background};
  88. border-radius: ${p => p.theme.panelBorderRadius};
  89. border: 1px solid ${p => p.theme.border};
  90. padding: var(--stories-grid-space);
  91. overflow-x: hidden;
  92. overflow-y: scroll;
  93. position: relative;
  94. `;