hydrateA11yFrame.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. export interface RawA11yResponse {
  2. data: RawA11yFrame[];
  3. }
  4. export interface RawA11yFrame {
  5. elements: A11yIssueElement[];
  6. help: string;
  7. help_url: string;
  8. id: string;
  9. timestamp: number;
  10. impact?: 'minor' | 'moderate' | 'serious' | 'critical';
  11. }
  12. interface A11yIssueElement {
  13. alternatives: A11yIssueElementAlternative[];
  14. element: string;
  15. target: string[];
  16. }
  17. interface A11yIssueElementAlternative {
  18. id: string;
  19. message: string;
  20. }
  21. type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
  22. export type HydratedA11yFrame = Overwrite<
  23. Omit<RawA11yFrame, 'elements' | 'help'>,
  24. {
  25. /**
  26. * For compatibility with Frames, to highlight the element within the replay
  27. */
  28. data: {
  29. element: A11yIssueElement;
  30. label: string;
  31. };
  32. /**
  33. * Rename `help` to conform to ReplayFrame basics.
  34. */
  35. description: string;
  36. /**
  37. * The specific element instance
  38. */
  39. element: A11yIssueElement;
  40. /**
  41. * The difference in timestamp and replay.started_at, in millieseconds
  42. */
  43. offsetMs: number;
  44. /**
  45. * The Date when the a11yIssue happened
  46. */
  47. timestamp: Date;
  48. /**
  49. * Alias of timestamp, in milliseconds
  50. */
  51. timestampMs: number;
  52. }
  53. >;
  54. export default function hydrateA11yFrame(
  55. raw: RawA11yFrame,
  56. startTimestampMs: number
  57. ): HydratedA11yFrame[] {
  58. return raw.elements.map((element): HydratedA11yFrame => {
  59. const timestamp = new Date(raw.timestamp);
  60. const timestampMs = timestamp.getTime();
  61. const elementWithoutIframe = {
  62. ...element,
  63. target: element.target[0] === 'iframe' ? element.target.slice(1) : element.target,
  64. };
  65. return {
  66. data: {
  67. element: elementWithoutIframe,
  68. label: raw.id,
  69. },
  70. description: raw.help,
  71. element: elementWithoutIframe,
  72. help_url: raw.help_url,
  73. id: raw.id,
  74. impact: raw.impact,
  75. offsetMs: timestampMs - startTimestampMs,
  76. timestamp,
  77. timestampMs,
  78. };
  79. });
  80. }