Skip to content

Commit

Permalink
Fix layout and stylings (#152)
Browse files Browse the repository at this point in the history
* spinner blue, resizing fix

* improve fetching file

* update test snapshot and jest config for ResizeObserver

* lower case Fetching

* set peerDeps

* update snapshots

* table header width
  • Loading branch information
yuyi-sl authored Sep 11, 2024
1 parent 5468da5 commit 469c0a1
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 84 deletions.
14 changes: 14 additions & 0 deletions config/jest/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ global.IntersectionObserver = class IntersectionObserver {
return null;
}
};

global.ResizeObserver = class ResizeObserver {
disconnect() {
return null;
}

observe() {
return null;
}

unobserve() {
return null;
}
};
5 changes: 2 additions & 3 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const App = () => {
<div className={networkContainerClassName}>
<NetworkViewer
onDataLoaded={() => setIsDataLoaded(true)}
onReset={() => setIsDataLoaded(false)}
{...fileOptions}
/>
</div>
Expand Down
4 changes: 3 additions & 1 deletion examples/src/App.module.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
@import './../../src/styles/variables';

.app-container {
display: flex;
flex-direction: column;
height: 100%;

.network-container {
height: auto;
flex: 1 1 auto;

&.network-container-data-loaded {
height: 100%;
Expand Down
4 changes: 0 additions & 4 deletions examples/src/Components/Footer.module.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
@import './../../../src/styles/variables';

.footer {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
padding: 1rem;
background-color: $white-97;
border-top: 1px solid $white-80;
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
"recharts": ">=1.8.5"
},
"peerDependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1"
"react": "^16.13.1"
},
"devDependencies": {
"@babel/core": "^7.24.8",
Expand Down
11 changes: 7 additions & 4 deletions src/Components/Actions/ResetButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import IconReset from '../../icons/IconReset';
import Styles from './IconButton.styles.scss';
import { useNetwork } from '../../state/network/Context';
import Tooltip from '../Common/Tooltip/Tooltip';
import { useTheme } from '../../state/theme/Context';

const ResetButton = () => {
const {
actions,
callbacks,
} = useNetwork();
const { actions, callbacks } = useNetwork();
const { showImportHar } = useTheme();

const handleReset = () => {
actions.resetState();
callbacks.onReset();

if (showImportHar) {
window.history.pushState({}, document.title, '/');
}
};

return (
Expand Down
50 changes: 21 additions & 29 deletions src/Components/Import/InputHAR.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,26 @@ import URLInput from './URLInput';

const SAMPLE_HAR_URL = 'https://raw.githubusercontent.com/saucelabs/network-viewer/main/examples/src/data/network.har';

const InputHAR = () => {
const handleURLSubmit = (fetchInfo) => {
const { origin, pathname } = document.location;
const newURL = `${origin}${pathname}?${stringify(fetchInfo)}`;
document.location.href = newURL;
};

return (
<div className={Styles['input-har-container']}>
<h4 className={Styles['input-har-text']}>
OR add HAR file URL in the below input box
</h4>
<URLInput onSubmit={handleURLSubmit} />
<p>
<span>
For Example use this har file
</span>
<a
className={Styles['example-url']}
href={SAMPLE_HAR_URL}
rel="noopener noreferrer"
target="_blank"
>
{SAMPLE_HAR_URL}
</a>
</p>
</div>
);
};
const InputHAR = () => (
<div className={Styles['input-har-container']}>
<h4 className={Styles['input-har-text']}>
OR add HAR file URL in the below input box
</h4>
<URLInput />
<p>
<span>
For Example use this har file
</span>
<a
className={Styles['example-url']}
href={SAMPLE_HAR_URL}
rel="noopener noreferrer"
target="_blank"
>
{SAMPLE_HAR_URL}
</a>
</p>
</div>
);

export default InputHAR;
11 changes: 4 additions & 7 deletions src/Components/Import/URLInput.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';

import Styles from './URLInput.styles.scss';
import Button from './../../../src/Components/Common/Button';
import CORSCheckbox from './CORSCheckbox';
import { useNetwork } from '../../state/network/Context';

const URLInput = ({ onSubmit }) => {
const URLInput = () => {
const { actions } = useNetwork();
const [url, setURL] = useState('');
const [isCORSEnabled, setCORS] = useState(false);
const handleInputChange = ({ target }) => {
setURL(target.value);
};

const handleSubmit = () => {
onSubmit({
actions.fetchFile({
file: url,
isCORSEnabled,
});
Expand Down Expand Up @@ -43,8 +44,4 @@ const URLInput = ({ onSubmit }) => {
);
};

URLInput.propTypes = {
onSubmit: PropTypes.func.isRequired,
};

export default URLInput;
8 changes: 4 additions & 4 deletions src/Components/LoaderContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
import Styles from './LoaderContainer.styles.scss';

const LoaderContainer = ({ children, show, text }) => {
const spinnerColor = Styles.brandBlue;
const colorBrandBlue = '#0E75DD';
const uniqueId = `Gradient-${Math.round(Math.random() * 10000000)}`;

return !show ? children : (
Expand All @@ -18,16 +18,16 @@ const LoaderContainer = ({ children, show, text }) => {
<linearGradient id={uniqueId}>
<stop
offset="0%"
stopColor={spinnerColor}
stopColor={colorBrandBlue}
/>
<stop
offset="75%"
stopColor={spinnerColor}
stopColor={colorBrandBlue}
stopOpacity="0"
/>
<stop
offset="100%"
stopColor={spinnerColor}
stopColor={colorBrandBlue}
stopOpacity="0"
/>
</linearGradient>
Expand Down
20 changes: 11 additions & 9 deletions src/Components/NetworkTable/NetworkTableBody.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,24 @@ const NetworkTableBody = ({ height }) => {
const totalNetworkTime = state.get('totalNetworkTime');
const selectedReqIndex = state.get('selectedReqIndex');

const ref = useRef(null);
const { elementDims } = useResizeObserver(ref?.current?._outerRef || ref?.current);
const listRef = useRef(null);
const { elementDims } = useResizeObserver(listRef);

useEffect(() => actions.setTableHeaderWidth(elementDims.width), [elementDims]);
useEffect(() => {
actions.setTableHeaderWidth(elementDims.width);
}, [elementDims]);

useEffect(() => {
if (enableAutoScroll && ref?.current?._outerRef) {
const outerRef = ref?.current?._outerRef;
if (enableAutoScroll && listRef?.current?._outerRef) {
const outerRef = listRef?.current?._outerRef;
const needToScroll = outerRef.scrollTop +
outerRef.offsetHeight +
(numberOfNewEntries * TABLE_ENTRY_HEIGHT) >= outerRef.scrollHeight;
if (needToScroll) {
ref.current._outerRef.scrollTop = outerRef.scrollHeight;
listRef.current._outerRef.scrollTop = outerRef.scrollHeight;
}
}
}, [data, ref]);
}, [data, listRef]);

const handleReqSelect = (payload) => {
if (selectedReqIndex === payload.index) {
Expand All @@ -80,7 +82,7 @@ const NetworkTableBody = ({ height }) => {
if (actualData.size === 0) {
return (
<div
ref={ref}
ref={listRef}
className={Styles['no-data']}
>
<IconNetworkRequest className={Styles['network-icon']} />
Expand All @@ -98,7 +100,7 @@ const NetworkTableBody = ({ height }) => {
return (
<>
<FixedSizeList
ref={ref}
ref={listRef}
className={Styles['network-table-body']}
height={height}
itemCount={data.size}
Expand Down
4 changes: 3 additions & 1 deletion src/Containers/NetworkTableContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import InputHAR from '../Components/Import/InputHAR';
import NetworkTableBody from '../Components/NetworkTable/NetworkTableBody';
import { TABLE_HEADER_HEIGHT } from '../constants';
import { useIsVisible } from '../hooks/useIsVisible';
import { useResizeObserver } from '../hooks/useResizeObserver';

const context = classNames.bind(Styles);

Expand All @@ -24,12 +25,13 @@ const NetworkTableContainer = () => {
const [tableBodyHeight, setTableBodyHeight] = useState(0);
const ref = useRef(null);
const isVisible = useIsVisible(ref);
const { elementDims } = useResizeObserver(ref);

useEffect(() => {
if (ref?.current && isVisible) {
setTableBodyHeight(ref.current.clientHeight - TABLE_HEADER_HEIGHT);
}
}, [ref, actualData, isVisible]);
}, [ref, actualData, isVisible, elementDims]);

if (error) {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export const STATUS_FILTERS = [
},
];

export const FETCH_FILE_LOAD_TEXT = 'Please wait, Fetching file is in progress.';
export const FETCH_FILE_LOAD_TEXT = 'Please wait, fetching file is in progress.';

export const TIMINGS = {
queueing: {
Expand Down
20 changes: 12 additions & 8 deletions src/hooks/useResizeObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@ import { useEffect, useState } from 'react';
/* eslint no-underscore-dangle: 0 */

export const useResizeObserver = (elementRef) => {
let ref = null;
const [elementDims, setElementDims] = useState({
width: 0,
height: 0,
});

useEffect(() => {
ref = elementRef?.current?._outerRef || elementRef?.current;
const onResize = () => {
if (elementRef) {
if (ref) {
setElementDims({
width: elementRef.clientWidth,
height: elementRef.clientHeight,
width: ref.clientWidth,
height: ref.clientHeight,
});
}
};

const resizeObserver = new ResizeObserver(onResize);

if (elementRef) {
resizeObserver.observe(elementRef);
if (ref) {
resizeObserver.observe(ref);
}

return () => {
if (elementRef) {
resizeObserver.unobserve(elementRef);
if (ref) {
resizeObserver.unobserve(ref);
}
};
}, [elementRef]);
}, [ref]);

return { elementDims };
};
1 change: 1 addition & 0 deletions src/state/network/Context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const useNetwork = () => {
const [state, dispatch, callbacks] = context;

const wrappedActions = actionsWrapper({
fetchFile: actions.fetchFile,
updateData: actions.updateData,
updateSearch: actions.updateSearch,
updateSort: actions.updateSort,
Expand Down
2 changes: 1 addition & 1 deletion src/state/network/NetworkProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const NetworkProvider = (props) => {
// Fetch HAR file onChange of file prop
useEffect(() => {
if (file) {
fetchFile(dispatch)(file, fetchOptions);
fetchFile(dispatch)({ file, fetchOptions });
}
}, [file]);

Expand Down
3 changes: 2 additions & 1 deletion src/state/network/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export const resetState = (dispatch) => (payload) => dispatch({
payload,
});

export const fetchFile = (dispatch) => (file, options = { withCredentials: true }) => {
export const fetchFile = (dispatch) => (payload = { options: { withCredentials: true } }) => {
const { file, options } = payload;
fetchFileRequest(dispatch)();
axios.get(file, options)
.then(({ data }) => {
Expand Down
Loading

0 comments on commit 469c0a1

Please sign in to comment.