2021-01-08 01:15:34 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-02-24 22:39:51 +01:00
|
|
|
import { ActionEntry } from '../../../server/trace/viewer/traceModel';
|
2021-01-08 01:15:34 +01:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import { useAsyncMemo } from './helpers';
|
|
|
|
|
import './sourceTab.css';
|
2021-01-28 18:33:20 +01:00
|
|
|
import '../../../third_party/highlightjs/highlightjs/tomorrow.css';
|
2021-02-11 03:52:28 +01:00
|
|
|
import { StackFrame } from '../../../common/types';
|
2021-03-11 20:22:59 +01:00
|
|
|
import { Source as SourceView } from '../../components/source';
|
|
|
|
|
import { StackTraceView } from './stackTrace';
|
|
|
|
|
import { SplitView } from '../../components/splitView';
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2021-01-19 23:45:26 +01:00
|
|
|
type StackInfo = string | {
|
2021-02-11 03:52:28 +01:00
|
|
|
frames: StackFrame[];
|
2021-01-19 23:45:26 +01:00
|
|
|
fileContent: Map<string, string>;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-08 01:15:34 +01:00
|
|
|
export const SourceTab: React.FunctionComponent<{
|
|
|
|
|
actionEntry: ActionEntry | undefined,
|
|
|
|
|
}> = ({ actionEntry }) => {
|
2021-01-19 23:45:26 +01:00
|
|
|
const [lastAction, setLastAction] = React.useState<ActionEntry | undefined>();
|
|
|
|
|
const [selectedFrame, setSelectedFrame] = React.useState<number>(0);
|
|
|
|
|
const [needReveal, setNeedReveal] = React.useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
if (lastAction !== actionEntry) {
|
|
|
|
|
setLastAction(actionEntry);
|
|
|
|
|
setSelectedFrame(0);
|
|
|
|
|
setNeedReveal(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stackInfo = React.useMemo<StackInfo>(() => {
|
2021-01-08 01:15:34 +01:00
|
|
|
if (!actionEntry)
|
2021-01-19 23:45:26 +01:00
|
|
|
return '';
|
2021-03-10 20:43:26 +01:00
|
|
|
const { metadata } = actionEntry;
|
|
|
|
|
if (!metadata.stack)
|
2021-01-19 23:45:26 +01:00
|
|
|
return '';
|
2021-03-10 20:43:26 +01:00
|
|
|
const frames = metadata.stack;
|
2021-02-11 03:52:28 +01:00
|
|
|
return {
|
|
|
|
|
frames,
|
2021-01-19 23:45:26 +01:00
|
|
|
fileContent: new Map(),
|
|
|
|
|
};
|
2021-01-08 01:15:34 +01:00
|
|
|
}, [actionEntry]);
|
|
|
|
|
|
2021-03-11 20:22:59 +01:00
|
|
|
const content = useAsyncMemo<string>(async () => {
|
2021-01-19 23:45:26 +01:00
|
|
|
let value: string;
|
|
|
|
|
if (typeof stackInfo === 'string') {
|
|
|
|
|
value = stackInfo;
|
|
|
|
|
} else {
|
2021-02-11 03:52:28 +01:00
|
|
|
const filePath = stackInfo.frames[selectedFrame].file;
|
2021-01-19 23:45:26 +01:00
|
|
|
if (!stackInfo.fileContent.has(filePath))
|
2021-01-28 04:42:51 +01:00
|
|
|
stackInfo.fileContent.set(filePath, await fetch(`/file?${filePath}`).then(response => response.text()).catch(e => `<Unable to read "${filePath}">`));
|
2021-01-19 23:45:26 +01:00
|
|
|
value = stackInfo.fileContent.get(filePath)!;
|
|
|
|
|
}
|
2021-03-11 20:22:59 +01:00
|
|
|
return value;
|
|
|
|
|
}, [stackInfo, selectedFrame], '');
|
2021-01-19 23:45:26 +01:00
|
|
|
|
2021-04-23 18:28:18 +02:00
|
|
|
const targetLine = typeof stackInfo === 'string' ? 0 : stackInfo.frames[selectedFrame]?.line || 0;
|
2021-01-08 01:15:34 +01:00
|
|
|
|
|
|
|
|
const targetLineRef = React.createRef<HTMLDivElement>();
|
|
|
|
|
React.useLayoutEffect(() => {
|
2021-01-19 23:45:26 +01:00
|
|
|
if (needReveal && targetLineRef.current) {
|
2021-01-08 01:15:34 +01:00
|
|
|
targetLineRef.current.scrollIntoView({ block: 'center', inline: 'nearest' });
|
2021-01-19 23:45:26 +01:00
|
|
|
setNeedReveal(false);
|
|
|
|
|
}
|
|
|
|
|
}, [needReveal, targetLineRef]);
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2021-04-20 04:50:11 +02:00
|
|
|
return <SplitView sidebarSize={100} orientation='vertical'>
|
2021-03-11 20:22:59 +01:00
|
|
|
<SourceView text={content} language='javascript' highlight={[{ line: targetLine, type: 'running' }]} revealLine={targetLine}></SourceView>
|
|
|
|
|
<StackTraceView actionEntry={actionEntry} selectedFrame={selectedFrame} setSelectedFrame={setSelectedFrame}></StackTraceView>
|
|
|
|
|
</SplitView>;
|
2021-01-08 01:15:34 +01:00
|
|
|
};
|