File size: 5,135 Bytes
0e11366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71c1c9d
 
0e11366
 
 
 
 
 
 
 
 
71c1c9d
 
0e11366
 
 
 
 
 
 
 
 
 
 
71c1c9d
 
 
 
 
 
 
0e11366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71c1c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e11366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { UpdateItem } from "../../lib/types";
import { formatAgo } from "../../lib/utils";

const isEmoji = (s: string) => !!s && /\p{Extended_Pictographic}/u.test(s);

// Same slug cleaner used above
const cleanTitle = (t?: string) => {
  if (!t) return t;
  return t
    .replace(/^(?:[a-z0-9]+-){1,3}(?=[A-Z])/i, "")
    .replace(/^(?:[a-z0-9]+-){1,3}\s+/i, "");
};

export default function UpdatesPanel({
  activeTab,
  setActiveTab,
  localUpdates,
  globalUpdates,
  loadingLocal,
  loadingGlobal,
  selectedLL,
  onView,
  reactionsById,
  onReact,
}: {
  activeTab: "local" | "global";
  setActiveTab: (t: "local" | "global") => void;
  localUpdates: UpdateItem[];
  globalUpdates: UpdateItem[];
  loadingLocal: boolean;
  loadingGlobal: boolean;
  selectedLL: [number, number] | null;
  onView: (u: UpdateItem) => void;
  reactionsById: Record<string, any>;
  onReact: (rid: string, action: "verify" | "clear") => void;
}) {
  const renderList = (
    list: UpdateItem[],
    loading: boolean,
    emptyMsg: string
  ) => (
    <>
      {loading && <div className="muted">Loading…</div>}
      {!loading && list.length === 0 && <div className="muted">{emptyMsg}</div>}
      {!loading &&
        list.map((u, i) => {
          // get reaction info
          const rid = u.rid;
          const rx = rid ? reactionsById[rid] : null;
          const meVerified = !!rx?.me?.verified;
          const meCleared = !!rx?.me?.cleared;
          const verifyCount = rx?.verify_count ?? 0;
          const clearCount = rx?.clear_count ?? 0;

          const showEmoji =
            u.emoji && isEmoji(String(u.emoji)) ? u.emoji : null;
          const title = cleanTitle(u.title) || u.title || "Update";

          return (
            <div className="updateItem" key={`${activeTab}-${i}`}>
              <div className="flex items-center gap-2">
                {showEmoji ? <div className="text-xl">{showEmoji}</div> : null}
                <div className="flex-1">
                  <div className="font-medium">{title}</div>
                  <div className="text-xs muted">
                    {formatAgo(u.time)} · {u.kind}
                    {u.severity ? <> · {String(u.severity)}</> : null}
                  </div>
                  {u.sourceUrl && (
                    <div className="text-xs">
                      <a
                        className="link"
                        href={u.sourceUrl}
                        target="_blank"
                        rel="noreferrer"
                      >
                        Source
                      </a>
                    </div>
                  )}
                </div>
                {u.kind === "report" && rid ? (
                  <>
                    <button
                      className={`btn btn-ghost ${
                        meVerified ? "btn-active" : ""
                      }`}
                      onClick={() => onReact(rid, "verify")}
                      title="Others also saw/heard this"
                    >
                      {meVerified ? "Verified" : "Verify"} · {verifyCount}
                    </button>
                    <button
                      className={`btn btn-ghost ${
                        meCleared ? "btn-active" : ""
                      }`}
                      onClick={() => onReact(rid, "clear")}
                      title="Issue is cleared/resolved"
                    >
                      {meCleared ? "Cleared" : "Clear"} · {clearCount}
                    </button>
                  </>
                ) : null}
                <button className="btn btn-ghost" onClick={() => onView(u)}>
                  View
                </button>
              </div>
            </div>
          );
        })}
    </>
  );

  return (
    <div
      className="block"
      style={{ display: "flex", flexDirection: "column", gap: 8 }}
    >
      <div className="tabs" style={{ flex: "0 0 auto" }}>
        <button
          className={`tab ${activeTab === "local" ? "tab-active" : ""}`}
          onClick={() => setActiveTab("local")}
        >
          Local updates
        </button>
        <button
          className={`tab ${activeTab === "global" ? "tab-active" : ""}`}
          onClick={() => setActiveTab("global")}
        >
          Global updates
        </button>
      </div>
      <div
        className="updates"
        style={{
          flex: "0 0 auto",
          height: "50vh",
          overflowY: "auto",
          overflowX: "hidden",
          paddingRight: 4,
        }}
        onWheel={(e) => e.stopPropagation()}
      >
        {activeTab === "local" ? (
          selectedLL ? (
            renderList(localUpdates, loadingLocal, "No recent updates here.")
          ) : (
            <div className="muted">
              Pick a point (search/📍/click) to load local updates within 25
              miles (last 48h).
            </div>
          )
        ) : (
          renderList(
            globalUpdates,
            loadingGlobal,
            "No global updates right now."
          )
        )}
      </div>
    </div>
  );
}