Skip to content

Commit eab9eb5

Browse files
authored
Merge pull request #7 from GeorgeZhang2035/perf/sheet
Perf/sheet
2 parents e5f5527 + b0b40e9 commit eab9eb5

File tree

6 files changed

+186
-94
lines changed

6 files changed

+186
-94
lines changed

src/components/editor/operator/sheet/SheetOperator.tsx

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import { FC, useEffect, useRef, useState } from 'react'
1+
import { FC, useRef } from 'react'
22

33
import { SheetContainerSkeleton } from './SheetContainerSkeleton'
44
import { OperatorNoData } from './SheetNoneData'
5+
import { ProfClassification } from './sheetOperator/ProfClassification'
56
import {
6-
ProfClassification,
7-
ProfClassificationProp,
8-
defaultProfFilter,
9-
} from './sheetOperator/ProfClassification'
7+
OperatorFilterProvider,
8+
useOperatorFilterProvider,
9+
} from './sheetOperator/SheetOperatorFilterProvider'
1010
import { SheetOperatorItem } from './sheetOperator/SheetOperatorItem'
11-
import { ShowMore, defaultPagination } from './sheetOperator/ShowMore'
12-
import {
13-
PaginationFilter,
14-
useOperatorAfterFiltered,
15-
} from './sheetOperator/useOperatorFilter'
11+
import { ShowMore } from './sheetOperator/ShowMore'
1612

1713
export interface SheetOperatorProps {}
1814

@@ -23,18 +19,9 @@ export interface SheetOperatorProps {}
2319
const SheetOperator: FC<SheetOperatorProps> = () => {
2420
const operatorScrollRef = useRef<HTMLDivElement>(null)
2521

26-
const [profFilter, setProfFilter] =
27-
useState<ProfClassificationProp['profFilter']>(defaultProfFilter)
28-
2922
// const getOperatorRarity = (target: string) =>
3023
// operatorsGroupedByProf.find((item) => item.name === target)!.rarity
3124

32-
// pagination about via frontened
33-
const [pagination, setPagination] =
34-
useState<PaginationFilter>(defaultPagination)
35-
36-
// useEffect(resetPaginationState, [selectedProf, selectedSubProf])
37-
3825
// const selectAll = () => {
3926
// operatorsGroupedBySubProf.forEach((item) => {
4027
// submitOperator(item, () => {})
@@ -98,55 +85,36 @@ const SheetOperator: FC<SheetOperatorProps> = () => {
9885

9986
// console.log(selectedProf, selectedSubProf)
10087

101-
const toTop = () => {
102-
operatorScrollRef?.current?.scrollIntoView()
103-
}
88+
const toTop = () => operatorScrollRef?.current?.scrollIntoView()
10489

10590
const {
106-
data: operatorsAfterFiltered,
107-
meta: { dataTotal },
108-
} = useOperatorAfterFiltered(profFilter, pagination)
109-
110-
// prof listener
111-
useEffect(() => {
112-
setPagination(defaultPagination)
113-
toTop()
114-
}, [profFilter])
115-
116-
// pagination listener
117-
useEffect(() => {
118-
if (pagination.current === 1) toTop()
119-
}, [pagination])
91+
operatorFiltered: { data: operatorFilteredData },
92+
} = useOperatorFilterProvider()
12093

12194
return (
122-
<>
123-
<div className="flex h-full">
124-
<div className="flex-auto px-1" ref={operatorScrollRef}>
125-
{operatorsAfterFiltered.length ? (
126-
<>
127-
<div
128-
key="operatorContainer"
129-
className="flex flex-wrap items-start content-start overscroll-contain relative"
130-
>
131-
{operatorsAfterFiltered.map(({ name }, index) => (
132-
<div
133-
className="flex items-center flex-0 w-32 h-32"
134-
key={index}
135-
>
136-
<SheetOperatorItem {...{ name }} />
137-
</div>
138-
))}
139-
</div>
140-
<ShowMore {...{ pagination, setPagination, dataTotal }} />
141-
</>
142-
) : (
143-
OperatorNoData
144-
)}
145-
</div>
146-
{/* {ProfSelect} */}
147-
<ProfClassification {...{ profFilter, setProfFilter }} />
95+
<div className="flex h-full">
96+
<div className="flex-auto px-1" ref={operatorScrollRef}>
97+
{operatorFilteredData.length ? (
98+
<>
99+
<div
100+
key="operatorContainer"
101+
className="flex flex-wrap items-start content-start overscroll-contain relative"
102+
>
103+
{operatorFilteredData.map(({ name }, index) => (
104+
<div className="flex items-center flex-0 w-32 h-32" key={index}>
105+
<SheetOperatorItem {...{ name }} />
106+
</div>
107+
))}
108+
</div>
109+
<ShowMore {...{ toTop }} />
110+
</>
111+
) : (
112+
OperatorNoData
113+
)}
148114
</div>
149-
</>
115+
{/* {ProfSelect} */}
116+
<ProfClassification {...{ toTop }} />
117+
</div>
150118
)
151119
}
152120

