utils.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type {Location} from 'history';
  2. import type {GridColumnOrder} from 'sentry/components/gridEditable';
  3. import SortLink from 'sentry/components/gridEditable/sortLink';
  4. import {t} from 'sentry/locale';
  5. import type {SelectValue} from 'sentry/types/core';
  6. import {defined} from 'sentry/utils';
  7. import {
  8. isContinuousProfileReference,
  9. isTransactionProfileReference,
  10. } from 'sentry/utils/profiling/guards/profile';
  11. import {decodeScalar} from 'sentry/utils/queryString';
  12. type ColorEncoding =
  13. | 'version' // this will use a concatenation of `app_version_name` and `app_version`
  14. | 'device_manufacturer'
  15. | 'device_model'
  16. | 'device_os_version'
  17. | 'transaction_name';
  18. const COLOR_ENCODING_LABELS: Record<ColorEncoding, string> = {
  19. version: t('App Version'),
  20. device_manufacturer: t('Device Manufacturer'),
  21. device_model: t('Device Model'),
  22. device_os_version: t('Device Os Version'),
  23. transaction_name: t('Transaction Name'),
  24. };
  25. export const COLOR_ENCODINGS: SelectValue<ColorEncoding>[] = Object.entries(
  26. COLOR_ENCODING_LABELS
  27. ).map(([value, label]) => ({label, value: value as ColorEncoding}));
  28. export function getColorEncodingFromLocation(location: Location): ColorEncoding {
  29. const colorCoding = decodeScalar(location.query.colorEncoding);
  30. if (defined(colorCoding) && COLOR_ENCODING_LABELS.hasOwnProperty(colorCoding)) {
  31. return colorCoding as ColorEncoding;
  32. }
  33. return 'transaction_name';
  34. }
  35. export function requestAnimationFrameTimeout(cb: () => void, timeout: number) {
  36. const rafId = {current: 0};
  37. const start = performance.now();
  38. function timer() {
  39. if (rafId.current) {
  40. window.cancelAnimationFrame(rafId.current);
  41. }
  42. if (performance.now() - start > timeout) {
  43. cb();
  44. return;
  45. }
  46. rafId.current = window.requestAnimationFrame(timer);
  47. }
  48. rafId.current = window.requestAnimationFrame(timer);
  49. return rafId;
  50. }
  51. export function renderTableHeader<K>(rightAlignedColumns: Set<K>) {
  52. return function (column: GridColumnOrder<K>, _columnIndex: number) {
  53. return (
  54. <SortLink
  55. align={rightAlignedColumns.has(column.key) ? 'right' : 'left'}
  56. title={column.name}
  57. direction={undefined}
  58. canSort={false}
  59. generateSortLink={() => undefined}
  60. />
  61. );
  62. };
  63. }
  64. export const DEFAULT_PROFILING_DATETIME_SELECTION = {
  65. start: null,
  66. end: null,
  67. utc: false,
  68. period: '24h',
  69. };
  70. export function getProfileTargetId(reference: Profiling.BaseProfileReference): string {
  71. if (isTransactionProfileReference(reference)) {
  72. return reference.profile_id;
  73. }
  74. if (isContinuousProfileReference(reference)) {
  75. return reference.profiler_id;
  76. }
  77. return reference;
  78. }