aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/src/components/FeedList.tsx
blob: d2dc8e1b0e9521eadabb5065c8ec1e9b45b622b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useEffect, useState } from 'react';
import { Link, useNavigate, useSearchParams, useLocation, useParams } from 'react-router-dom';
import type { Feed, Category } from '../types';
import './FeedList.css';
import { apiFetch } from '../utils';

export default function FeedList({
  theme,
  setTheme,
}: {
  theme: string;
  setTheme: (t: string) => void;
}) {
  const [feeds, setFeeds] = useState<Feed[]>([]);
  const [tags, setTags] = useState<Category[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState('');
  const [feedsExpanded, setFeedsExpanded] = useState(false);
  const [searchQuery, setSearchQuery] = useState('');
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  const location = useLocation();
  const { feedId, tagName } = useParams();

  const currentFilter =
    searchParams.get('filter') ||
    (location.pathname === '/' && !feedId && !tagName ? 'unread' : '');

  const handleSearch = (e: React.FormEvent) => {
    e.preventDefault();
    if (searchQuery.trim()) {
      navigate(`/?q=${encodeURIComponent(searchQuery.trim())}`);
    }
  };

  const toggleFeeds = () => {
    setFeedsExpanded(!feedsExpanded);
  };

  useEffect(() => {
    Promise.all([
      apiFetch('/api/feed/').then((res) => {
        if (!res.ok) throw new Error('Failed to fetch feeds');
        return res.json();
      }),
      apiFetch('/api/tag').then((res) => {
        if (!res.ok) throw new Error('Failed to fetch tags');
        return res.json();
      }),
    ])
      .then(([feedsData, tagsData]) => {
        setFeeds(feedsData);
        setTags(tagsData);
        setLoading(false);
      })
      .catch((err) => {
        setError(err.message);
        setLoading(false);
      });
  }, []);

  if (loading) return <div className="feed-list-loading">Loading feeds...</div>;
  if (error) return <div className="feed-list-error">Error: {error}</div>;

  return (
    <div className="feed-list">
      <div className="search-section">
        <form onSubmit={handleSearch} className="search-form">
          <input
            type="search"
            placeholder="Search items..."
            value={searchQuery}
            onChange={(e) => setSearchQuery(e.target.value)}
            className="search-input"
          />
        </form>
      </div>
      <div className="filter-section">
        <ul className="filter-list">
          <li>
            <Link to="/?filter=unread" className={currentFilter === 'unread' ? 'active' : ''}>
              Unread
            </Link>
          </li>
          <li>
            <Link to="/?filter=all" className={currentFilter === 'all' ? 'active' : ''}>
              All
            </Link>
          </li>
          <li>
            <Link to="/?filter=starred" className={currentFilter === 'starred' ? 'active' : ''}>
              Starred
            </Link>
          </li>
        </ul>
      </div>
      <div className="feed-section">
        <h2 onClick={toggleFeeds} className="feed-section-header">
          <span className="toggle-indicator">{feedsExpanded ? '▼' : '▶'}</span> Feeds
        </h2>
        {feedsExpanded &&
          (feeds.length === 0 ? (
            <p>No feeds found.</p>
          ) : (
            <ul className="feed-list-items">
              {feeds.map((feed) => (
                <li key={feed._id} className="sidebar-feed-item">
                  <Link
                    to={`/feed/${feed._id}`}
                    className={`feed-title ${feedId === String(feed._id) ? 'active' : ''}`}
                  >
                    {feed.title || feed.url}
                  </Link>
                  {feed.category && <span className="feed-category">{feed.category}</span>}
                </li>
              ))}
            </ul>
          ))}
      </div>

      {tags && tags.length > 0 && (
        <div className="tag-section">
          <h2>Tags</h2>
          <ul className="tag-list-items">
            {tags.map((tag) => (
              <li key={tag.title} className="tag-item">
                <Link
                  to={`/tag/${encodeURIComponent(tag.title)}`}
                  className={`tag-link ${tagName === tag.title ? 'active' : ''}`}
                >
                  {tag.title}
                </Link>
              </li>
            ))}
          </ul>
        </div>
      )}

      <div className="theme-section">
        <div className="theme-selector">
          <button onClick={() => setTheme('light')} className={theme === 'light' ? 'active' : ''}>
            light
          </button>
          <button onClick={() => setTheme('dark')} className={theme === 'dark' ? 'active' : ''}>
            dark
          </button>
          <button onClick={() => setTheme('black')} className={theme === 'black' ? 'active' : ''}>
            black
          </button>
        </div>
      </div>
    </div>
  );
}