@@ -229,6 +197,8 @@ export const SheetOperatorContainer = (
229197
sheetOperatorProp: SheetOperatorProps,
230198
) => (
231199
<SheetContainerSkeleton title="选择干员" icon="person">
232-
<SheetOperator {...sheetOperatorProp} />
200+
<OperatorFilterProvider>
201+
<SheetOperator {...sheetOperatorProp} />
202+
</OperatorFilterProvider>
233203
</SheetContainerSkeleton>
234204
)

src/components/editor/operator/sheet/sheetOperator/ProfClassification.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
import { Divider, H4, H5 } from '@blueprintjs/core'
22

33
import clsx from 'clsx'
4-
import {
5-
Dispatch,
6-
FC,
7-
ImgHTMLAttributes,
8-
SetStateAction,
9-
useMemo,
10-
useState,
11-
} from 'react'
4+
import { FC, ImgHTMLAttributes, useEffect, useMemo, useState } from 'react'
125

136
import { PROFESSIONS } from 'models/operator'
147

158
import {
169
DEFAULTPROFID,
1710
DEFAULTSUBPROFID,
1811
ProfFilter as ProfFilterOuter,
19-
} from './useOperatorFilter'
12+
useOperatorFilterProvider,
13+
} from './SheetOperatorFilterProvider'
14+
import { defaultPagination } from './ShowMore'
2015

