querySymbol.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {forwardRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import {space} from 'sentry/styles/space';
  4. import {hasMetricsNewInputs} from 'sentry/utils/metrics/features';
  5. import useOrganization from 'sentry/utils/useOrganization';
  6. const indexToChar = 'abcdefghijklmnopqrstuvwxyz';
  7. export const getQuerySymbol = (index: number, uppercaseChar?: boolean) => {
  8. let result = '';
  9. let i = index;
  10. do {
  11. result = indexToChar[i % indexToChar.length] + result;
  12. i = Math.floor(i / indexToChar.length) - 1;
  13. } while (i >= 0);
  14. return uppercaseChar ? result.toUpperCase() : result;
  15. };
  16. export const DeprecatedSymbol = styled('span')<{
  17. isHidden?: boolean;
  18. }>`
  19. display: flex;
  20. width: 38px;
  21. height: 38px;
  22. line-height: 16px;
  23. padding: ${space(0.5)};
  24. justify-content: center;
  25. align-items: center;
  26. flex-shrink: 0;
  27. border-radius: ${p => p.theme.borderRadius};
  28. ${p => p.theme.fontWeightNormal};
  29. color: ${p => p.theme.white};
  30. font-size: 14px;
  31. background: ${p => p.theme.purple300};
  32. ${p =>
  33. p.isHidden &&
  34. `
  35. background: ${p.theme.background};
  36. color: ${p.theme.textColor};
  37. border: 1px solid ${p.theme.border};
  38. `}
  39. `;
  40. export const Symbol = styled(DeprecatedSymbol)`
  41. color: ${p => p.theme.purple300};
  42. border: 1px solid ${p => p.theme.purple200};
  43. background: ${p => p.theme.purple100};
  44. font-weight: 600;
  45. ${p => p.isHidden && 'opacity: 0.5;'}
  46. `;
  47. interface QuerySymbolProps extends React.ComponentProps<typeof Symbol> {
  48. queryId: number;
  49. }
  50. export const QuerySymbol = forwardRef<HTMLSpanElement, QuerySymbolProps>(
  51. function QuerySymbol({queryId, ...props}, ref) {
  52. const organization = useOrganization();
  53. if (queryId < 0) {
  54. return null;
  55. }
  56. if (hasMetricsNewInputs(organization)) {
  57. return (
  58. <Symbol ref={ref} {...props}>
  59. <span>{getQuerySymbol(queryId, true)}</span>
  60. </Symbol>
  61. );
  62. }
  63. return (
  64. <DeprecatedSymbol ref={ref} {...props}>
  65. <span>{getQuerySymbol(queryId, false)}</span>
  66. </DeprecatedSymbol>
  67. );
  68. }
  69. );