xref: /MusicFree/src/pages/searchPage/components/resultPanel/resultSubPanel.tsx (revision 927dbe935e5ed3130d85ec27a6ac3efd1a163ad1)
1d139abf1S猫头猫import React, {memo, useCallback, useEffect, useState} from 'react';
2bf6e62f2S猫头猫import {StyleSheet, Text, View} 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';
11*927dbe93S猫头猫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];
28a4ae8da5S猫头猫          return <ResultWrapper
294f2deeb0S猫头猫            tab={tab}
30a4ae8da5S猫头猫            searchResult={pluginSearchResult}
314f2deeb0S猫头猫            pluginHash={pluginHash}
324f2deeb0S猫头猫            pluginName={pluginName}></ResultWrapper>
33a4ae8da5S猫头猫        },
34d139abf1S猫头猫        () => true,
35d139abf1S猫头猫      )
36bf6e62f2S猫头猫    : () => <DefaultResults></DefaultResults>;
37bf6e62f2S猫头猫}
38bf6e62f2S猫头猫
39bf6e62f2S猫头猫/** 结果scene */
40bf6e62f2S猫头猫function getSubRouterScene(
41bf6e62f2S猫头猫  tab: ICommon.SupportMediaType,
42bf6e62f2S猫头猫  routes: Array<{key: string; title: string}>,
43bf6e62f2S猫头猫) {
44d139abf1S猫头猫  const scene: Record<string, React.FC> = {};
45bf6e62f2S猫头猫  routes.forEach(r => {
469d40a3faS猫头猫    scene[r.key] = getResultComponent(tab, r.key, r.title);
47bf6e62f2S猫头猫  });
48bf6e62f2S猫头猫  return SceneMap(scene);
49bf6e62f2S猫头猫}
50bf6e62f2S猫头猫
51d139abf1S猫头猫function ResultSubPanel(props: IResultSubPanelProps) {
52bf6e62f2S猫头猫  const [index, setIndex] = useState(0);
539d40a3faS猫头猫  // todo 是否聚合结果,如果是的话
548b88e961S猫头猫  const routes = PluginManager.getValidPlugins().map(_ => ({
55d139abf1S猫头猫    key: _.hash,
569d40a3faS猫头猫    title: _.name,
570b940038S猫头猫  }));
58d139abf1S猫头猫
59bf6e62f2S猫头猫  return (
60bf6e62f2S猫头猫    <TabView
610b940038S猫头猫      lazy
62bf6e62f2S猫头猫      navigationState={{
63bf6e62f2S猫头猫        index,
64bf6e62f2S猫头猫        routes,
65bf6e62f2S猫头猫      }}
66bf6e62f2S猫头猫      renderTabBar={props => (
67bf6e62f2S猫头猫        <TabBar
68bf6e62f2S猫头猫          {...props}
69bf6e62f2S猫头猫          style={{
70bf6e62f2S猫头猫            backgroundColor: 'transparent',
71bf6e62f2S猫头猫            shadowColor: 'transparent',
72bf6e62f2S猫头猫            borderColor: 'transparent',
73bf6e62f2S猫头猫          }}
74bf6e62f2S猫头猫          tabStyle={{
7520e2869eS猫头猫            width: rpx(200),
76bf6e62f2S猫头猫          }}
77bf6e62f2S猫头猫          renderIndicator={() => null}
78bf6e62f2S猫头猫          pressColor="transparent"
79bf6e62f2S猫头猫          renderLabel={({route, focused, color}) => (
80bf6e62f2S猫头猫            <Text
814f2deeb0S猫头猫              numberOfLines={1}
82bf6e62f2S猫头猫              style={{
83d139abf1S猫头猫                fontWeight: focused
84d139abf1S猫头猫                  ? fontWeightConst.bolder
85d139abf1S猫头猫                  : fontWeightConst.bold,
86bf6e62f2S猫头猫                color,
87bf6e62f2S猫头猫              }}>
88bf6e62f2S猫头猫              {route.title ?? '(未命名)'}
89bf6e62f2S猫头猫            </Text>
90bf6e62f2S猫头猫          )}></TabBar>
91bf6e62f2S猫头猫      )}
92d139abf1S猫头猫      renderScene={useCallback(getSubRouterScene(props.tab, routes), [
93d139abf1S猫头猫        props.tab,
94d139abf1S猫头猫      ])}
95bf6e62f2S猫头猫      onIndexChange={setIndex}
96bf6e62f2S猫头猫      initialLayout={{width: rpx(750)}}></TabView>
97bf6e62f2S猫头猫  );
98bf6e62f2S猫头猫}
99d139abf1S猫头猫
100d139abf1S猫头猫// 不然会一直重新渲染
101d139abf1S猫头猫export default memo(ResultSubPanel);
102