2116
const formattedProfessions = [
2217
{
@@ -44,14 +39,15 @@ export const defaultProfFilter: ProfFilter = {
4439
}
4540

4641
export interface ProfClassificationProp {
47-
profFilter: ProfFilter
48-
setProfFilter: Dispatch<SetStateAction<ProfFilter>>
42+
toTop: () => void
4943
}
5044

51-
export const ProfClassification: FC<ProfClassificationProp> = ({
52-
profFilter: { selectedProf },
53-
setProfFilter,
54-
}) => {
45+
export const ProfClassification: FC<ProfClassificationProp> = ({ toTop }) => {
46+
const {
47+
useProfFilterState: [{ selectedProf }, setProfFilter],
48+
usePaginationFilterState: [_, setPaginationFilter],
49+
} = useOperatorFilterProvider()
50+
5551
const subProfs = useMemo(() => {
5652
return [
5753
{ id: DEFAULTSUBPROFID.ALL, name: '全部' },
@@ -61,6 +57,11 @@ export const ProfClassification: FC<ProfClassificationProp> = ({
6157
]
6258
}, [selectedProf])
6359

60+
useEffect(() => {
61+
toTop()
62+
setPaginationFilter(defaultPagination)
63+
}, [selectedProf, setPaginationFilter, toTop])
64+
6465
// const ActionList = (
6566
// <div className="absolute bottom-0">
6667
// <Popover2

src/components/editor/operator/sheet/sheetOperator/useOperatorFilter.tsx renamed to src/components/editor/operator/sheet/sheetOperator/SheetOperatorFilterProvider.tsx

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { useAtomValue } from 'jotai'
2-
import { useMemo } from 'react'
2+
import {
3+
Dispatch,
4+
FC,
5+
ReactNode,
6+
SetStateAction,
7+
createContext,
8+
useContext,
9+
useMemo,
10+
useState,
11+
} from 'react'
312

413
import { OperatorInfo as ModelsOperator, OPERATORS } from 'models/operator'
514
import { favOperatorAtom } from 'store/useFavOperators'
615

716
import { useSheet } from '../SheetProvider'
17+
import { defaultProfFilter } from './ProfClassification'
18+
import { defaultPagination } from './ShowMore'
819

920
type OperatorInfo = ModelsOperator
1021

@@ -33,6 +44,51 @@ export interface PaginationFilter {
3344
current: number
3445
}
3546

47+
interface OperatorFilterProviderProp {
48+
children: ReactNode
49+
}
50+
51+
type UseState<T> = [T, Dispatch<SetStateAction<T>>]
52+
53+
type OperatorFilterProviderData = {
54+
usePaginationFilterState: UseState<PaginationFilter>
55+
useProfFilterState: UseState<ProfFilter>
56+
// useRarityFilterState: UseState<RarityFilter>
57+
operatorFiltered: {
58+
data: OperatorInfo[]
59+
meta: {
60+
dataTotal: number
61+
}
62+
}
63+
}
64+
65+
const OperatorFilterContext = createContext<OperatorFilterProviderData>(
66+
{} as OperatorFilterProviderData,
67+
)
68+
69+
export const OperatorFilterProvider: FC<OperatorFilterProviderProp> = ({
70+
children,
71+
}) => {
72+
const [paginationFilter, setPaginationFilter] =
73+
useState<PaginationFilter>(defaultPagination)
74+
const [profFilter, setProfFilter] = useState<ProfFilter>(defaultProfFilter)
75+
76+
return (
77+
<OperatorFilterContext.Provider
78+
value={{
79+
usePaginationFilterState: [paginationFilter, setPaginationFilter],
80+
useProfFilterState: [profFilter, setProfFilter],
81+
// useRarityFilterState: []
82+
operatorFiltered: useOperatorFiltered(profFilter, paginationFilter),
83+
}}
84+
>
85+
{children}
86+
</OperatorFilterContext.Provider>
87+
)
88+
}
89+
90+
export const useOperatorFilterProvider = () => useContext(OperatorFilterContext)
91+
3692
const generateCustomizedOperInfo = (name: string): OperatorInfo => ({
3793
id: 'customized-' + name,
3894
name,
@@ -43,7 +99,7 @@ const generateCustomizedOperInfo = (name: string): OperatorInfo => ({
4399
alt_name: 'custormized operator named' + name,
44100
})
45101

46-
export const useOperatorAfterFiltered = (
102+
const useOperatorFiltered = (
47103
profFilter: ProfFilter,
48104
paginationFilter: PaginationFilter,
49105
) => {

src/components/editor/operator/sheet/sheetOperator/ShowMore.tsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
import { H6 } from '@blueprintjs/core'
22

3-
import { Dispatch, FC, SetStateAction } from 'react'
3+
import { FC, useEffect } from 'react'
44

5-
import { PaginationFilter } from './useOperatorFilter'
5+
import {
6+
PaginationFilter,
7+
useOperatorFilterProvider,
8+
} from './SheetOperatorFilterProvider'
69

710
export interface ShowMoreProp {
8-
pagination: PaginationFilter
9-
dataTotal: number
10-
setPagination: Dispatch<SetStateAction<PaginationFilter>>
11+
toTop: () => void
1112
}
1213

1314
export const defaultPagination: PaginationFilter = {
1415
current: 1,
1516
size: 60,
1617
}
1718

18-
export const ShowMore: FC<ShowMoreProp> = ({
19-
pagination: { current, size },
20-
dataTotal,
21-
setPagination,
22-
}) => {
19+
export const ShowMore: FC<ShowMoreProp> = ({ toTop }) => {
20+
const {
21+
operatorFiltered: {
22+
meta: { dataTotal },
23+
},
24+
usePaginationFilterState: [{ current, size }, setPagination],
25+
} = useOperatorFilterProvider()
26+
2327
const lastIndex = current * size
2428

29+
useEffect(() => {
30+
if (current === 1) toTop()
31+
}, [current, toTop])
32+
2533
return (
2634
<div className="flex items-center justify-center pt-3 cursor-default">
2735
{lastIndex >= dataTotal ? (
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
import { FC } from 'react'
1+
import { Dispatch, FC, SetStateAction } from 'react'
22

3-
export interface ToolBoxProp {}
3+
import { RarityFilter } from './SheetOperatorFilterProvider'
44

5-
export const ToolBox: FC<ToolBoxProp> = () => {
5+
export interface ToolBoxProp {
6+
toTop: () => void
7+
rarityFilter: RarityFilter
8+
setRarityFilter: Dispatch<SetStateAction<RarityFilter>>
9+
}
10+
11+
export const ToolBox: FC<ToolBoxProp> = ({
12+
toTop,
13+
rarityFilter,
14+
setRarityFilter,
15+
}) => {
616
return <>111</>
717
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Button } from '@blueprintjs/core'
2+
3+
import { FC, useMemo } from 'react'
4+
5+
import { useSheet } from '../../SheetProvider'
6+
import { useOperatorFilterProvider } from '../SheetOperatorFilterProvider'
7+
8+
export interface MutipleSelectProp {}
9+
10+
export const MutipleSelect: FC<MutipleSelectProp> = () => {
11+
const {
12+
operatorFiltered: { data: operatorFilteredData },
13+
} = useOperatorFilterProvider()
14+
const { existedOperators } = useSheet()
15+
16+
const { cancelAllDisabled, selectAllDisabled } = useMemo(() => {
17+
console.log('111')
18+
const existedOperatorsNames = existedOperators.map(({ name }) => name)
19+
return {
20+
cancelAllDisabled: !operatorFilteredData.some(({ name }) =>
21+
existedOperatorsNames.includes(name),
22+
),
23+
selectAllDisabled: operatorFilteredData.every(({ name }) =>
24+
existedOperatorsNames.includes(name),
25+
),
26+
}
27+
}, [existedOperators, operatorFilteredData])
28+
29+
return (
30+
<div className="flex justify-center content-center">
31+
<Button
32+
minimal
33+
icon="circle"
34+
disabled={cancelAllDisabled}
35+
title={`取消选择全部${existedOperators.length}位干员`}
36+
onClick={cancelAll}
37+
/>
38+
<Button
39+
minimal
40+
icon="selection"
41+
title={`全选${operatorFilteredData.length}位干员`}
42+
disabled={selectAllDisabled}
43+
onClick={selectAll}
44+
/>
45+
</div>
46+
)
47+
}

0 commit comments

Comments
 (0)