summaryrefslogtreecommitdiff
path: root/apps/web/app/reader/_components/sidebar-content.tsx
blob: ee5c873235c28358ccf97ef15b0e7d38be40f493 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"use client"

import Link from "next/link"
import { usePathname, useSearchParams } from "next/navigation"
import { useSubscriptions } from "@/lib/queries/use-subscriptions"
import { useUnreadCounts } from "@/lib/queries/use-unread-counts"
import { useCustomFeeds } from "@/lib/queries/use-custom-feeds"
import { useUserInterfaceStore } from "@/lib/stores/user-interface-store"
import { classNames } from "@/lib/utilities"

const NAVIGATION_LINK_CLASS =
  "block px-2 py-1 text-text-secondary transition-colors hover:bg-background-tertiary hover:text-text-primary"

const ACTIVE_LINK_CLASS = "bg-background-tertiary text-text-primary"

function getFaviconUrl(feedUrl: string): string | null {
  try {
    const hostname = new URL(feedUrl).hostname
    return `https://www.google.com/s2/favicons?domain=${hostname}&sz=16`
  } catch {
    return null
  }
}

function FeedFavicon({ feedUrl }: { feedUrl: string }) {
  const faviconUrl = getFaviconUrl(feedUrl)
  if (!faviconUrl) return null

  return (
    <img
      src={faviconUrl}
      alt=""
      width={16}
      height={16}
      className="shrink-0"
      loading="lazy"
    />
  )
}

function displayNameForSubscription(subscription: {
  customTitle: string | null
  feedTitle: string
  feedUrl: string
}): string {
  if (subscription.customTitle) return subscription.customTitle
  if (subscription.feedTitle) return subscription.feedTitle

  try {
    return new URL(subscription.feedUrl).hostname
  } catch {
    return subscription.feedUrl || "untitled feed"
  }
}

function UnreadBadge({ count }: { count: number }) {
  if (count === 0) return null

  return (
    <span className="ml-auto shrink-0 text-[0.625rem] tabular-nums text-text-dim">
      {count > 999 ? "999+" : count}
    </span>
  )
}

function sidebarFocusClass(
  focusedPanel: string,
  focusedSidebarIndex: number,
  navIndex: number
): string {
  return focusedPanel === "sidebar" && focusedSidebarIndex === navIndex
    ? "bg-background-tertiary text-text-primary"
    : ""
}

