1import { useNavigation, useRoute } from "@react-navigation/native"; 2import { useCallback } from "react"; 3import { LogBox } from "react-native"; 4 5LogBox.ignoreLogs([ 6 'Non-serializable values were found in the navigation state', 7]); 8 9/** 路由key */ 10export const ROUTE_PATH = { 11 /** 主页 */ 12 HOME: 'home', 13 /** 音乐播放页 */ 14 MUSIC_DETAIL: 'music-detail', 15 /** 搜索页 */ 16 SEARCH_PAGE: 'search-page', 17 /** 本地歌单页 */ 18 LOCAL_SHEET_DETAIL: 'local-sheet-detail', 19 /** 专辑页 */ 20 ALBUM_DETAIL: 'album-detail', 21 /** 歌手页 */ 22 ARTIST_DETAIL: 'artist-detail', 23 /** 榜单页 */ 24 TOP_LIST: 'top-list', 25 /** 榜单详情页 */ 26 TOP_LIST_DETAIL: 'top-list-detail', 27 /** 设置页 */ 28 SETTING: 'setting', 29 /** 本地音乐 */ 30 LOCAL: 'local', 31 /** 正在下载 */ 32 DOWNLOADING: 'downloading', 33 /** 从歌曲列表中搜索 */ 34 SEARCH_MUSIC_LIST: 'search-music-list', 35 /** 批量编辑 */ 36 MUSIC_LIST_EDITOR: 'music-list-editor', 37 /** 选择文件夹 */ 38 FILE_SELECTOR: 'file-selector', 39 /** 推荐歌单 */ 40 RECOMMEND_SHEETS: 'recommend-sheets', 41 /** 歌单详情 */ 42 PLUGIN_SHEET_DETAIL: 'plugin-sheet-detail', 43 /** 历史记录 */ 44 HISTORY: 'history', 45 /** 自定义主题 */ 46 SET_CUSTOM_THEME: 'set-custom-theme', 47 /** 权限管理 */ 48 PERMISSIONS: 'permissions', 49} as const; 50 51type ValueOf<T> = T[keyof T]; 52type RoutePaths = ValueOf<typeof ROUTE_PATH>; 53 54type RouterParamsBase = Record<RoutePaths, any>; 55/** 路由参数 */ 56interface RouterParams extends RouterParamsBase { 57 home: undefined; 58 'music-detail': undefined; 59 'search-page': undefined; 60 'local-sheet-detail': { 61 id: string; 62 }; 63 'album-detail': { 64 albumItem: IAlbum.IAlbumItem; 65 }; 66 'artist-detail': { 67 artistItem: IArtist.IArtistItem; 68 pluginHash: string; 69 }; 70 setting: { 71 type: string; 72 // anchor?: string | number; 73 }; 74 local: undefined; 75 downloading: undefined; 76 'search-music-list': { 77 musicList: IMusic.IMusicItem[] | null; 78 musicSheet?: IMusic.IMusicSheetItem; 79 }; 80 'music-list-editor': { 81 musicSheet?: Partial<IMusic.IMusicSheetItem>; 82 musicList: IMusic.IMusicItem[] | null; 83 }; 84 'file-selector': { 85 fileType?: 'folder' | 'file' | 'file-and-folder'; // 10: folder 11: file and folder, 86 multi?: boolean; // 是否多选 87 actionText?: string; // 底部行动点的文本 88 actionIcon?: string; // 底部行动点的图标 89 onAction?: ( 90 selectedFiles: { 91 path: string; 92 type: 'file' | 'folder'; 93 }[], 94 ) => Promise<boolean>; // true会自动关闭,false会停在当前页面 95 matchExtension?: (path: string) => boolean; 96 }; 97 'top-list-detail': { 98 pluginHash: string; 99 topList: IMusic.IMusicSheetItemBase; 100 }; 101 'plugin-sheet-detail': { 102 pluginHash?: string; 103 sheetInfo: IMusic.IMusicSheetItemBase; 104 }; 105} 106 107/** 路由参数Hook */ 108export function useParams<T extends RoutePaths>(): RouterParams[T] { 109 const route = useRoute<any>(); 110 111 const routeParams = route?.params as RouterParams[T]; 112 return routeParams; 113} 114 115/** 导航 */ 116export function useNavigate() { 117 const navigation = useNavigation<any>(); 118 119 const navigate = useCallback(function <T extends RoutePaths>( 120 route: T, 121 params?: RouterParams[T], 122 ) { 123 navigation.navigate(route, params); 124 }, 125 []); 126 127 return navigate; 128} 129