|
1 | | -import { ChipsSet } from 'src/components/Elements' |
| 1 | +import { useMemo, useState } from 'react' |
| 2 | +import { ChipsSet, ConfirmModal } from 'src/components/Elements' |
2 | 3 | import { SettingsContentLayout } from 'src/components/Layout/SettingsContentLayout/SettingsContentLayout' |
3 | 4 | import { SUPPORTED_CARDS } from 'src/config/supportedCards' |
4 | 5 | import { trackSourceAdd, trackSourceRemove } from 'src/lib/analytics' |
5 | 6 | import { useUserPreferences } from 'src/stores/preferences' |
6 | | -import { SelectedCard } from 'src/types' |
| 7 | +import { Option, SelectedCard } from 'src/types' |
7 | 8 | import { RssSetting } from './RssSetting' |
8 | 9 |
|
9 | 10 | export const SourceSettings = () => { |
10 | | - const { cards, setCards, userCustomCards } = useUserPreferences() |
| 11 | + const { cards, setCards, userCustomCards, setUserCustomCards } = useUserPreferences() |
| 12 | + const [confirmDelete, setConfirmDelete] = useState<{ |
| 13 | + showModal: boolean |
| 14 | + option?: Option |
| 15 | + }>({ |
| 16 | + showModal: false, |
| 17 | + option: undefined, |
| 18 | + }) |
11 | 19 |
|
12 | | - const mergedSources = [ |
13 | | - ...SUPPORTED_CARDS.map((source) => { |
14 | | - return { |
15 | | - label: source.label, |
16 | | - value: source.value, |
17 | | - icon: source.icon, |
18 | | - } |
19 | | - }), |
20 | | - ...userCustomCards.map((source) => { |
21 | | - return { |
22 | | - label: source.label, |
23 | | - value: source.value, |
24 | | - icon: <img src={source.icon as string} alt="" />, |
25 | | - } |
26 | | - }), |
27 | | - ].sort((a, b) => (a.label > b.label ? 1 : -1)) |
| 20 | + const mergedSources = useMemo(() => { |
| 21 | + return [ |
| 22 | + ...SUPPORTED_CARDS.map((source) => { |
| 23 | + return { |
| 24 | + label: source.label, |
| 25 | + value: source.value, |
| 26 | + icon: source.icon, |
| 27 | + } |
| 28 | + }), |
| 29 | + ...userCustomCards.map((source) => { |
| 30 | + return { |
| 31 | + label: source.label, |
| 32 | + value: source.value, |
| 33 | + removeable: true, |
| 34 | + icon: <img src={source.icon as string} alt="" />, |
| 35 | + } |
| 36 | + }), |
| 37 | + ].sort((a, b) => (a.label > b.label ? 1 : -1)) |
| 38 | + }, [userCustomCards]) |
28 | 39 |
|
29 | 40 | return ( |
30 | 41 | <SettingsContentLayout |
31 | 42 | title="Sources" |
32 | 43 | description={`Your feed will be tailored by following the sources you are interested in.`}> |
33 | 44 | <> |
| 45 | + <ConfirmModal |
| 46 | + showModal={confirmDelete.showModal} |
| 47 | + title={`Confirm delete source: ${confirmDelete.option?.label}`} |
| 48 | + description={`Are you sure you want to delete ${confirmDelete.option?.label} source? This action cannot be undone.`} |
| 49 | + onClose={() => |
| 50 | + setConfirmDelete({ |
| 51 | + showModal: false, |
| 52 | + option: undefined, |
| 53 | + }) |
| 54 | + } |
| 55 | + onConfirm={() => { |
| 56 | + const newUserCards = userCustomCards.filter( |
| 57 | + (card) => card.value !== confirmDelete.option?.value |
| 58 | + ) |
| 59 | + console.log('newUserCards', userCustomCards, newUserCards) |
| 60 | + setUserCustomCards(newUserCards) |
| 61 | + setConfirmDelete({ showModal: false, option: undefined }) |
| 62 | + }} |
| 63 | + /> |
34 | 64 | <ChipsSet |
35 | 65 | canSelectMultiple={true} |
36 | 66 | options={mergedSources} |
37 | 67 | defaultValues={cards.map((source) => source.name)} |
| 68 | + onRemove={(option) => { |
| 69 | + setConfirmDelete({ showModal: true, option: option }) |
| 70 | + }} |
38 | 71 | onChange={(changes, selectedChips) => { |
39 | 72 | const selectedValues = selectedChips.map((chip) => chip.value) |
40 | 73 |
|
|
0 commit comments