xref: /MusicFree/src/pages/searchPage/components/historyPanel.tsx (revision 734113be9d256a2b4d36bb272d6d3565beaeb236)
1import React, {useEffect, useState} from 'react';
2import {ScrollView, StyleSheet, View} from 'react-native';
3import rpx from '@/utils/rpx';
4import Loading from '@/components/base/loading';
5import {Chip} from 'react-native-paper';
6import useSearch from '../hooks/useSearch';
7import {
8    addHistory,
9    getHistory,
10    removeAllHistory,
11    removeHistory,
12} from '../common/historySearch';
13import {useSetAtom} from 'jotai';
14import {
15    initSearchResults,
16    PageStatus,
17    pageStatusAtom,
18    queryAtom,
19    searchResultsAtom,
20} from '../store/atoms';
21import ThemeText from '@/components/base/themeText';
22import Button from '@/components/base/button';
23
24export default function () {
25    const [history, setHistory] = useState<string[] | null>(null);
26    const search = useSearch();
27
28    const setQuery = useSetAtom(queryAtom);
29    const setPageStatus = useSetAtom(pageStatusAtom);
30    const setSearchResultsState = useSetAtom(searchResultsAtom);
31
32    useEffect(() => {
33        getHistory().then(setHistory);
34    }, []);
35
36    return (
37        <View style={style.wrapper}>
38            {history === null ? (
39                <Loading />
40            ) : (
41                <>
42                    <View style={style.header}>
43                        <ThemeText fontSize="title" fontWeight="semibold">
44                            历史记录
45                        </ThemeText>
46                        <Button
47                            fontColor="secondary"
48                            onPress={async () => {
49                                await removeAllHistory();
50                                getHistory().then(setHistory);
51                            }}>
52                            清空
53                        </Button>
54                    </View>
55                    <ScrollView
56                        style={style.historyContent}
57                        contentContainerStyle={style.historyContentConainer}>
58                        {history.map(_ => (
59                            <Chip
60                                key={`search-history-${_}`}
61                                style={style.chip}
62                                onClose={async () => {
63                                    await removeHistory(_);
64                                    getHistory().then(setHistory);
65                                }}
66                                onPress={() => {
67                                    setSearchResultsState(initSearchResults);
68                                    setPageStatus(PageStatus.SEARCHING);
69                                    search(_, 1);
70                                    addHistory(_);
71                                    setQuery(_);
72                                }}>
73                                {_}
74                            </Chip>
75                        ))}
76                    </ScrollView>
77                </>
78            )}
79        </View>
80    );
81}
82
83const style = StyleSheet.create({
84    wrapper: {
85        width: '100%',
86        maxWidth: '100%',
87        flexDirection: 'column',
88        padding: rpx(24),
89        flex: 1,
90    },
91    header: {
92        width: '100%',
93        flexDirection: 'row',
94        paddingVertical: rpx(28),
95        justifyContent: 'space-between',
96        alignItems: 'center',
97    },
98    historyContent: {
99        width: '100%',
100        flex: 1,
101    },
102    historyContentConainer: {
103        flexDirection: 'row',
104        flexWrap: 'wrap',
105    },
106    chip: {
107        flexGrow: 0,
108        marginRight: rpx(24),
109        marginBottom: rpx(24),
110    },
111});
112