querySymbol.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {forwardRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import {space} from 'sentry/styles/space';
  4. const indexToChar = 'abcdefghijklmnopqrstuvwxyz';
  5. export const getQuerySymbol = (index: number) => {
  6. let result = '';
  7. let i = index;
  8. do {
  9. result = indexToChar[i % indexToChar.length] + result;
  10. i = Math.floor(i / indexToChar.length) - 1;
  11. } while (i >= 0);
  12. return result;
  13. };
  14. export const Symbol = styled('span')<{isHidden?: boolean}>`
  15. display: flex;
  16. width: 38px;
  17. height: 38px;
  18. line-height: 16px;
  19. padding: ${space(0.5)};
  20. justify-content: center;
  21. align-items: center;
  22. flex-shrink: 0;
  23. border-radius: ${p => p.theme.borderRadius};
  24. ${p => p.theme.fontWeightNormal};
  25. color: ${p => p.theme.white};
  26. font-size: 14px;
  27. background: ${p => p.theme.purple300};
  28. ${p =>
  29. p.isHidden &&
  30. `
  31. background: ${p.theme.background};
  32. color: ${p.theme.textColor};
  33. border: 1px solid ${p.theme.border};
  34. `}
  35. `;
  36. interface QuerySymbolProps extends React.ComponentProps<typeof Symbol> {
  37. queryId: number;
  38. }
  39. export const QuerySymbol = forwardRef<HTMLSpanElement, QuerySymbolProps>(
  40. function QuerySymbol({queryId, ...props}, ref) {
  41. if (queryId < 0) {
  42. return null;
  43. }
  44. return (
  45. <Symbol ref={ref} {...props}>
  46. <span>{getQuerySymbol(queryId)}</span>
  47. </Symbol>
  48. );
  49. }
  50. );