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