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