Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to remove articles from custom feed #4013

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/shared/src/components/PostOptionsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,22 @@ export default function PostOptionsMenu({
const isEnabled = checkSettingsEnabledState(video?.id);
const icon = isEnabled ? '⛔️' : '✅';
const label = isEnabled ? 'blocked' : 'unblocked';
await onUpdateSettings(video.id, !isEnabled);
await onUpdateSettings([
{
id: video.id,
enabled: !isEnabled,
},
]);
await showMessageAndRemovePost(
`${icon} Video content ${label}`,
postIndex,
() => onUpdateSettings(video.id, isEnabled),
() =>
onUpdateSettings([
{
id: video.id,
enabled: !isEnabled,
},
]),
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FeedSettingsEditContext } from '../FeedSettingsEditContext';
import useFeedSettings from '../../../../hooks/useFeedSettings';
import { useAdvancedSettings } from '../../../../hooks/feed/useAdvancedSettings';
import {
getArticleSettings,
getContentCurationList,
getContentSourceList,
getVideoSetting,
Expand All @@ -13,18 +14,21 @@ import {
TypographyType,
} from '../../../typography/Typography';
import { FilterCheckbox } from '../../../fields/FilterCheckbox';
import { FeedType } from '../../../../graphql/feed';

const ADVANCED_SETTINGS_KEY = 'advancedSettings';

export const FeedSettingsContentPreferencesSection = (): ReactElement => {
const { feed } = useContext(FeedSettingsEditContext);
const { advancedSettings } = useFeedSettings({ feedId: feed?.id });
const videoSetting = getVideoSetting(advancedSettings);
const articleSetting = getArticleSettings(advancedSettings);
const {
selectedSettings,
onToggleSettings,
checkSourceBlocked,
onToggleSource,
onUpdateSettings,
} = useAdvancedSettings({ feedId: feed?.id });

const contentSourceList = useMemo(
Expand Down Expand Up @@ -69,9 +73,26 @@ export const FeedSettingsContentPreferencesSection = (): ReactElement => {
{videoSetting.title}
</FilterCheckbox>
)}
<FilterCheckbox name="Articles" disabled checked>
Articles
</FilterCheckbox>
{articleSetting.length && feed?.type === FeedType.Custom ? (
<FilterCheckbox
name="Articles"
checked={
articleSetting.filter(({ id }) => selectedSettings[id] ?? true)
.length > 0
}
onToggleCallback={(enabled) => {
onUpdateSettings(
articleSetting.map(({ id }) => ({ id, enabled })),
);
}}
>
Articles
</FilterCheckbox>
) : (
<FilterCheckbox name="Articles" disabled checked>
Articles
</FilterCheckbox>
)}
</div>
</div>
<div className="flex flex-col gap-4">
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/src/components/filters/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ export const getVideoSetting = (
advancedSettings: AdvancedSettings[],
): AdvancedSettings =>
advancedSettings?.find(({ title }) => title === 'Videos');

export const getArticleSettings = (
advancedSettings: AdvancedSettings[],
): AdvancedSettings[] =>
advancedSettings?.filter(({ title }) =>
['Article', 'Share', 'Freeform', 'Welcome', 'Collection'].includes(title),
) || [];
20 changes: 11 additions & 9 deletions packages/shared/src/hooks/feed/useAdvancedSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface UseAdvancedSettings {
selectedSettings: Record<string, boolean>;
onToggleSettings(id: number, state: boolean): void;
onToggleSource(source: Source): void;
onUpdateSettings(id: number, state: boolean): void;
onUpdateSettings(updatedSettings: { id: number; enabled: boolean }[]): void;
checkSourceBlocked(source: Source): boolean;
}

Expand All @@ -36,16 +36,18 @@ export const useAdvancedSettings = (
);

const onUpdateSettings = useCallback(
(id: number, enabled: boolean) => {
logEvent({
event_name: `toggle ${enabled ? 'on' : 'off'}`,
target_type: 'advanced setting',
target_id: id.toString(),
extra: JSON.stringify({ origin: 'advanced settings filter' }),
(updatedSettings: { id: number; enabled: boolean }[]) => {
updatedSettings.forEach(({ id, enabled }) => {
logEvent({
event_name: `toggle ${enabled ? 'on' : 'off'}`,
target_type: 'advanced setting',
target_id: id.toString(),
extra: JSON.stringify({ origin: 'advanced settings filter' }),
});
});

return updateAdvancedSettings({
advancedSettings: [{ id, enabled }],
advancedSettings: updatedSettings,
});
},
[logEvent, updateAdvancedSettings],
Expand All @@ -61,7 +63,7 @@ export const useAdvancedSettings = (

const enabled = !(selectedSettings[id] ?? defaultEnabledState);

return onUpdateSettings(id, enabled);
return onUpdateSettings([{ id, enabled }]);
},
[alerts?.filter, selectedSettings, onUpdateSettings, updateAlerts, user],
);
Expand Down
Loading