tabbedCodeSnippets.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {CodeSnippet} from 'sentry/components/codeSnippet';
  4. import {space} from 'sentry/styles/space';
  5. export interface CodeSnippetTab {
  6. code: string;
  7. label: string;
  8. language: string;
  9. value: string;
  10. filename?: string;
  11. }
  12. interface TabbedCodeSnippetProps {
  13. /**
  14. * An array of tabs to be displayed
  15. */
  16. tabs: CodeSnippetTab[];
  17. }
  18. export function TabbedCodeSnippet({tabs}: TabbedCodeSnippetProps) {
  19. const [selectedTabValue, setSelectedTabValue] = useState(tabs[0].value);
  20. const selectedTab = tabs.find(tab => tab.value === selectedTabValue) ?? tabs[0];
  21. const {code, language, filename} = selectedTab;
  22. return (
  23. <StyledCodeSnippet
  24. tabs={tabs}
  25. language={language}
  26. hideCopyButton
  27. selectedTab={selectedTabValue}
  28. onTabClick={value => setSelectedTabValue(value)}
  29. filename={filename}
  30. >
  31. {code}
  32. </StyledCodeSnippet>
  33. );
  34. }
  35. const StyledCodeSnippet = styled(CodeSnippet)`
  36. pre {
  37. height: 213px;
  38. }
  39. margin-top: ${space(1)};
  40. `;