xref: /MusicFree/src/pages/searchPage/components/resultPanel/resultSubPanel.tsx (revision ef60be1c70f066ecf4ce40dd183880baa09898a2)
14060c00aS猫头猫import React, {memo, useCallback, useState} from 'react';
24060c00aS猫头猫import {Text} from 'react-native';
3bf6e62f2S猫头猫import rpx from '@/utils/rpx';
4bf6e62f2S猫头猫import {SceneMap, TabBar, TabView} from 'react-native-tab-view';
5bf6e62f2S猫头猫import DefaultResults from './results/defaultResults';
6bf6e62f2S猫头猫import {renderMap} from './results';
7bf6e62f2S猫头猫import ResultWrapper from './resultWrapper';
8bf6e62f2S猫头猫import {fontWeightConst} from '@/constants/uiConst';
9a4ae8da5S猫头猫import {useAtomValue} from 'jotai';
10a4ae8da5S猫头猫import {searchResultsAtom} from '../../store/atoms';
11927dbe93S猫头猫import PluginManager from '@/core/pluginManager';
12bf6e62f2S猫头猫
13bf6e62f2S猫头猫interface IResultSubPanelProps {
14bf6e62f2S猫头猫    tab: ICommon.SupportMediaType;
15bf6e62f2S猫头猫}
16bf6e62f2S猫头猫
17bf6e62f2S猫头猫// 展示结果的视图
184f2deeb0S猫头猫function getResultComponent(
194f2deeb0S猫头猫    tab: ICommon.SupportMediaType,
204f2deeb0S猫头猫    pluginHash: string,
214f2deeb0S猫头猫    pluginName: string,
224f2deeb0S猫头猫) {
23bf6e62f2S猫头猫    return tab in renderMap
24d139abf1S猫头猫        ? memo(
25a4ae8da5S猫头猫              () => {
26a4ae8da5S猫头猫                  const searchResults = useAtomValue(searchResultsAtom);
27a4ae8da5S猫头猫                  const pluginSearchResult = searchResults[tab][pluginHash];
284060c00aS猫头猫                  return (
294060c00aS猫头猫                      <ResultWrapper
304f2deeb0S猫头猫                          tab={tab}
31a4ae8da5S猫头猫                          searchResult={pluginSearchResult}
324f2deeb0S猫头猫                          pluginHash={pluginHash}
334060c00aS猫头猫                          pluginName={pluginName}
344060c00aS猫头猫                      />
354060c00aS猫头猫                  );
36a4ae8da5S猫头猫              },
37d139abf1S猫头猫              () => true,
38d139abf1S猫头猫          )
394060c00aS猫头猫        : () => <DefaultResults />;
40bf6e62f2S猫头猫}
41bf6e62f2S猫头猫
42bf6e62f2S猫头猫/** 结果scene */
43bf6e62f2S猫头猫function getSubRouterScene(
44bf6e62f2S猫头猫    tab: ICommon.SupportMediaType,
45bf6e62f2S猫头猫    routes: Array<{key: string; title: string}>,
46bf6e62f2S猫头猫) {
47d139abf1S猫头猫    const scene: Record<string, React.FC> = {};
48bf6e62f2S猫头猫    routes.forEach(r => {
499d40a3faS猫头猫        scene[r.key] = getResultComponent(tab, r.key, r.title);
50bf6e62f2S猫头猫    });
51bf6e62f2S猫头猫    return SceneMap(scene);
52bf6e62f2S猫头猫}
53bf6e62f2S猫头猫
54d139abf1S猫头猫function ResultSubPanel(props: IResultSubPanelProps) {
55bf6e62f2S猫头猫    const [index, setIndex] = useState(0);
569d40a3faS猫头猫    // todo 是否聚合结果,如果是的话
578b88e961S猫头猫    const routes = PluginManager.getValidPlugins().map(_ => ({
58d139abf1S猫头猫        key: _.hash,
599d40a3faS猫头猫        title: _.name,
600b940038S猫头猫    }));
61d139abf1S猫头猫
62bf6e62f2S猫头猫    return (
63bf6e62f2S猫头猫        <TabView
640b940038S猫头猫            lazy
65bf6e62f2S猫头猫            navigationState={{
66bf6e62f2S猫头猫                index,
67bf6e62f2S猫头猫                routes,
68bf6e62f2S猫头猫            }}
694060c00aS猫头猫            renderTabBar={_ => (
70bf6e62f2S猫头猫                <TabBar
714060c00aS猫头猫                    {..._}
72*ef60be1cS猫头猫                    scrollEnabled
73bf6e62f2S猫头猫                    style={{
74bf6e62f2S猫头猫                        backgroundColor: 'transparent',
75bf6e62f2S猫头猫                        shadowColor: 'transparent',
76bf6e62f2S猫头猫                        borderColor: 'transparent',
77bf6e62f2S猫头猫                    }}
78bf6e62f2S猫头猫                    tabStyle={{
7920e2869eS猫头猫                        width: rpx(200),
80bf6e62f2S猫头猫                    }}
81bf6e62f2S猫头猫                    renderIndicator={() => null}
82bf6e62f2S猫头猫                    pressColor="transparent"
83bf6e62f2S猫头猫                    renderLabel={({route, focused, color}) => (
84bf6e62f2S猫头猫                        <Text
854f2deeb0S猫头猫                            numberOfLines={1}
86bf6e62f2S猫头猫                            style={{
87d139abf1S猫头猫                                fontWeight: focused
88d139abf1S猫头猫                                    ? fontWeightConst.bolder
89d139abf1S猫头猫                                    : fontWeightConst.bold,
90bf6e62f2S猫头猫                                color,
91bf6e62f2S猫头猫                            }}>
92bf6e62f2S猫头猫                            {route.title ?? '(未命名)'}
93bf6e62f2S猫头猫                        </Text>
944060c00aS猫头猫                    )}
954060c00aS猫头猫                />
96bf6e62f2S猫头猫            )}
97d139abf1S猫头猫            renderScene={useCallback(getSubRouterScene(props.tab, routes), [
98d139abf1S猫头猫                props.tab,
99d139abf1S猫头猫            ])}
100bf6e62f2S猫头猫            onIndexChange={setIndex}
1014060c00aS猫头猫            initialLayout={{width: rpx(750)}}
1024060c00aS猫头猫        />
103bf6e62f2S猫头猫    );
104bf6e62f2S猫头猫}
105d139abf1S猫头猫
106d139abf1S猫头猫// 不然会一直重新渲染
107d139abf1S猫头猫export default memo(ResultSubPanel);
108