xref: /MusicFree/src/pages/searchPage/components/resultPanel/resultSubPanel.tsx (revision a4ae8da553c6798d1634b4da485abd00e53dbd67)
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 DefaultResults from './results/defaultResults';
7bf6e62f2S猫头猫import {renderMap} from './results';
8bf6e62f2S猫头猫import ResultWrapper from './resultWrapper';
9bf6e62f2S猫头猫import {fontWeightConst} from '@/constants/uiConst';
10*a4ae8da5S猫头猫import { useAtomValue } from 'jotai';
11*a4ae8da5S猫头猫import { searchResultsAtom } from '../../store/atoms';
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(
25*a4ae8da5S猫头猫        () => {
26*a4ae8da5S猫头猫          const searchResults = useAtomValue(searchResultsAtom);
27*a4ae8da5S猫头猫          const pluginSearchResult = searchResults[tab][pluginHash];
28*a4ae8da5S猫头猫          // 写的不对
29*a4ae8da5S猫头猫          useEffect(() => {
30*a4ae8da5S猫头猫            console.log(searchResults, 'results');
31*a4ae8da5S猫头猫          }, [searchResults])
32*a4ae8da5S猫头猫          return <ResultWrapper
334f2deeb0S猫头猫            tab={tab}
34*a4ae8da5S猫头猫            searchResult={pluginSearchResult}
354f2deeb0S猫头猫            pluginHash={pluginHash}
364f2deeb0S猫头猫            pluginName={pluginName}></ResultWrapper>
37*a4ae8da5S猫头猫        },
38d139abf1S猫头猫        () => true,
39d139abf1S猫头猫      )
40bf6e62f2S猫头猫    : () => <DefaultResults></DefaultResults>;
41bf6e62f2S猫头猫}
42bf6e62f2S猫头猫
43bf6e62f2S猫头猫/** 结果scene */
44bf6e62f2S猫头猫function getSubRouterScene(
45bf6e62f2S猫头猫  tab: ICommon.SupportMediaType,
46bf6e62f2S猫头猫  routes: Array<{key: string; title: string}>,
47bf6e62f2S猫头猫) {
48d139abf1S猫头猫  const scene: Record<string, React.FC> = {};
49bf6e62f2S猫头猫  routes.forEach(r => {
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);
579d40a3faS猫头猫  // todo 是否聚合结果,如果是的话
580b940038S猫头猫  const routes = pluginManager.getPlugins().map(_ => ({
59d139abf1S猫头猫    key: _.hash,
609d40a3faS猫头猫    title: _.name,
610b940038S猫头猫  }));
62d139abf1S猫头猫
63bf6e62f2S猫头猫  return (
64bf6e62f2S猫头猫    <TabView
650b940038S猫头猫      lazy
66bf6e62f2S猫头猫      navigationState={{
67bf6e62f2S猫头猫        index,
68bf6e62f2S猫头猫        routes,
69bf6e62f2S猫头猫      }}
70bf6e62f2S猫头猫      renderTabBar={props => (
71bf6e62f2S猫头猫        <TabBar
72bf6e62f2S猫头猫          {...props}
73bf6e62f2S猫头猫          style={{
74bf6e62f2S猫头猫            backgroundColor: 'transparent',
75bf6e62f2S猫头猫            shadowColor: 'transparent',
76bf6e62f2S猫头猫            borderColor: 'transparent',
77bf6e62f2S猫头猫          }}
78bf6e62f2S猫头猫          tabStyle={{
79bf6e62f2S猫头猫            width: rpx(128),
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>
94bf6e62f2S猫头猫          )}></TabBar>
95bf6e62f2S猫头猫      )}
96d139abf1S猫头猫      renderScene={useCallback(getSubRouterScene(props.tab, routes), [
97d139abf1S猫头猫        props.tab,
98d139abf1S猫头猫      ])}
99bf6e62f2S猫头猫      onIndexChange={setIndex}
100bf6e62f2S猫头猫      initialLayout={{width: rpx(750)}}></TabView>
101bf6e62f2S猫头猫  );
102bf6e62f2S猫头猫}
103d139abf1S猫头猫
104d139abf1S猫头猫// 不然会一直重新渲染
105d139abf1S猫头猫export default memo(ResultSubPanel);
106