querySymbol.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const Symbol = styled('span')<{isHidden?: boolean; isSelected?: boolean}>`
  15. display: flex;
  16. width: 16px;
  17. height: 16px;
  18. line-height: 16px;
  19. padding: ${space(0.5)};
  20. justify-content: center;
  21. align-items: center;
  22. flex-shrink: 0;
  23. border-radius: 50%;
  24. font-weight: 500;
  25. color: ${p => p.theme.black};
  26. font-size: 11px;
  27. background: ${p => p.theme.yellow300};
  28. ${p =>
  29. p.isSelected &&
  30. !p.isHidden &&
  31. `
  32. background: ${p.theme.purple300};
  33. color: ${p.theme.white};
  34. `}
  35. ${p =>
  36. p.isHidden &&
  37. `
  38. background: ${p.theme.gray300};
  39. color: ${p.theme.white};
  40. `}
  41. `;
  42. interface QuerySymbolProps extends React.ComponentProps<typeof Symbol> {
  43. queryId: number;
  44. }
  45. export const QuerySymbol = forwardRef<HTMLSpanElement, QuerySymbolProps>(
  46. function QuerySymbol({queryId, ...props}, ref) {
  47. if (queryId < 0) {
  48. return null;
  49. }
  50. return (
  51. <Symbol ref={ref} {...props}>
  52. <span>{getQuerySymbol(queryId)}</span>
  53. </Symbol>
  54. );
  55. }
  56. );