2019-08-28 01:54:01 +08:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
*
|
|
|
|
|
* @flow
|
|
|
|
|
*/
|
2019-04-01 08:02:30 +08:00
|
|
|
|
2020-02-22 11:45:20 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import {Fragment, useContext, useCallback, useRef} from 'react';
|
2019-08-14 08:58:03 +08:00
|
|
|
import {ProfilerContext} from './ProfilerContext';
|
|
|
|
|
import {ModalDialogContext} from '../ModalDialog';
|
2019-04-01 08:02:30 +08:00
|
|
|
import Button from '../Button';
|
|
|
|
|
import ButtonIcon from '../ButtonIcon';
|
2019-08-14 08:58:03 +08:00
|
|
|
import {StoreContext} from '../context';
|
2019-04-30 18:16:52 +08:00
|
|
|
import {
|
2019-05-22 21:05:25 +08:00
|
|
|
prepareProfilingDataExport,
|
|
|
|
|
prepareProfilingDataFrontendFromExport,
|
2019-04-30 18:16:52 +08:00
|
|
|
} from './utils';
|
2019-08-14 08:58:03 +08:00
|
|
|
import {downloadFile} from '../utils';
|
2019-04-01 08:02:30 +08:00
|
|
|
|
|
|
|
|
import styles from './ProfilingImportExportButtons.css';
|
|
|
|
|
|
2019-08-14 08:58:03 +08:00
|
|
|
import type {ProfilingDataExport} from './types';
|
2019-05-22 21:05:25 +08:00
|
|
|
|
2019-04-01 08:02:30 +08:00
|
|
|
export default function ProfilingImportExportButtons() {
|
2019-08-14 08:58:03 +08:00
|
|
|
const {isProfiling, profilingData, rootID} = useContext(ProfilerContext);
|
2019-04-01 08:02:30 +08:00
|
|
|
const store = useContext(StoreContext);
|
2019-08-14 08:58:03 +08:00
|
|
|
const {profilerStore} = store;
|
2019-04-01 08:02:30 +08:00
|
|
|
|
|
|
|
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
2019-09-03 23:35:12 +08:00
|
|
|
const downloadRef = useRef<HTMLAnchorElement | null>(null);
|
2019-04-01 08:02:30 +08:00
|
|
|
|
2019-08-14 08:58:03 +08:00
|
|
|
const {dispatch: modalDialogDispatch} = useContext(ModalDialogContext);
|
2019-05-08 05:38:11 +08:00
|
|
|
|
2020-01-09 21:54:11 +08:00
|
|
|
const downloadData = useCallback(() => {
|
|
|
|
|
if (rootID === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-01 08:02:30 +08:00
|
|
|
|
2020-01-09 21:54:11 +08:00
|
|
|
if (profilingData !== null && downloadRef.current !== null) {
|
|
|
|
|
const profilingDataExport = prepareProfilingDataExport(profilingData);
|
|
|
|
|
const date = new Date();
|
|
|
|
|
const dateString = date
|
|
|
|
|
.toLocaleDateString(undefined, {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
})
|
|
|
|
|
.replace(/\//g, '-');
|
|
|
|
|
const timeString = date
|
|
|
|
|
.toLocaleTimeString(undefined, {
|
|
|
|
|
hour12: false,
|
|
|
|
|
})
|
|
|
|
|
.replace(/:/g, '-');
|
|
|
|
|
downloadFile(
|
|
|
|
|
downloadRef.current,
|
|
|
|
|
`profiling-data.${dateString}.${timeString}.json`,
|
|
|
|
|
JSON.stringify(profilingDataExport, null, 2),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}, [rootID, profilingData]);
|
2019-04-01 08:02:30 +08:00
|
|
|
|
|
|
|
|
const uploadData = useCallback(() => {
|
|
|
|
|
if (inputRef.current !== null) {
|
|
|
|
|
inputRef.current.click();
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
2020-01-09 21:54:11 +08:00
|
|
|
const handleFiles = useCallback(() => {
|
|
|
|
|
const input = inputRef.current;
|
|
|
|
|
if (input !== null && input.files.length > 0) {
|
|
|
|
|
const fileReader = new FileReader();
|
|
|
|
|
fileReader.addEventListener('load', () => {
|
|
|
|
|
try {
|
|
|
|
|
const raw = ((fileReader.result: any): string);
|
|
|
|
|
const profilingDataExport = ((JSON.parse(
|
|
|
|
|
raw,
|
|
|
|
|
): any): ProfilingDataExport);
|
|
|
|
|
profilerStore.profilingData = prepareProfilingDataFrontendFromExport(
|
|
|
|
|
profilingDataExport,
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
modalDialogDispatch({
|
|
|
|
|
type: 'SHOW',
|
|
|
|
|
title: 'Import failed',
|
|
|
|
|
content: (
|
|
|
|
|
<Fragment>
|
|
|
|
|
<div>The profiling data you selected cannot be imported.</div>
|
|
|
|
|
{error !== null && (
|
|
|
|
|
<div className={styles.ErrorMessage}>{error.message}</div>
|
|
|
|
|
)}
|
|
|
|
|
</Fragment>
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// TODO (profiling) Handle fileReader errors.
|
|
|
|
|
fileReader.readAsText(input.files[0]);
|
|
|
|
|
}
|
|
|
|
|
}, [modalDialogDispatch, profilerStore]);
|
2019-04-01 08:02:30 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Fragment>
|
|
|
|
|
<div className={styles.VRule} />
|
|
|
|
|
<input
|
|
|
|
|
ref={inputRef}
|
|
|
|
|
className={styles.Input}
|
|
|
|
|
type="file"
|
|
|
|
|
onChange={handleFiles}
|
2019-04-08 00:00:52 +08:00
|
|
|
tabIndex={-1}
|
2019-04-01 08:02:30 +08:00
|
|
|
/>
|
2019-09-03 23:35:12 +08:00
|
|
|
<a ref={downloadRef} className={styles.Input} />
|
2019-04-01 08:02:30 +08:00
|
|
|
<Button
|
|
|
|
|
disabled={isProfiling}
|
|
|
|
|
onClick={uploadData}
|
2019-08-14 08:58:03 +08:00
|
|
|
title="Load profile...">
|
2019-04-01 08:02:30 +08:00
|
|
|
<ButtonIcon type="import" />
|
|
|
|
|
</Button>
|
2019-05-22 21:46:42 +08:00
|
|
|
<Button
|
2019-05-23 23:36:34 +08:00
|
|
|
disabled={isProfiling || !profilerStore.didRecordCommits}
|
2019-05-22 21:46:42 +08:00
|
|
|
onClick={downloadData}
|
2019-08-14 08:58:03 +08:00
|
|
|
title="Save profile...">
|
2019-05-22 21:46:42 +08:00
|
|
|
<ButtonIcon type="export" />
|
|
|
|
|
</Button>
|
2019-04-01 08:02:30 +08:00
|
|
|
</Fragment>
|
|
|
|
|
);
|
|
|
|
|
}
|