utils.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {ReplayClickElement} from 'sentry/views/replays/types';
  2. export function filterItems<I extends object, K extends string>({
  3. filterFns,
  4. filterVals,
  5. items,
  6. }: {
  7. filterFns: Record<K, (item: I, val: any) => boolean>;
  8. filterVals: Record<K, any>;
  9. items: undefined | I[];
  10. }): I[] {
  11. return (
  12. items?.filter(item => {
  13. for (const key in filterFns) {
  14. const filter = filterFns[key];
  15. const val = filterVals[key];
  16. if (!filter(item, val)) {
  17. return false;
  18. }
  19. }
  20. return true;
  21. }) || []
  22. );
  23. }
  24. export function operationName(op: string) {
  25. return op.split('.')?.[1] ?? op;
  26. }
  27. export function getAriaLabel(str: string) {
  28. const matches = str.match(/\[aria=(.*?)\]/g);
  29. if (!matches) {
  30. return '';
  31. }
  32. const pre = matches[0];
  33. return matches[0].substring(pre.indexOf('aria="') + 6, pre.lastIndexOf('"]'));
  34. }
  35. function trimAttribute(elementAttribute, fullAlltribute) {
  36. return elementAttribute === '' ? '' : fullAlltribute;
  37. }
  38. export function constructSelector(element: ReplayClickElement) {
  39. const fullAlt = '[alt="' + element.alt + '"]';
  40. const alt = trimAttribute(element.alt, fullAlt);
  41. const fullAriaLabel = '[aria="' + element.aria_label + '"]';
  42. const ariaLabel = trimAttribute(element.aria_label, fullAriaLabel);
  43. const trimClass = element.class.filter(e => e !== '');
  44. const classWithPeriod = trimClass.join('.');
  45. const classNoPeriod = classWithPeriod.replace('.', '');
  46. const classes = trimAttribute(classNoPeriod, '.' + classWithPeriod);
  47. const id = trimAttribute(element.id, '#' + element.id);
  48. const fullRole = '[role="' + element.role + '"]';
  49. const role = trimAttribute(element.role, fullRole);
  50. const tag = element.tag;
  51. const fullTestId = '[data-test-id="' + element.testid + '"]';
  52. const testId = trimAttribute(element.testid, fullTestId);
  53. const fullTitle = '[title="' + element.title + '"]';
  54. const title = trimAttribute(element.title, fullTitle);
  55. const fullSelector =
  56. tag + id + classes + fullRole + fullAriaLabel + fullTestId + fullAlt + fullTitle;
  57. const selector = tag + id + classes + role + ariaLabel + testId + alt + title;
  58. return {fullSelector, selector};
  59. }