querySymbol.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. `;
  46. interface QuerySymbolProps extends React.ComponentProps<typeof Symbol> {
  47. queryId: number;
  48. }
  49. export const QuerySymbol = forwardRef<HTMLSpanElement, QuerySymbolProps>(
  50. function QuerySymbol({queryId, ...props}, ref) {
  51. const organization = useOrganization();
  52. if (queryId < 0) {
  53. return null;
  54. }
  55. if (hasMetricsNewInputs(organization)) {
  56. return (
  57. <Symbol ref={ref} {...props}>
  58. <span>{getQuerySymbol(queryId, true)}</span>
  59. </Symbol>
  60. );
  61. }
  62. return (
  63. <DeprecatedSymbol ref={ref} {...props}>
  64. <span>{getQuerySymbol(queryId, false)}</span>
  65. </DeprecatedSymbol>
  66. );
  67. }
  68. );