summaryrefslogtreecommitdiff
path: root/apps/web/app/reader/_components/entry-list-item.tsx
blob: ad21c28eb048a2dee46abacafd0d966cf3820a80 (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
"use client"

import { formatDistanceToNow, format } from "date-fns"
import { classNames } from "@/lib/utilities"
import { useUserInterfaceStore } from "@/lib/stores/user-interface-store"
import type { TimelineEntry } from "@/lib/types/timeline"
import type { VirtualItem } from "@tanstack/react-virtual"

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

interface EntryListItemProperties {
  entry: TimelineEntry
  isSelected: boolean
  isFocused: boolean
  viewMode: "compact" | "comfortable" | "expanded"
  onSelect: () => void
  measureReference: (element: HTMLElement | null) => void
  virtualItem: VirtualItem
}

function stripHtmlTags(html: string): string {
  return html
    .replace(/<[^>]*>/g, "")
    .replace(/&#(\d+);/g, (_, code) => String.fromCharCode(Number(code)))
    .replace(/&#x([0-9a-fA-F]+);/g, (_, code) => String.fromCharCode(parseInt(code, 16)))
    .replace(/&amp;/g, "&")
    .replace(/&lt;/g, "<")
    .replace(/&gt;/g, ">")
    .replace(/&quot;/g, '"')
    .replace(/&apos;/g, "'")
    .replace(/&nbsp;/g, " ")
    .trim()
}

export function EntryListItem({
  entry,
  isSelected,
  isFocused,
  viewMode,
  onSelect,
  measureReference,
  virtualItem,
}: EntryListItemProperties) {
  const timeDisplayFormat = useUserInterfaceStore(
    (state) => state.timeDisplayFormat
  )
  const showEntryImages = useUserInterfaceStore(
    (state) => state.showEntryImages
  )
  const showEntryFavicons = useUserInterfaceStore(
    (state) => state.showEntryFavicons
  )

  const faviconUrl = showEntryFavicons ? getEntryFaviconUrl(entry.entryUrl) : null

  const formattedTimestamp = entry.publishedAt
    ? timeDisplayFormat === "absolute"
      ? format(new Date(entry.publishedAt), "MMM d, h:mm a")
      : formatDistanceToNow(new Date(entry.publishedAt), { addSuffix: true })
    : ""

  const displayTitle = entry.customTitle ?? entry.feedTitle

  return (
    <button
      type="button"
      ref={measureReference}
      data-index={virtualItem.index}
      onClick={onSelect}
      className={classNames(
        "absolute left-0 top-0 w-full cursor-pointer border-b border-border bg-transparent px-4 text-left font-[inherit] text-[inherit] transition-colors",
        isSelected
          ? "bg-background-tertiary"
          : isFocused
            ? "bg-background-secondary"
            : "hover:bg-background-secondary",
        isFocused && !isSelected ? "border-l-2 border-l-text-dim" : "",
        entry.isRead ? "opacity-60" : ""
      )}
      style={{ transform: `translateY(${virtualItem.start}px)` }}
    >
      {viewMode === "compact" && (
        <div className="flex items-center gap-2 py-2.5">
          {faviconUrl && (
            <img src={faviconUrl} alt="" width={16} height={16} className="shrink-0" loading="lazy" />
          )}
          <span className="shrink-0 text-text-dim">{displayTitle}</span>
          {entry.enclosureUrl && (
            <span className="shrink-0 text-text-dim" title="podcast episode">&#9835;</span>
          )}
          <span className="min-w-0 flex-1 truncate text-text-primary">
            {entry.entryTitle}
          </span>
          <span className="shrink-0 text-text-dim">{formattedTimestamp}</span>
        </div>
      )}

      {viewMode === "comfortable" && (
        <div className="py-2.5">
          <div className="truncate text-text-primary">{entry.entryTitle}</div>
          <div className="mt-0.5 flex items-center gap-2 text-text-dim">
            {faviconUrl && (
              <img src={faviconUrl} alt="" width={16} height={16} className="shrink-0" loading="lazy" />
            )}
            <span>{displayTitle}</span>
            {entry.enclosureUrl && (
              <span title="podcast episode">&#9835;</span>
            )}
            {entry.author && (
              <>
                <span>&middot;</span>
                <span>{entry.author}</span>
              </>
            )}
            <span>&middot;</span>
            <span>{formattedTimestamp}</span>
          </div>
        </div>
      )}

      {viewMode === "expanded" && (
        <div className="flex gap-3 py-3">
          <div className="min-w-0 flex-1">
            <div className="truncate text-text-primary">
              {entry.entryTitle}
            </div>
            {entry.summary && (
              <p className="mt-1 line-clamp-2 text-text-secondary">
                {stripHtmlTags(entry.summary)}
              </p>
            )}
            <div className="mt-1 flex items-center gap-2 text-text-dim">
              {faviconUrl && (
                <img src={faviconUrl} alt="" width={16} height={16} className="shrink-0" loading="lazy" />
              )}
              <span>{displayTitle}</span>
              {entry.enclosureUrl && (
                <span title="podcast episode">&#9835;</span>
              )}
              {entry.author && (
                <>
                  <span>&middot;</span>
                  <span>{entry.author}</span>
                </>
              )}
              <span>&middot;</span>
              <span>{formattedTimestamp}</span>
            </div>
          </div>
          {showEntryImages && entry.imageUrl && (
            <img
              src={entry.imageUrl}
              alt=""
              className="hidden h-16 w-16 shrink-0 object-cover sm:block"
              loading="lazy"
            />
          )}
        </div>
      )}
    </button>
  )
}