Skip to content

Commit

Permalink
Minor clean up of EventTooltip.js
Browse files Browse the repository at this point in the history
  • Loading branch information
taneliang committed Aug 6, 2020
1 parent 87eb3b3 commit 25d1626
Showing 1 changed file with 44 additions and 64 deletions.
108 changes: 44 additions & 64 deletions src/EventTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function trimComponentName(name) {
return name;
}

function getReactEventLabel(type) {
function getReactEventLabel(type): string | null {
switch (type) {
case 'schedule-render':
return 'render scheduled';
Expand All @@ -58,7 +58,25 @@ function getReactEventLabel(type) {
}
}

function getReactMeasureLabel(type: string) {
function getReactEventColor(event: ReactEvent): string | null {
switch (event.type) {
case 'schedule-render':
return COLORS.REACT_SCHEDULE_HOVER;
case 'schedule-state-update':
case 'schedule-force-update':
return event.isCascading
? COLORS.REACT_SCHEDULE_CASCADING_HOVER
: COLORS.REACT_SCHEDULE_HOVER;
case 'suspense-suspend':
case 'suspense-resolved':
case 'suspense-rejected':
return COLORS.REACT_SUSPEND_HOVER;
default:
return null;
}
}

function getReactMeasureLabel(type): string | null {
switch (type) {
case 'commit':
return 'commit';
Expand All @@ -85,65 +103,18 @@ export default function EventTooltip({data, hoveredEvent, origin}: Props) {
return null;
}

const {event, flamechartStackFrame, measure, userTimingMark} = hoveredEvent;
const {event, measure, flamechartStackFrame, userTimingMark} = hoveredEvent;

if (event !== null) {
switch (event.type) {
case 'schedule-render':
return (
<TooltipReactEvent
color={COLORS.REACT_SCHEDULE_HOVER}
data={data}
event={event}
tooltipRef={tooltipRef}
/>
);
case 'schedule-state-update': // eslint-disable-line no-case-declarations
case 'schedule-force-update':
const color = event.isCascading
? COLORS.REACT_SCHEDULE_CASCADING_HOVER
: COLORS.REACT_SCHEDULE_HOVER;
return (
<TooltipReactEvent
color={color}
data={data}
event={event}
tooltipRef={tooltipRef}
/>
);
case 'suspense-suspend':
case 'suspense-resolved':
case 'suspense-rejected':
return (
<TooltipReactEvent
color={COLORS.REACT_SUSPEND_HOVER}
data={data}
event={event}
tooltipRef={tooltipRef}
/>
);
default:
console.warn(`Unexpected event type "${event.type}"`);
break;
}
return <TooltipReactEvent event={event} tooltipRef={tooltipRef} />;
} else if (measure !== null) {
switch (measure.type) {
case 'commit':
case 'render-idle':
case 'render':
case 'layout-effects':
case 'passive-effects':
return (
<TooltipReactMeasure
data={data}
measure={measure}
tooltipRef={tooltipRef}
/>
);
default:
console.warn(`Unexpected measure type "${measure.type}"`);
break;
}
return (
<TooltipReactMeasure
data={data}
measure={measure}
tooltipRef={tooltipRef}
/>
);
} else if (flamechartStackFrame !== null) {
return (
<TooltipFlamechartNode
Expand Down Expand Up @@ -210,16 +181,20 @@ const TooltipFlamechartNode = ({
};

const TooltipReactEvent = ({
color,
event,
tooltipRef,
}: {
color: string,
event: ReactEvent,
tooltipRef: Return<typeof useRef>,
}) => {
const {componentName, componentStack, timestamp, type} = event;
const label = getReactEventLabel(type);
const label = getReactEventLabel(event.type);
const color = getReactEventColor(event);
if (!label || !color) {
console.warn(`Unexpected event type "${event.type}"`);
return null;
}

const {componentName, componentStack, timestamp} = event;

return (
<div className={styles.Tooltip} ref={tooltipRef}>
Expand Down Expand Up @@ -255,8 +230,13 @@ const TooltipReactMeasure = ({
measure: ReactMeasure,
tooltipRef: Return<typeof useRef>,
}) => {
const {batchUID, duration, timestamp, type, lanes} = measure;
const label = getReactMeasureLabel(type);
const label = getReactMeasureLabel(measure.type);
if (!label) {
console.warn(`Unexpected measure type "${measure.type}"`);
return null;
}

const {batchUID, duration, timestamp, lanes} = measure;
const [startTime, stopTime] = getBatchRange(batchUID, data);

return (
Expand Down

0 comments on commit 25d1626

Please sign in to comment.