xref: /MusicFree/src/pages/searchPage/components/historyPanel.tsx (revision fe32deaa67e69870228108e3ac35afbeeeef61d4)
1bf6e62f2S猫头猫import React, {useEffect, useState} from 'react';
24060c00aS猫头猫import {ScrollView, StyleSheet, View} from 'react-native';
3bf6e62f2S猫头猫import rpx from '@/utils/rpx';
419dc08ecS猫头猫import Loading from '@/components/base/loading';
5*fe32deaaS猫头猫import Chip from '@/components/base/chip';
6bf6e62f2S猫头猫import useSearch from '../hooks/useSearch';
74f168b1fS猫头猫import {
84f168b1fS猫头猫    addHistory,
94f168b1fS猫头猫    getHistory,
104f168b1fS猫头猫    removeAllHistory,
114f168b1fS猫头猫    removeHistory,
124f168b1fS猫头猫} from '../common/historySearch';
13bf6e62f2S猫头猫import {useSetAtom} from 'jotai';
14bf6e62f2S猫头猫import {
15a4ae8da5S猫头猫    initSearchResults,
16bf6e62f2S猫头猫    PageStatus,
17bf6e62f2S猫头猫    pageStatusAtom,
18bf6e62f2S猫头猫    queryAtom,
19bf6e62f2S猫头猫    searchResultsAtom,
20bf6e62f2S猫头猫} from '../store/atoms';
2119dc08ecS猫头猫import ThemeText from '@/components/base/themeText';
224f168b1fS猫头猫import Button from '@/components/base/button';
23bf6e62f2S猫头猫
244060c00aS猫头猫export default function () {
25bf6e62f2S猫头猫    const [history, setHistory] = useState<string[] | null>(null);
26bf6e62f2S猫头猫    const search = useSearch();
27bf6e62f2S猫头猫
28bf6e62f2S猫头猫    const setQuery = useSetAtom(queryAtom);
29bf6e62f2S猫头猫    const setPageStatus = useSetAtom(pageStatusAtom);
30bf6e62f2S猫头猫    const setSearchResultsState = useSetAtom(searchResultsAtom);
31bf6e62f2S猫头猫
32bf6e62f2S猫头猫    useEffect(() => {
3319c8eb6fS猫头猫        getHistory().then(setHistory);
34bf6e62f2S猫头猫    }, []);
35bf6e62f2S猫头猫
36bf6e62f2S猫头猫    return (
37bf6e62f2S猫头猫        <View style={style.wrapper}>
38bf6e62f2S猫头猫            {history === null ? (
394060c00aS猫头猫                <Loading />
40bf6e62f2S猫头猫            ) : (
41bf6e62f2S猫头猫                <>
424f168b1fS猫头猫                    <View style={style.header}>
434f168b1fS猫头猫                        <ThemeText fontSize="title" fontWeight="semibold">
442fac4245S猫头猫                            历史记录
452fac4245S猫头猫                        </ThemeText>
464f168b1fS猫头猫                        <Button
474f168b1fS猫头猫                            fontColor="secondary"
484f168b1fS猫头猫                            onPress={async () => {
494f168b1fS猫头猫                                await removeAllHistory();
504f168b1fS猫头猫                                getHistory().then(setHistory);
514f168b1fS猫头猫                            }}>
524f168b1fS猫头猫                            清空
534f168b1fS猫头猫                        </Button>
544f168b1fS猫头猫                    </View>
552fac4245S猫头猫                    <ScrollView
562fac4245S猫头猫                        style={style.historyContent}
572fac4245S猫头猫                        contentContainerStyle={style.historyContentConainer}>
58bf6e62f2S猫头猫                        {history.map(_ => (
59bf6e62f2S猫头猫                            <Chip
60bf6e62f2S猫头猫                                key={`search-history-${_}`}
61*fe32deaaS猫头猫                                containerStyle={style.chip}
62bf6e62f2S猫头猫                                onClose={async () => {
63bf6e62f2S猫头猫                                    await removeHistory(_);
64bf6e62f2S猫头猫                                    getHistory().then(setHistory);
65bf6e62f2S猫头猫                                }}
66bf6e62f2S猫头猫                                onPress={() => {
67a4ae8da5S猫头猫                                    setSearchResultsState(initSearchResults);
68c30d30e1S猫头猫                                    setPageStatus(PageStatus.SEARCHING);
690b940038S猫头猫                                    search(_, 1);
70bf6e62f2S猫头猫                                    addHistory(_);
71bf6e62f2S猫头猫                                    setQuery(_);
72bf6e62f2S猫头猫                                }}>
73bf6e62f2S猫头猫                                {_}
74bf6e62f2S猫头猫                            </Chip>
75bf6e62f2S猫头猫                        ))}
762fac4245S猫头猫                    </ScrollView>
77bf6e62f2S猫头猫                </>
78bf6e62f2S猫头猫            )}
79bf6e62f2S猫头猫        </View>
80bf6e62f2S猫头猫    );
81bf6e62f2S猫头猫}
82bf6e62f2S猫头猫
83bf6e62f2S猫头猫const style = StyleSheet.create({
84bf6e62f2S猫头猫    wrapper: {
85c446f2b8S猫头猫        width: '100%',
86c446f2b8S猫头猫        maxWidth: '100%',
872fac4245S猫头猫        flexDirection: 'column',
88bf6e62f2S猫头猫        padding: rpx(24),
892fac4245S猫头猫        flex: 1,
90bf6e62f2S猫头猫    },
914f168b1fS猫头猫    header: {
92bf6e62f2S猫头猫        width: '100%',
934f168b1fS猫头猫        flexDirection: 'row',
944f168b1fS猫头猫        paddingVertical: rpx(28),
954f168b1fS猫头猫        justifyContent: 'space-between',
964f168b1fS猫头猫        alignItems: 'center',
97bf6e62f2S猫头猫    },
982fac4245S猫头猫    historyContent: {
99c446f2b8S猫头猫        width: '100%',
1002fac4245S猫头猫        flex: 1,
1012fac4245S猫头猫    },
1022fac4245S猫头猫    historyContentConainer: {
1032fac4245S猫头猫        flexDirection: 'row',
1042fac4245S猫头猫        flexWrap: 'wrap',
1052fac4245S猫头猫    },
106bf6e62f2S猫头猫    chip: {
107bf6e62f2S猫头猫        flexGrow: 0,
108bf6e62f2S猫头猫        marginRight: rpx(24),
109bf6e62f2S猫头猫        marginBottom: rpx(24),
110bf6e62f2S猫头猫    },
111bf6e62f2S猫头猫});
112