xref: /MusicFree/src/pages/searchPage/components/resultPanel/resultSubPanel.tsx (revision 9d40a3fa971e70a778b405ee3569bce02856db4c)
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';
5d139abf1S猫头猫import {pluginManager, usePlugins} from '@/common/pluginManager';
6bf6e62f2S猫头猫import MusicResults from './results/musicResults';
7bf6e62f2S猫头猫import AlbumResults from './results/albumResults';
8bf6e62f2S猫头猫import DefaultResults from './results/defaultResults';
9bf6e62f2S猫头猫import {renderMap} from './results';
10bf6e62f2S猫头猫import ResultWrapper from './resultWrapper';
11bf6e62f2S猫头猫import {fontWeightConst} from '@/constants/uiConst';
12bf6e62f2S猫头猫
13bf6e62f2S猫头猫interface IResultSubPanelProps {
14bf6e62f2S猫头猫  tab: ICommon.SupportMediaType;
15bf6e62f2S猫头猫}
16bf6e62f2S猫头猫
17bf6e62f2S猫头猫// 展示结果的视图
18*9d40a3faS猫头猫function getResultComponent(tab: ICommon.SupportMediaType, pluginHash: string, pluginName: string) {
19bf6e62f2S猫头猫  return tab in renderMap
20d139abf1S猫头猫    ? memo(
21*9d40a3faS猫头猫        () => <ResultWrapper tab={tab} pluginHash={pluginHash} pluginName={pluginName}></ResultWrapper>,
22d139abf1S猫头猫        () => true,
23d139abf1S猫头猫      )
24bf6e62f2S猫头猫    : () => <DefaultResults></DefaultResults>;
25bf6e62f2S猫头猫}
26bf6e62f2S猫头猫
27bf6e62f2S猫头猫/** 结果scene */
28bf6e62f2S猫头猫function getSubRouterScene(
29bf6e62f2S猫头猫  tab: ICommon.SupportMediaType,
30bf6e62f2S猫头猫  routes: Array<{key: string; title: string}>,
31bf6e62f2S猫头猫) {
32d139abf1S猫头猫  const scene: Record<string, React.FC> = {};
33bf6e62f2S猫头猫  routes.forEach(r => {
34*9d40a3faS猫头猫    scene[r.key] = getResultComponent(tab, r.key, r.title);
35bf6e62f2S猫头猫  });
36bf6e62f2S猫头猫  return SceneMap(scene);
37bf6e62f2S猫头猫}
38bf6e62f2S猫头猫
39d139abf1S猫头猫function ResultSubPanel(props: IResultSubPanelProps) {
40bf6e62f2S猫头猫  const [index, setIndex] = useState(0);
41*9d40a3faS猫头猫  // todo 是否聚合结果,如果是的话
42d139abf1S猫头猫  const routes = [
43d139abf1S猫头猫    {
44d139abf1S猫头猫      key: 'all',
45d139abf1S猫头猫      title: '全部',
46d139abf1S猫头猫    },
47d139abf1S猫头猫  ].concat(
48d139abf1S猫头猫    pluginManager.getPlugins().map(_ => ({
49d139abf1S猫头猫      key: _.hash,
50*9d40a3faS猫头猫      title: _.name,
51d139abf1S猫头猫    })),
52d139abf1S猫头猫  );
53d139abf1S猫头猫
54bf6e62f2S猫头猫  return (
55bf6e62f2S猫头猫    <TabView
56bf6e62f2S猫头猫      navigationState={{
57bf6e62f2S猫头猫        index,
58bf6e62f2S猫头猫        routes,
59bf6e62f2S猫头猫      }}
60bf6e62f2S猫头猫      renderTabBar={props => (
61bf6e62f2S猫头猫        <TabBar
62bf6e62f2S猫头猫          {...props}
63bf6e62f2S猫头猫          style={{
64bf6e62f2S猫头猫            backgroundColor: 'transparent',
65bf6e62f2S猫头猫            shadowColor: 'transparent',
66bf6e62f2S猫头猫            borderColor: 'transparent',
67bf6e62f2S猫头猫          }}
68bf6e62f2S猫头猫          tabStyle={{
69bf6e62f2S猫头猫            width: rpx(128),
70bf6e62f2S猫头猫          }}
71bf6e62f2S猫头猫          renderIndicator={() => null}
72bf6e62f2S猫头猫          pressColor="transparent"
73bf6e62f2S猫头猫          renderLabel={({route, focused, color}) => (
74bf6e62f2S猫头猫            <Text
75bf6e62f2S猫头猫              style={{
76d139abf1S猫头猫                fontWeight: focused
77d139abf1S猫头猫                  ? fontWeightConst.bolder
78d139abf1S猫头猫                  : fontWeightConst.bold,
79bf6e62f2S猫头猫                color,
80bf6e62f2S猫头猫              }}>
81bf6e62f2S猫头猫              {route.title ?? '(未命名)'}
82bf6e62f2S猫头猫            </Text>
83bf6e62f2S猫头猫          )}></TabBar>
84bf6e62f2S猫头猫      )}
85d139abf1S猫头猫      renderScene={useCallback(getSubRouterScene(props.tab, routes), [
86d139abf1S猫头猫        props.tab,
87d139abf1S猫头猫      ])}
88bf6e62f2S猫头猫      onIndexChange={setIndex}
89bf6e62f2S猫头猫      initialLayout={{width: rpx(750)}}></TabView>
90bf6e62f2S猫头猫  );
91bf6e62f2S猫头猫}
92d139abf1S猫头猫
93d139abf1S猫头猫// 不然会一直重新渲染
94d139abf1S猫头猫export default memo(ResultSubPanel);
95