Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 8bebbd4

Browse files
committed
Improve performance
1 parent 46d5c38 commit 8bebbd4

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

components/calculationInput/equipments/inventory/AddToInventoryDialog.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import styles from './AddToInventoryDialog.module.scss';
22
import {Button, Dialog, DialogActions, DialogContent, DialogTitle, useMediaQuery, useTheme} from '@mui/material';
3-
import React, {useEffect, useMemo} from 'react';
3+
import React, {useEffect, useMemo, useReducer} from 'react';
44
import {useTranslation} from 'next-i18next';
55
import {useForm} from 'react-hook-form';
66
import {DropPieceIdWithProbAndCount, EquipmentsById} from 'components/calculationInput/PiecesCalculationCommonTypes';
77
import {InventoryForm} from './InventoryUpdateDialog';
88
import ObtainedPieceBox from './ObtainedPieceBox';
9-
import {EquipmentsByTierAndCategory} from '../EquipmentsInput';
109
import {useStore} from 'stores/WizStore';
1110
import {PieceState} from './PiecesInventory';
1211

@@ -29,6 +28,12 @@ const AddToInventoryDialog = ({
2928
const {t} = useTranslation('home');
3029
const theme = useTheme();
3130

31+
// hack to lazy rendering
32+
const [onceOpen, notifyOpened] = useReducer((x) => true, false);
33+
useEffect(() => {
34+
open && notifyOpened();
35+
}, [open]);
36+
3237
const pieceIds = useMemo(() => drops.map((drop) => drop.id), [drops]);
3338
const pieces = useMemo(() => {
3439
return Array.from(piecesState.values())
@@ -51,30 +56,28 @@ const AddToInventoryDialog = ({
5156
defaultValues,
5257
});
5358

54-
useEffect(() => {
55-
open && reset();
56-
}, [open, reset]);
57-
5859
const handleCancelDialog = () => {
5960
onCancel();
61+
reset();
6062
};
6163

6264
const handleUpdateInventory = () => {
6365
onUpdate(getValues());
6466
const inventory = Object.entries(getValues()).reduce<InventoryForm>((acc, [id, value]) => {
65-
const count = parseInt(value);
66-
const stock = piecesState.get(id)?.inStockCount;
67-
count && stock && (acc[id] = `${count + stock}`);
67+
const count = parseInt(value) ?? 0;
68+
const stock = piecesState.get(id)?.inStockCount ?? 0;
69+
acc[id] = `${count + stock}`;
6870
return acc;
6971
}, {});
7072
store.equipmentsRequirementStore.updateInventory(inventory);
73+
reset();
7174
};
7275

7376
const isFullScreen = useMediaQuery(theme.breakpoints.down('md'));
7477
const isXsOrSmallerScreen = useMediaQuery(theme.breakpoints.down('sm'));
7578
const hasManyPieces = () => pieces.length > 3;
7679

77-
return <Dialog open={open} keepMounted fullWidth
80+
return !onceOpen ? null : <Dialog open={open} keepMounted fullWidth
7881
fullScreen={hasManyPieces() && isFullScreen}
7982
maxWidth={hasManyPieces() && 'xl'}>
8083
<DialogTitle>獲得した設計図</DialogTitle>

0 commit comments

Comments
 (0)