export function SidebarContent() {
  const pathname = usePathname()
  const searchParameters = useSearchParams()
  const { data } = useSubscriptions()
  const { data: unreadCounts } = useUnreadCounts()
  const { data: customFeedsData } = useCustomFeeds()
  const setAddFeedDialogOpen = useUserInterfaceStore(
    (state) => state.setAddFeedDialogOpen
  )
  const toggleSidebar = useUserInterfaceStore((state) => state.toggleSidebar)
  const showFeedFavicons = useUserInterfaceStore(
    (state) => state.showFeedFavicons
  )
  const expandedFolderIdentifiers = useUserInterfaceStore(
    (state) => state.expandedFolderIdentifiers
  )
  const toggleFolderExpansion = useUserInterfaceStore(
    (state) => state.toggleFolderExpansion
  )
  const focusedPanel = useUserInterfaceStore((state) => state.focusedPanel)
  const focusedSidebarIndex = useUserInterfaceStore(
    (state) => state.focusedSidebarIndex
  )

  function closeSidebarOnMobile() {
    if (typeof window !== "undefined" && window.innerWidth < 768) {
      toggleSidebar()
    }
  }

  const folders = data?.folders ?? []
  const subscriptions = data?.subscriptions ?? []
  const ungroupedSubscriptions = subscriptions.filter(
    (subscription) => !subscription.folderIdentifier
  )

  const totalUnreadCount = Object.values(unreadCounts ?? {}).reduce(
    (sum, count) => sum + count,
    0
  )

  function getFolderUnreadCount(folderIdentifier: string): number {
    return subscriptions
      .filter(
        (subscription) =>
          subscription.folderIdentifier === folderIdentifier
      )
      .reduce(
        (sum, subscription) =>
          sum + (unreadCounts?.[subscription.feedIdentifier] ?? 0),
        0
      )
  }

  const activeFeedIdentifier = searchParameters.get("feed")
  const activeFolderIdentifier = searchParameters.get("folder")
  const activeCustomFeedIdentifier = searchParameters.get("custom_feed")

  let navIndex = 0

  return (
    <nav className="flex-1 space-y-1 overflow-auto px-2">
      <Link
        href="/reader"
        data-sidebar-nav-item
        onClick={closeSidebarOnMobile}
        className={classNames(
          NAVIGATION_LINK_CLASS,
          "flex items-center",
          pathname === "/reader" &&
            !activeFeedIdentifier &&
            !activeFolderIdentifier &&
            !activeCustomFeedIdentifier &&
            ACTIVE_LINK_CLASS,
          sidebarFocusClass(focusedPanel, focusedSidebarIndex, navIndex++)
        )}
      >
        <span>all entries</span>
        <UnreadBadge count={totalUnreadCount} />
      </Link>
      <Link
        href="/reader/saved"
        data-sidebar-nav-item
        onClick={closeSidebarOnMobile}
        className={classNames(
          NAVIGATION_LINK_CLASS,
          pathname === "/reader/saved" && ACTIVE_LINK_CLASS,
          sidebarFocusClass(focusedPanel, focusedSidebarIndex, navIndex++)
        )}
      >
        saved
      </Link>
      <Link
        href="/reader/highlights"
        data-sidebar-nav-item
        onClick={closeSidebarOnMobile}
        className={classNames(
          NAVIGATION_LINK_CLASS,
          pathname === "/reader/highlights" && ACTIVE_LINK_CLASS,
          sidebarFocusClass(focusedPanel, focusedSidebarIndex, navIndex++)
        )}
      >
        highlights
      </Link>
      <Link
        href="/reader/shares"
        data-sidebar-nav-item
        onClick={closeSidebarOnMobile}
        className={classNames(
          NAVIGATION_LINK_CLASS,
          pathname === "/reader/shares" && ACTIVE_LINK_CLASS,
          sidebarFocusClass(focusedPanel, focusedSidebarIndex, navIndex++)
        )}
      >
        shares
      </Link>

      {customFeedsData && customFeedsData.length > 0 && (
        <div className="mt-3 space-y-0.5">
          {customFeedsData.map((customFeed) => (
            <Link
              key={customFeed.identifier}
              href={`/reader?custom_feed=${customFeed.identifier}`}
              data-sidebar-nav-item
              onClick={closeSidebarOnMobile}
              className={classNames(
                NAVIGATION_LINK_CLASS,
                "truncate pl-4 text-[0.85em]",
                activeCustomFeedIdentifier === customFeed.identifier &&
                  ACTIVE_LINK_CLASS,
                sidebarFocusClass(focusedPanel, focusedSidebarIndex, navIndex++)
              )}
            >
              {customFeed.name}
            </Link>
          ))}
        </div>
      )}

      {ungroupedSubscriptions.length > 0 && (
        <div className="mt-3 space-y-0.5">
          {ungroupedSubscriptions.map((subscription) => (
            <Link
              key={subscription.subscriptionIdentifier}
              href={`/reader?feed=${subscription.feedIdentifier}`}
              data-sidebar-nav-item
              onClick={closeSidebarOnMobile}
              className={classNames(
                NAVIGATION_LINK_CLASS,
                "flex items-center truncate pl-4 text-[0.85em]",
                activeFeedIdentifier === subscription.feedIdentifier &&
                  ACTIVE_LINK_CLASS,
                sidebarFocusClass(focusedPanel, focusedSidebarIndex, navIndex++)
              )}
            >
              {showFeedFavicons && (
                <FeedFavicon feedUrl={subscription.feedUrl} />
              )}
              <span className={classNames("truncate", showFeedFavicons && "ml-2")}>
                {displayNameForSubscription(subscription)}
              </span>
              {subscription.feedType === "podcast" && (
                <span className="ml-1 shrink-0 text-text-dim" title="podcast">&#9835;</span>
              )}
              {subscription.consecutiveFailures > 0 && (
                <span className="ml-1 shrink-0 text-status-warning" title={subscription.lastFetchError ?? "feed error"}>
                  [!]
                </span>
              )}
              <UnreadBadge
                count={unreadCounts?.[subscription.feedIdentifier] ?? 0}
              />
            </Link>
          ))}
        </div>
      )}

      {folders.map((folder) => {
        const isExpanded = expandedFolderIdentifiers.includes(
          folder.folderIdentifier
        )
        const folderSubscriptions = subscriptions.filter(
          (subscription) =>
            subscription.folderIdentifier === folder.folderIdentifier
        )
        const folderUnreadCount = getFolderUnreadCount(
          folder.folderIdentifier
        )

        const folderNavIndex = navIndex++

        return (
          <div key={folder.folderIdentifier} className="mt-2">
            <div
              data-sidebar-nav-item
              className={classNames(
                "flex w-full items-center gap-1 px-2 py-1",
                sidebarFocusClass(focusedPanel, focusedSidebarIndex, folderNavIndex)
              )}
            >
              <button
                type="button"
                onClick={() =>
                  toggleFolderExpansion(folder.folderIdentifier)
                }
                className="shrink-0 px-0.5 text-text-secondary transition-colors hover:text-text-primary"
              >
                {isExpanded ? "\u25BE" : "\u25B8"}
              </button>
              <Link
                href={`/reader?folder=${folder.folderIdentifier}`}
                onClick={closeSidebarOnMobile}
                className={classNames(
                  "flex-1 truncate text-text-secondary transition-colors hover:text-text-primary",
                  activeFolderIdentifier === folder.folderIdentifier &&
                    "text-text-primary"
                )}
              >
                {folder.name}
              </Link>
              <UnreadBadge count={folderUnreadCount} />
            </div>
            {isExpanded && (
              <div className="space-y-0.5">
                {folderSubscriptions.map((subscription) => (
                  <Link
                    key={subscription.subscriptionIdentifier}
                    href={`/reader?feed=${subscription.feedIdentifier}`}
                    data-sidebar-nav-item
                    onClick={closeSidebarOnMobile}
                    className={classNames(
                      NAVIGATION_LINK_CLASS,
                      "flex items-center truncate pl-6 text-[0.85em]",
                      activeFeedIdentifier ===
                        subscription.feedIdentifier && ACTIVE_LINK_CLASS,
                      sidebarFocusClass(focusedPanel, focusedSidebarIndex, navIndex++)
                    )}
                  >
                    {showFeedFavicons && (
                      <FeedFavicon feedUrl={subscription.feedUrl} />
                    )}
                    <span className={classNames("truncate", showFeedFavicons && "ml-2")}>
                      {displayNameForSubscription(subscription)}
                    </span>
                    {subscription.feedType === "podcast" && (
                      <span className="ml-1 shrink-0 text-text-dim" title="podcast">&#9835;</span>
                    )}
                    {subscription.consecutiveFailures > 0 && (
                      <span className="ml-1 shrink-0 text-status-warning" title={subscription.lastFetchError ?? "feed error"}>
                        [!]
                      </span>
                    )}
                    <UnreadBadge
                      count={
                        unreadCounts?.[subscription.feedIdentifier] ?? 0
                      }
                    />
                  </Link>
                ))}
              </div>
            )}
          </div>
        )
      })}

      <div className="mt-3">
        <button
          type="button"
          data-sidebar-nav-item
          onClick={() => setAddFeedDialogOpen(true)}
          className={classNames(
            "w-full px-2 py-1 text-left text-text-dim transition-colors hover:bg-background-tertiary hover:text-text-secondary",
            sidebarFocusClass(focusedPanel, focusedSidebarIndex, navIndex++)
          )}
        >
          + add feed
        </button>
      </div>
    </nav>
  )
}