Skip to content

Commit 5f5215c

Browse files
committed
fix: refactor SourceSettings component to use useMemo for mergedSources and add confirm delete modal functionality
1 parent f70612b commit 5f5215c

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

src/features/settings/components/SourceSettings.tsx

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,73 @@
1-
import { ChipsSet } from 'src/components/Elements'
1+
import { useMemo, useState } from 'react'
2+
import { ChipsSet, ConfirmModal } from 'src/components/Elements'
23
import { SettingsContentLayout } from 'src/components/Layout/SettingsContentLayout/SettingsContentLayout'
34
import { SUPPORTED_CARDS } from 'src/config/supportedCards'
45
import { trackSourceAdd, trackSourceRemove } from 'src/lib/analytics'
56
import { useUserPreferences } from 'src/stores/preferences'
6-
import { SelectedCard } from 'src/types'
7+
import { Option, SelectedCard } from 'src/types'
78
import { RssSetting } from './RssSetting'
89

910
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+
})
1119

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])
2839

2940
return (
3041
<SettingsContentLayout
3142
title="Sources"
3243
description={`Your feed will be tailored by following the sources you are interested in.`}>
3344
<>
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+
/>
3464
<ChipsSet
3565
canSelectMultiple={true}
3666
options={mergedSources}
3767
defaultValues={cards.map((source) => source.name)}
68+
onRemove={(option) => {
69+
setConfirmDelete({ showModal: true, option: option })
70+
}}
3871
onChange={(changes, selectedChips) => {
3972
const selectedValues = selectedChips.map((chip) => chip.value)
4073

0 commit comments

Comments
 (0)