11import styles from './AddToInventoryDialog.module.scss' ;
22import { 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' ;
44import { useTranslation } from 'next-i18next' ;
55import { useForm } from 'react-hook-form' ;
66import { DropPieceIdWithProbAndCount , EquipmentsById } from 'components/calculationInput/PiecesCalculationCommonTypes' ;
77import { InventoryForm } from './InventoryUpdateDialog' ;
88import ObtainedPieceBox from './ObtainedPieceBox' ;
9- import { EquipmentsByTierAndCategory } from '../EquipmentsInput' ;
109import { useStore } from 'stores/WizStore' ;
1110import { 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