import * as React from 'react';
import { OutputDescriptor } from 'tsp-typescript-client/lib/models/output-descriptor';
import { ITspClient } from 'tsp-typescript-client/lib/protocol/tsp-client';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faBars, faSpinner, faThumbtack, faTimes } from '@fortawesome/free-solid-svg-icons';
import { TimeGraphUnitController } from 'timeline-chart/lib/time-graph-unit-controller';
import { TimeRange } from 'traceviewer-base/lib/utils/time-range';
import { OutputComponentStyle } from './utils/output-component-style';
import { OutputStyleModel } from 'tsp-typescript-client/lib/models/styles';
import { TooltipComponent } from './tooltip-component';
import { ResponseStatus } from 'tsp-typescript-client/lib/models/response/responses';
import { signalManager } from 'traceviewer-base/lib/signals/signal-manager';
import { DropDownComponent, DropDownSubSection, OptionState } from './drop-down-component';
export interface AbstractOutputProps {
tspClient: ITspClient;
traceId: string;
traceName?: string;
range: TimeRange;
nbEvents: number;
viewRange: TimeRange;
selectionRange: TimeRange | undefined;
resolution?: number;
outputDescriptor: OutputDescriptor;
markerCategories: string[] | undefined;
markerSetId: string;
style: OutputComponentStyle;
outputWidth: number;
backgroundTheme: string;
onOutputRemove: (outputId: string) => void;
// TODO Not sure
unitController: TimeGraphUnitController;
onSelectionRangeChange?: () => void;
onViewRangeChange?: () => void;
className?: string;
onMouseUp?: VoidFunction;
onMouseDown?: VoidFunction;
onTouchStart?: VoidFunction;
onTouchEnd?: VoidFunction;
setChartOffset?: (chartOffset: number) => void;
pinned?: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
persistChartState?: any;
children?: string;
}
export interface AbstractOutputState {
outputStatus: string;
styleModel?: OutputStyleModel;
dropDownOpen?: boolean;
tooltipContent?: React.ReactNode;
tooltipVisible?: boolean;
}
export abstract class AbstractOutputComponent<
P extends AbstractOutputProps,
S extends AbstractOutputState
> extends React.Component
{
private readonly DEFAULT_HANDLE_WIDTH = 30;
protected mainOutputContainer: React.RefObject;
protected dropDownMenuRef: React.RefObject;
private titleRef: React.RefObject;
private titleBarLabelRef: React.RefObject;
public readonly PIN_VIEW_LABEL = 'Pin View';
public readonly UNPIN_VIEW_LABEL = 'Unpin View';
public readonly TOGGLE_COLUMN_LABEL = 'Toggle Columns';
private dropDownOptions: OptionState[] = [];
constructor(props: P) {
super(props);
this.mainOutputContainer = React.createRef();
this.closeComponent = this.closeComponent.bind(this);
this.renderTitleBar = this.renderTitleBar.bind(this);
this.toggleDropDown = this.toggleDropDown.bind(this);
this.openDropDown = this.openDropDown.bind(this);
this.closeDropDown = this.closeDropDown.bind(this);
this.dropDownMenuRef = React.createRef();
this.titleRef = React.createRef();
this.titleBarLabelRef = React.createRef();
}
render(): JSX.Element {
return (
this.closeTooltip()}
>
{this.renderTitleBar()}
{this.renderMainOutputContainer()}
{this.props.children}
);
}
private renderTitleBar(): React.ReactNode {
const outputName = this.getOutputComponentName();
const outputTooltip = this.getTitleBarTooltip();
// Manually check overflown titles to fix ellipsis not shown on Safari (issue #764).
const isOverflown = () => {
const el = this.titleRef.current;
const labelHeight = this.titleBarLabelRef.current;
if (!el || !labelHeight) {
return false;
}
return el.getBoundingClientRect().height > labelHeight.getBoundingClientRect().height;
};
const titleOverflown = this.titleRef.current && isOverflown() ? 'custom-ellipsis' : 'hidden-ellipsis';
return (
{this.showViewOptionsCondition() && (
)}
this.setFocus()}
>
{outputName}
...
{this.props.pinned === true && (
)}
);
}
private showViewOptionsCondition(): boolean {
const nonPinViewOptionExists = this.dropDownOptions.some(
option => option.label !== this.PIN_VIEW_LABEL && option.label !== this.UNPIN_VIEW_LABEL
);
return nonPinViewOptionExists || this.props.pinned !== false;
}
protected toggleDropDown(): void {
if (this.state.dropDownOpen) {
this.closeDropDown();
} else {
this.openDropDown();
}
}
protected closeDropDown(): void {
this.setState({
dropDownOpen: false
});
}
protected openDropDown(): void {
this.setState({
dropDownOpen: true
});
}
private closeComponent() {
if (this.props.pinned) {
signalManager().emit('UNPIN_VIEW', this.props.outputDescriptor);
}
this.props.onOutputRemove(this.props.outputDescriptor.id);
}
protected getHandleWidth(): number {
return this.props.style.handleWidth || this.DEFAULT_HANDLE_WIDTH;
}
public getMainAreaWidth(): number {
return this.props.outputWidth - this.getHandleWidth();
}
private renderMainOutputContainer(): React.ReactNode {
if (this.state.outputStatus === ResponseStatus.FAILED) {
return this.renderAnalysisFailed();
}
if (this.state.outputStatus === ResponseStatus.COMPLETED && this.resultsAreEmpty()) {
return this.renderEmptyResults();
}
return this.renderMainArea();
}
abstract setFocus(): void;
abstract renderMainArea(): React.ReactNode;
abstract resultsAreEmpty(): boolean;
protected addPinViewOptions(arg?: () => unknown): void {
this.addOptions(
this.PIN_VIEW_LABEL,
() => this.pinView(),
arg,
() => this.props.pinned === undefined
);
this.addOptions(
this.UNPIN_VIEW_LABEL,
() => this.unPinView(),
arg,
() => this.props.pinned === true
);
}
protected addOptions(
label: string,
onClick?: (arg?: unknown) => void,
arg?: unknown,
condition?: () => boolean,
subSection?: DropDownSubSection
): void {
const newOption = {
label: label,
onClick: onClick,
arg: arg,
condition: condition,
subSection: subSection
} as OptionState;
if (this.dropDownOptions.includes(newOption)) {
return;
}
this.dropDownOptions = [...(this.dropDownOptions ?? []), newOption];
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
protected pinView(payload?: any): void {
signalManager().emit('PIN_VIEW', this.props.outputDescriptor, payload);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
protected unPinView(payload?: any): void {
if (payload) {
signalManager().emit('UNPIN_VIEW', this.props.outputDescriptor, payload);
} else {
signalManager().emit('UNPIN_VIEW', this.props.outputDescriptor);
}
}
protected renderAnalysisFailed(): React.ReactElement {
return (
<>
Trace analysis failed.
>
);
}
protected renderEmptyResults(): React.ReactElement {
return (
<>
Trace analysis complete.
No results: Trace missing required events.
>
);
}
protected getOutputComponentDomId(): string {
return this.props.traceId + this.props.outputDescriptor.id;
}
protected getOutputComponentName(): string {
return this.props.outputDescriptor.name;
}
protected getTitleBarTooltip(): string {
return this.props.outputDescriptor.name;
}
protected setTooltipContent(content: React.ReactNode): void {
this.setState({ tooltipContent: content, tooltipVisible: true });
}
protected closeTooltip(): void {
this.setState({ tooltipVisible: false });
}
}