xref: /MusicFree/src/pages/searchPage/components/resultPanel/resultSubPanel.tsx (revision efb9da24633a003d2b1a4a47f4bf16d38033266e)
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 => {
49*efb9da24S猫头猫        // todo: 是否声明不可搜索
509d40a3faS猫头猫        scene[r.key] = getResultComponent(tab, r.key, r.title);
51bf6e62f2S猫头猫    });
52bf6e62f2S猫头猫    return SceneMap(scene);
53bf6e62f2S猫头猫}
54bf6e62f2S猫头猫
55d139abf1S猫头猫function ResultSubPanel(props: IResultSubPanelProps) {
56bf6e62f2S猫头猫    const [index, setIndex] = useState(0);
57*efb9da24S猫头猫
58*efb9da24S猫头猫    const routes = PluginManager.getSearchablePlugins().map(_ => ({
59d139abf1S猫头猫        key: _.hash,
609d40a3faS猫头猫        title: _.name,
610b940038S猫头猫    }));
62d139abf1S猫头猫
63bf6e62f2S猫头猫    return (
64bf6e62f2S猫头猫        <TabView
650b940038S猫头猫            lazy
66bf6e62f2S猫头猫            navigationState={{
67bf6e62f2S猫头猫                index,
68bf6e62f2S猫头猫                routes,
69bf6e62f2S猫头猫            }}
704060c00aS猫头猫            renderTabBar={_ => (
71bf6e62f2S猫头猫                <TabBar
724060c00aS猫头猫                    {..._}
73ef60be1cS猫头猫                    scrollEnabled
74bf6e62f2S猫头猫                    style={{
75bf6e62f2S猫头猫                        backgroundColor: 'transparent',
76bf6e62f2S猫头猫                        shadowColor: 'transparent',
77bf6e62f2S猫头猫                        borderColor: 'transparent',
78bf6e62f2S猫头猫                    }}
79bf6e62f2S猫头猫                    tabStyle={{
8020e2869eS猫头猫                        width: rpx(200),
81bf6e62f2S猫头猫                    }}
82bf6e62f2S猫头猫                    renderIndicator={() => null}
83bf6e62f2S猫头猫                    pressColor="transparent"
84bf6e62f2S猫头猫                    renderLabel={({route, focused, color}) => (
85bf6e62f2S猫头猫                        <Text
864f2deeb0S猫头猫                            numberOfLines={1}
87bf6e62f2S猫头猫                            style={{
88d139abf1S猫头猫                                fontWeight: focused
89d139abf1S猫头猫                                    ? fontWeightConst.bolder
90d139abf1S猫头猫                                    : fontWeightConst.bold,
91bf6e62f2S猫头猫                                color,
92bf6e62f2S猫头猫                            }}>
93bf6e62f2S猫头猫                            {route.title ?? '(未命名)'}
94bf6e62f2S猫头猫                        </Text>
954060c00aS猫头猫                    )}
964060c00aS猫头猫                />
97bf6e62f2S猫头猫            )}
98d139abf1S猫头猫            renderScene={useCallback(getSubRouterScene(props.tab, routes), [
99d139abf1S猫头猫                props.tab,
100d139abf1S猫头猫            ])}
101bf6e62f2S猫头猫            onIndexChange={setIndex}
1024060c00aS猫头猫            initialLayout={{width: rpx(750)}}
1034060c00aS猫头猫        />
104bf6e62f2S猫头猫    );
105bf6e62f2S猫头猫}
106d139abf1S猫头猫
107d139abf1S猫头猫// 不然会一直重新渲染
108d139abf1S猫头猫export default memo(ResultSubPanel);
109