xref: /MusicFree/src/pages/searchPage/components/historyPanel.tsx (revision 410a159129b1f6a7a1f44fde7bfad9a46f91e161)
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 '@/components/base/chip';
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';
23import Empty from '@/components/base/empty';
24
25export default function () {
26    const [history, setHistory] = useState<string[] | null>(null);
27    const search = useSearch();
28
29    const setQuery = useSetAtom(queryAtom);
30    const setPageStatus = useSetAtom(pageStatusAtom);
31    const setSearchResultsState = useSetAtom(searchResultsAtom);
32
33    useEffect(() => {
34        getHistory().then(setHistory);
35    }, []);
36
37    return (
38        <View style={style.wrapper}>
39            {history === null ? (
40                <Loading />
41            ) : (
42                <>
43                    <View style={style.header}>
44                        <ThemeText fontSize="title" fontWeight="semibold">
45                            历史记录
46                        </ThemeText>
47                        <Button
48                            fontColor="textSecondary"
49                            onPress={async () => {
50                                await removeAllHistory();
51                                getHistory().then(setHistory);
52                            }}>
53                            清空
54                        </Button>
55                    </View>
56                    <ScrollView
57                        style={style.historyContent}
58                        contentContainerStyle={style.historyContentConainer}>
59                        {history.length ? (
60                            history.map(_ => (
61                                <Chip
62                                    key={`search-history-${_}`}
63                                    containerStyle={style.chip}
64                                    onClose={async () => {
65                                        await removeHistory(_);
66                                        getHistory().then(setHistory);
67                                    }}
68                                    onPress={() => {
69                                        setSearchResultsState(
70                                            initSearchResults,
71                                        );
72                                        setPageStatus(PageStatus.SEARCHING);
73                                        search(_, 1);
74                                        addHistory(_);
75                                        setQuery(_);
76                                    }}>
77                                    {_}
78                                </Chip>
79                            ))
80                        ) : (
81                            <Empty />
82                        )}
83                    </ScrollView>
84                </>
85            )}
86        </View>
87    );
88}
89
90const style = StyleSheet.create({
91    wrapper: {
92        width: '100%',
93        maxWidth: '100%',
94        flexDirection: 'column',
95        padding: rpx(24),
96        flex: 1,
97    },
98    header: {
99        width: '100%',
100        flexDirection: 'row',
101        paddingVertical: rpx(28),
102        justifyContent: 'space-between',
103        alignItems: 'center',
104    },
105    historyContent: {
106        width: '100%',
107        flex: 1,
108    },
109    historyContentConainer: {
110        flexDirection: 'row',
111        flexWrap: 'wrap',
112    },
113    chip: {
114        flexGrow: 0,
115        marginRight: rpx(24),
116        marginBottom: rpx(24),
117    },
118});
119