xref: /MusicFree/src/pages/searchPage/components/historyPanel.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 Loading from '@/components/loading';
5*bf6e62f2S猫头猫import {Chip, useTheme} from 'react-native-paper';
6*bf6e62f2S猫头猫import useSearch from '../hooks/useSearch';
7*bf6e62f2S猫头猫import {addHistory, getHistory, removeHistory} from '../common/historySearch';
8*bf6e62f2S猫头猫import {useSetAtom} from 'jotai';
9*bf6e62f2S猫头猫import {
10*bf6e62f2S猫头猫  PageStatus,
11*bf6e62f2S猫头猫  pageStatusAtom,
12*bf6e62f2S猫头猫  queryAtom,
13*bf6e62f2S猫头猫  searchResultsAtom,
14*bf6e62f2S猫头猫} from '../store/atoms';
15*bf6e62f2S猫头猫import {fontSizeConst, fontWeightConst} from '@/constants/uiConst';
16*bf6e62f2S猫头猫
17*bf6e62f2S猫头猫interface IProps {}
18*bf6e62f2S猫头猫export default function (props: IProps) {
19*bf6e62f2S猫头猫  const [history, setHistory] = useState<string[] | null>(null);
20*bf6e62f2S猫头猫  const search = useSearch();
21*bf6e62f2S猫头猫
22*bf6e62f2S猫头猫  const setQuery = useSetAtom(queryAtom);
23*bf6e62f2S猫头猫  const setPageStatus = useSetAtom(pageStatusAtom);
24*bf6e62f2S猫头猫  const setSearchResultsState = useSetAtom(searchResultsAtom);
25*bf6e62f2S猫头猫  const {colors} = useTheme();
26*bf6e62f2S猫头猫
27*bf6e62f2S猫头猫  useEffect(() => {
28*bf6e62f2S猫头猫    getHistory().then(setHistory);
29*bf6e62f2S猫头猫  }, []);
30*bf6e62f2S猫头猫
31*bf6e62f2S猫头猫  return (
32*bf6e62f2S猫头猫    <View style={style.wrapper}>
33*bf6e62f2S猫头猫      {history === null ? (
34*bf6e62f2S猫头猫        <Loading></Loading>
35*bf6e62f2S猫头猫      ) : (
36*bf6e62f2S猫头猫        <>
37*bf6e62f2S猫头猫          <Text style={[style.title, {color: colors.text}]}>历史记录</Text>
38*bf6e62f2S猫头猫          {history.map(_ => (
39*bf6e62f2S猫头猫            <Chip
40*bf6e62f2S猫头猫              key={`search-history-${_}`}
41*bf6e62f2S猫头猫              style={style.chip}
42*bf6e62f2S猫头猫              onClose={async () => {
43*bf6e62f2S猫头猫                await removeHistory(_);
44*bf6e62f2S猫头猫                getHistory().then(setHistory);
45*bf6e62f2S猫头猫              }}
46*bf6e62f2S猫头猫              onPress={() => {
47*bf6e62f2S猫头猫                search(_, 'all');
48*bf6e62f2S猫头猫                addHistory(_);
49*bf6e62f2S猫头猫                setPageStatus(PageStatus.SEARCHING);
50*bf6e62f2S猫头猫                setSearchResultsState({});
51*bf6e62f2S猫头猫                setQuery(_);
52*bf6e62f2S猫头猫              }}>
53*bf6e62f2S猫头猫              {_}
54*bf6e62f2S猫头猫            </Chip>
55*bf6e62f2S猫头猫          ))}
56*bf6e62f2S猫头猫        </>
57*bf6e62f2S猫头猫      )}
58*bf6e62f2S猫头猫    </View>
59*bf6e62f2S猫头猫  );
60*bf6e62f2S猫头猫}
61*bf6e62f2S猫头猫
62*bf6e62f2S猫头猫const style = StyleSheet.create({
63*bf6e62f2S猫头猫  wrapper: {
64*bf6e62f2S猫头猫    width: rpx(750),
65*bf6e62f2S猫头猫    maxWidth: rpx(750),
66*bf6e62f2S猫头猫    flexDirection: 'row',
67*bf6e62f2S猫头猫    flexWrap: 'wrap',
68*bf6e62f2S猫头猫    padding: rpx(24),
69*bf6e62f2S猫头猫  },
70*bf6e62f2S猫头猫  title: {
71*bf6e62f2S猫头猫    width: '100%',
72*bf6e62f2S猫头猫    marginBottom: rpx(28),
73*bf6e62f2S猫头猫    fontSize: fontSizeConst.normal,
74*bf6e62f2S猫头猫    fontWeight: fontWeightConst.bold,
75*bf6e62f2S猫头猫  },
76*bf6e62f2S猫头猫  chip: {
77*bf6e62f2S猫头猫    flexGrow: 0,
78*bf6e62f2S猫头猫    marginRight: rpx(24),
79*bf6e62f2S猫头猫    marginBottom: rpx(24),
80*bf6e62f2S猫头猫  },
81*bf6e62f2S猫头猫});
82