import { useState } from "react"; import { useMutation, useQuery } from "urql"; import { AddFeedForm, FeedList } from "../components"; import { GetFeedsDocument, MarkFeedReadDocument, MarkFeedUnreadDocument, UnsubscribeFeedDocument, } from "../graphql/generated/graphql"; export function Settings() { const [{ data: feedsData }, refetchFeeds] = useQuery({ query: GetFeedsDocument, }); const [, markFeedRead] = useMutation(MarkFeedReadDocument); const [, markFeedUnread] = useMutation(MarkFeedUnreadDocument); const [, unsubscribeFeed] = useMutation(UnsubscribeFeedDocument); const [selectedFeeds, setSelectedFeeds] = useState>(new Set()); const handleFeedAdded = () => { refetchFeeds(); }; const handleFeedUnsubscribed = () => { refetchFeeds(); setSelectedFeeds(new Set()); }; const handleSelectFeed = (feedId: string, selected: boolean) => { const newSelection = new Set(selectedFeeds); if (selected) { newSelection.add(feedId); } else { newSelection.delete(feedId); } setSelectedFeeds(newSelection); }; const handleSelectAll = () => { if (!feedsData?.feeds) return; if (selectedFeeds.size === feedsData.feeds.length) { setSelectedFeeds(new Set()); } else { setSelectedFeeds(new Set(feedsData.feeds.map((feed) => feed.id))); } }; const handleBulkMarkRead = async () => { const promises = Array.from(selectedFeeds).map((feedId) => markFeedRead({ id: feedId }), ); await Promise.all(promises); refetchFeeds(); }; const handleBulkMarkUnread = async () => { const promises = Array.from(selectedFeeds).map((feedId) => markFeedUnread({ id: feedId }), ); await Promise.all(promises); refetchFeeds(); }; const handleBulkUnsubscribe = async () => { const confirmed = window.confirm( `Are you sure you want to unsubscribe from ${selectedFeeds.size} selected feeds?`, ); if (!confirmed) return; const promises = Array.from(selectedFeeds).map((feedId) => unsubscribeFeed({ id: feedId }), ); await Promise.all(promises); handleFeedUnsubscribed(); }; const hasFeeds = feedsData?.feeds && feedsData.feeds.length > 0; const hasSelectedFeeds = selectedFeeds.size > 0; return (

Feed Settings

{/* Subscribe to New Feed Section */}

Subscribe to New Feed

{/* Manage Feeds Section */}

Manage Feeds

{hasFeeds && (
)}
{/* Bulk Operations */} {hasSelectedFeeds && (
{selectedFeeds.size} feed{selectedFeeds.size > 1 ? "s" : ""}{" "} selected
)}
); }