Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ module.exports = (api) => {

return {
presets: [
'@babel/preset-react',
[
'@babel/preset-react',
{
// uncomment to use why-did-you-render
// runtime: 'automatic',
// development: !isProd,
// importSource: '@welldone-software/why-did-you-render',
},
],
[
'@babel/preset-typescript',
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@babel/preset-env": "^7.12.11",
"@babel/preset-react": "^7.12.10",
"@babel/preset-typescript": "^7.12.7",
"@welldone-software/why-did-you-render": "^6.1.1",
"axios": "0.21.1",
"bignumber.js": "^9.0.1",
"chart.js": "^2.9.3",
Expand Down
2 changes: 2 additions & 0 deletions querybook/webapp/components/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// import './wdyr';

import { hot } from 'react-hot-loader/root';
import { setConfig } from 'react-hot-loader';
import React from 'react';
Expand Down
14 changes: 14 additions & 0 deletions querybook/webapp/components/App/wdyr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="@welldone-software/why-did-you-render" />

import React from 'react';

if (process.env.NODE_ENV === 'development') {
const whyDidYouRender = require('@welldone-software/why-did-you-render');

whyDidYouRender(React, {
trackAllPureComponents: true,
trackHooks: true,
trackExtraHooks: [[require('react-redux/lib'), 'useSelector']],
exclude: [],
});
}
70 changes: 38 additions & 32 deletions querybook/webapp/components/DataDoc/DataDoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ interface IState {
showSearchAndReplace: boolean;
}

class DataDocComponent extends React.Component<IProps, IState> {
class DataDocComponent extends React.PureComponent<IProps, IState> {
public readonly state = {
errorObj: null,
focusedCellIndex: null,
Expand Down Expand Up @@ -157,7 +157,7 @@ class DataDocComponent extends React.Component<IProps, IState> {
}

@bind
@debounce(200)
@debounce(500)
public updateDocCursor(index: number) {
const {
docId,
Expand Down Expand Up @@ -408,31 +408,43 @@ class DataDocComponent extends React.Component<IProps, IState> {
});
}

@bind
public onQuerycellSelectExecution(cellId: number, executionId: number) {
this.setState(
({ cellIdToExecutionId: oldCellIdToExecutionId }) => ({
cellIdToExecutionId: {
...oldCellIdToExecutionId,
[cellId]: executionId,
},
}),
() => {
this.updateDocUrl(cellId, executionId);
}
);
}

@decorate(memoizeOne)
public _getCellFocusProps() {
return {
onUpKeyPressed: (index: number) => this.focusCellAt(index - 1),
onDownKeyPressed: (index: number) => this.focusCellAt(index + 1),
onFocus: this.onCellFocus,
onBlur: this.onCellBlur,
};
}

@bind
@decorate(memoizeOne)
public _getDataDocContextState(
isEditable: boolean,
defaultCollapse: boolean,
focusedCellIndex: number,
fullScreenCellIndex: number,
highlightCellIndex: number,
cellIdToExecutionId: Record<number, number>
): IDataDocContextType {
return {
cellIdToExecutionId,
onQueryCellSelectExecution: (cellId, executionId) => {
this.setState(
({ cellIdToExecutionId: oldCellIdToExecutionId }) => ({
cellIdToExecutionId: {
...oldCellIdToExecutionId,
[cellId]: executionId,
},
}),
() => {
this.updateDocUrl(cellId, executionId);
}
);
},
onQueryCellSelectExecution: this.onQuerycellSelectExecution,

insertCellAt: this.insertCellAt,
updateCell: this.updateCell,
Expand All @@ -441,17 +453,10 @@ class DataDocComponent extends React.Component<IProps, IState> {
fullScreenCellAt: this.fullScreenCellAt,

defaultCollapse,
focusedCellIndex,
highlightCellIndex,
fullScreenCellIndex,

cellFocus: {
onUpKeyPressed: (index: number) => this.focusCellAt(index - 1),
onDownKeyPressed: (index: number) =>
this.focusCellAt(index + 1),
onFocus: this.onCellFocus,
onBlur: this.onCellBlur,
},
cellFocus: this._getCellFocusProps(),
isEditable,
};
}
Expand All @@ -461,7 +466,6 @@ class DataDocComponent extends React.Component<IProps, IState> {
return this._getDataDocContextState(
this.props.isEditable,
this.state.defaultCollapseAllCells,
this.state.focusedCellIndex,
this.state.fullScreenCellIndex,
this.state.highlightCellIndex,
this.state.cellIdToExecutionId
Expand Down Expand Up @@ -515,6 +519,7 @@ class DataDocComponent extends React.Component<IProps, IState> {
queryIndexInDoc: number
) {
const { dataDoc, isEditable } = this.props;
const { focusedCellIndex } = this.state;

const insertCellAtBinded = this.insertCellAt;

Expand Down Expand Up @@ -545,11 +550,14 @@ class DataDocComponent extends React.Component<IProps, IState> {
return (
<DataDocCell
key={cell.id}
dataDoc={dataDoc}
docId={dataDoc.id}
numberOfCells={dataDoc.dataDocCells.length}
templatedVariables={dataDoc.meta}
cell={cell}
index={index}
queryIndexInDoc={queryIndexInDoc}
lastQueryCellId={lastQueryCellId}
isFocused={focusedCellIndex === index}
/>
);
}
Expand Down Expand Up @@ -772,18 +780,17 @@ class DataDocComponent extends React.Component<IProps, IState> {

function mapStateToProps(state: IStoreState, ownProps: IOwnProps) {
const userInfo = myUserInfoSelector(state);
const dataDoc = dataDocSelectors.dataDocSelector(state, ownProps);
const dataDoc = dataDocSelectors.dataDocSelector(state, ownProps.docId);
const dataDocSavePromise =
state.dataDoc.dataDocSavePromiseById[ownProps.docId];

const isEditable = dataDocSelectors.canCurrentUserEditSelector(
state,
ownProps
ownProps.docId
);
const userIds = dataDocSelectors.dataDocViewerIdsSelector(state, ownProps);
const cellIdtoUid = dataDocSelectors.dataDocCursorByCellIdSelector(
const userIds = dataDocSelectors.dataDocViewerIdsSelector(
state,
ownProps
ownProps.docId
);

return {
Expand All @@ -795,7 +802,6 @@ function mapStateToProps(state: IStoreState, ownProps: IOwnProps) {
isEditable,
userIds,
environment: currentEnvironmentSelector(state),
cellIdtoUid,
};
}

Expand Down
82 changes: 50 additions & 32 deletions querybook/webapp/components/DataDoc/DataDocCellControl.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import clsx from 'clsx';
import React from 'react';
import React, { useCallback } from 'react';
import toast from 'react-hot-toast';
import { titleize, sleep, copy } from 'lib/utils';

import { IDataCellMeta } from 'const/datadoc';
import { useBoundFunc } from 'hooks/useBoundFunction';
import { AsyncButton } from 'ui/AsyncButton/AsyncButton';
import { Button, SoftButton } from 'ui/Button/Button';
import { SoftButton } from 'ui/Button/Button';
import { Dropdown } from 'ui/Dropdown/Dropdown';
import { ListMenu, IListMenuItem } from 'ui/Menu/ListMenu';

Expand Down Expand Up @@ -69,6 +70,19 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
);
}, [toggleDefaultCollapsed]);

const handleShare = useCallback(() => {
copy(shareUrl);
toast('Url Copied!');
}, [shareUrl]);
const handleCopyCell = useBoundFunc(copyCellAt, index, false);
const handleCutCell = useBoundFunc(copyCellAt, index, true);
const handlePasteCell = useBoundFunc(pasteCellAt, index);
const handleDeleteCell = useBoundFunc(deleteCellAt, index);
const handleMoveCellClick = useCallback(
() => moveCellAt(index, isHeader ? index - 1 : index + 1),
[moveCellAt, index, isHeader]
);

const rightButtons: JSX.Element[] = [];
const centerButtons: JSX.Element[] = [];

Expand All @@ -79,10 +93,7 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
if (shareUrl) {
leftMenuItems.push({
name: 'Share',
onClick: () => {
copy(shareUrl);
toast('Url Copied!');
},
onClick: handleShare,
tooltip: 'Click to copy',
tooltipPos: 'right',
icon: 'share',
Expand All @@ -93,7 +104,7 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
if (copyCellAt) {
leftMenuItems.push({
name: 'Copy',
onClick: () => copyCellAt(index, false),
onClick: handleCopyCell,
tooltip: 'Copy cell',
tooltipPos: 'right',
icon: 'copy',
Expand All @@ -103,7 +114,7 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
if (isEditable && copyCellAt) {
leftMenuItems.push({
name: 'Cut',
onClick: () => copyCellAt(index, true),
onClick: handleCutCell,
tooltip: 'Cut cell',
tooltipPos: 'right',
icon: 'cut',
Expand All @@ -113,7 +124,7 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
if (isEditable && pasteCellAt) {
leftMenuItems.push({
name: 'Paste',
onClick: () => pasteCellAt(index),
onClick: handlePasteCell,
tooltip: 'Paste cell to above',
tooltipPos: 'right',
icon: 'paste',
Expand All @@ -127,7 +138,7 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
deleteCellAt && isHeader && numberOfCells > 0 && (
<AsyncButton
className="block-crud-button"
onClick={deleteCellAt.bind(this, index)}
onClick={handleDeleteCell}
icon="x"
type="soft"
key="delete"
Expand All @@ -142,11 +153,7 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
moveCellAt && (
<AsyncButton
className="block-crud-button"
onClick={moveCellAt.bind(
this,
index,
isHeader ? index - 1 : index + 1
)}
onClick={handleMoveCellClick}
icon={isHeader ? 'chevrons-up' : 'chevrons-down'}
type="soft"
key="swap"
Expand Down Expand Up @@ -192,23 +199,12 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
);
}

Object.keys(cellTypes).forEach((cellKey) =>
centerButtons.push(
<AsyncButton
className="block-crud-button"
key={cellKey}
onClick={insertCellAt.bind(
null,
index,
cellKey,
null,
null
)}
icon="plus"
title={titleize(cellKey)}
type="soft"
/>
)
centerButtons.push(
<InsertCellButtons
index={index}
key="insert-cell-buttons"
insertCellAt={insertCellAt}
/>
);
} else {
// In case center buttons are empty
Expand Down Expand Up @@ -256,3 +252,25 @@ export const DataDocCellControl: React.FunctionComponent<IProps> = ({
</div>
);
};

const InsertCellButtons: React.FC<{
insertCellAt: IProps['insertCellAt'];
index: number;
}> = React.memo(({ insertCellAt, index }) => {
const handleInsertcell = useCallback(
(cellType: string) => insertCellAt(index, cellType, null, null),
[insertCellAt, index]
);

const buttonsDOM = Object.keys(cellTypes).map((cellKey) => (
<AsyncButton
className="block-crud-button"
key={cellKey}
onClick={() => handleInsertcell(cellKey)}
icon="plus"
title={titleize(cellKey)}
type="soft"
/>
));
return <>{buttonsDOM}</>;
});
Loading