Skip to content

Commit

Permalink
fix fetching files
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi-sl committed Sep 19, 2024
1 parent 23eabf9 commit 62d335e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/Components/Import/URLInput.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import React, { useState } from 'react';
import { stringify } from 'qs';

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 = () => {
const { actions } = useNetwork();
const [url, setURL] = useState('');
const [isCORSEnabled, setCORS] = useState(false);
const handleInputChange = ({ target }) => {
setURL(target.value);
};

const handleSubmit = () => {
actions.fetchFile({
const { origin, pathname } = document.location;
const newURL = `${origin}${pathname}?${stringify({
file: url,
isCORSEnabled,
});
})}`;
document.location.href = newURL;
};

return (
Expand Down
2 changes: 1 addition & 1 deletion src/Components/NetworkTable/NetworkTableBody.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const NetworkTableBody = ({ height }) => {
const selectedReqIndex = state.get('selectedReqIndex');

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

useEffect(() => {
actions.setTableHeaderWidth(elementDims.width);
Expand Down
10 changes: 4 additions & 6 deletions src/Containers/NetworkTableContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useTheme } from '../state/theme/Context';
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 @@ -23,15 +22,14 @@ const NetworkTableContainer = () => {
const showReqDetail = state.get('showReqDetail');

const [tableBodyHeight, setTableBodyHeight] = useState(0);
const ref = useRef(null);
const isVisible = useIsVisible(ref);
const { elementDims } = useResizeObserver(ref);
const ref = useRef();
const { elementDims } = useResizeObserver(ref?.current);

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

if (error) {
return (
Expand Down
34 changes: 21 additions & 13 deletions src/hooks/useResizeObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,44 @@ import { useEffect, useState } from 'react';

/* eslint no-underscore-dangle: 0 */

const debounce = (func, delay) => {
let timeoutId;
return (...args) => {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func(...args);
}, delay);
};
};

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 (ref) {
const onResize = debounce(() => {
if (elementRef) {
setElementDims({
width: ref.clientWidth,
height: ref.clientHeight,
width: elementRef.clientWidth,
height: elementRef.clientHeight,
});
}
};
}, 50);

const resizeObserver = new ResizeObserver(onResize);
const resizeObserver = new ResizeObserver(() => onResize());

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

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

return { elementDims };
};
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: 1 addition & 2 deletions src/state/network/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ export const resetState = (dispatch) => (payload) => dispatch({
payload,
});

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

0 comments on commit 62d335e

Please sign in to comment.