Skip to content

Commit

Permalink
[PwaLayout] Optimize initial load filter logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
cvanem committed Nov 24, 2024
1 parent 6af1d63 commit e52bc17
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
53 changes: 53 additions & 0 deletions src/components/pages/useAppTableData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,56 @@ export default function useAppTableData({ trigger = true, triggerWhenEmpty = fal

return { filtered, loading, apps, setApps, handleRefresh, handleGetRow };
}

export function useAppTableDataInit({ trigger = true } = {}) {
const [, setApps] = useApplications();
const [, setLoading] = React.useState(false);

const handleRefresh = React.useCallback(
({ requestParams = undefined } = {}) => {
const getItems = async () => {
let scanResults = [];
let items;
var params = {
TableName: tables.applications,
ExclusiveStartKey: undefined,
...requestParams
};
var firstPass = true;
setLoading(true);
do {
items = await dynamo.scan(params).promise();
items.Items.forEach(i => scanResults.push(i));
params.ExclusiveStartKey = items.LastEvaluatedKey;
if (firstPass) {
firstPass = false; // Show the first results so the user doesn't see an empty screen
setApps(prev => ({
...prev,
...scanResults.reduce((f, c: any) => {
f[c._id] = c;
return f;
}, {})
}));
}
} while (typeof items.LastEvaluatedKey != 'undefined');
setApps(prev => ({
...prev,
...scanResults.reduce((f, c: any) => {
f[c._id] = c;
return f;
}, {})
}));
setLoading(false);
};
getItems();
},
[setApps]
);

// Load data from the database
React.useEffect(() => {
trigger && handleRefresh();
}, [trigger, handleRefresh]);

return { handleRefresh };
}
4 changes: 2 additions & 2 deletions src/components/pwa/PwaLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import createStyles from '@mui/styles/createStyles';
import { useAppBarHeight } from '../layout/hooks';
import ScrollElementProvider from '../layout/ScrollElementProvider';
import useHeight from '../layout/ViewPort/hooks/useHeight';
import useAppTableData from '../pages/useAppTableData';
import { useAppTableDataInit } from '../pages/useAppTableData';
import PwaAppBar from './PwaAppBar';

const useStyles = makeStyles(({ breakpoints, palette, appBarHeight }: any) =>
Expand Down Expand Up @@ -43,7 +43,7 @@ export function PwaLayout({ children }) {
var contentHeight = height - componentsOnPage.reduce((t, c) => t + c, 0);
var [scrollElement, setScrollElement] = React.useState(null);

useAppTableData({ trigger: true });
useAppTableDataInit({ trigger: true });

const classes = useStyles({
leftDrawerOpen: false,
Expand Down

0 comments on commit e52bc17

Please sign in to comment.