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