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
|
"""
PodcastItLater Shared UI Components.
Common UI components and utilities shared across web pages.
"""
# : out podcastitlater-ui
# : dep ludic
import ludic.html as html
import typing
from ludic.attrs import Attrs
from ludic.components import Component
from ludic.types import AnyChildren
from typing import override
def format_duration(seconds: int | None) -> str:
"""Format duration from seconds to human-readable format.
Examples:
300 -> "5m"
3840 -> "1h 4m"
11520 -> "3h 12m"
"""
if seconds is None or seconds <= 0:
return "Unknown"
# Constants for time conversion
seconds_per_minute = 60
minutes_per_hour = 60
seconds_per_hour = 3600
# Round up to nearest minute
minutes = (seconds + seconds_per_minute - 1) // seconds_per_minute
# Show as minutes only if under 60 minutes (exclusive)
# 3599 seconds rounds up to 60 minutes, which we keep as "60m"
if minutes <= minutes_per_hour:
# If exactly 3600 seconds (already 60 full minutes without rounding)
if seconds >= seconds_per_hour:
return "1h"
return f"{minutes}m"
hours = minutes // minutes_per_hour
remaining_minutes = minutes % minutes_per_hour
if remaining_minutes == 0:
return f"{hours}h"
return f"{hours}h {remaining_minutes}m"
def create_bootstrap_styles() -> html.style:
"""Load Bootstrap CSS and icons."""
return html.style(
"@import url('https://cdn.jsdelivr.net/npm/bootstrap@5.3.2"
"/dist/css/bootstrap.min.css');"
"@import url('https://cdn.jsdelivr.net/npm/bootstrap-icons"
"@1.11.3/font/bootstrap-icons.min.css');",
)
def create_auto_dark_mode_style() -> html.style:
"""Create CSS for automatic dark mode based on prefers-color-scheme."""
return html.style(
"""
/* Auto dark mode - applies Bootstrap dark theme via media query */
@media (prefers-color-scheme: dark) {
:root {
color-scheme: dark;
--bs-body-color: #dee2e6;
--bs-body-color-rgb: 222, 226, 230;
--bs-body-bg: #212529;
--bs-body-bg-rgb: 33, 37, 41;
--bs-emphasis-color: #fff;
--bs-emphasis-color-rgb: 255, 255, 255;
--bs-secondary-color: rgba(222, 226, 230, 0.75);
--bs-secondary-bg: #343a40;
--bs-tertiary-color: rgba(222, 226, 230, 0.5);
--bs-tertiary-bg: #2b3035;
--bs-heading-color: inherit;
--bs-link-color: #6ea8fe;
--bs-link-hover-color: #8bb9fe;
--bs-link-color-rgb: 110, 168, 254;
--bs-link-hover-color-rgb: 139, 185, 254;
--bs-code-color: #e685b5;
--bs-border-color: #495057;
--bs-border-color-translucent: rgba(255, 255, 255, 0.15);
}
/* Navbar dark mode */
.navbar.bg-body-tertiary {
background-color: #2b3035 !important;
}
.navbar .navbar-text {
color: #dee2e6 !important;
}
/* Table header dark mode */
.table-light {
--bs-table-bg: #343a40;
--bs-table-color: #dee2e6;
background-color: #343a40 !important;
color: #dee2e6 !important;
}
}
""",
)
def create_htmx_script() -> html.script:
"""Load HTMX library."""
return html.script(
src="https://unpkg.com/htmx.org@1.9.10",
integrity="sha384-D1Kt99CQMDuVetoL1lrYwg5t+9QdHe7NLX/SoJYkXDFfX37iInKRy5xLSi8nO7UC",
crossorigin="anonymous",
)
def create_bootstrap_js() -> html.script:
"""Load Bootstrap JavaScript bundle."""
return html.script(
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js",
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL",
crossorigin="anonymous",
)
class PageLayoutAttrs(Attrs):
"""Attributes for PageLayout component."""
user: dict[str, typing.Any] | None
current_page: str
error: str | None
class PageLayout(Component[AnyChildren, PageLayoutAttrs]):
"""Reusable page layout with header and navbar."""
@staticmethod
def _render_navbar(
user: dict[str, typing.Any] | None,
current_page: str,
) -> html.nav:
"""Render navigation bar."""
# Import here to avoid circular dependency
import Biz.PodcastItLater.Core as Core # noqa: PLC0415 # type: ignore[import-untyped]
def is_active(page: str) -> bool:
return current_page == page
return html.nav(
html.div(
html.button( # type: ignore[call-arg]
html.span(classes=["navbar-toggler-icon"]),
classes=["navbar-toggler", "ms-auto"],
type="button",
data_bs_toggle="collapse",
data_bs_target="#navbarNav",
aria_controls="navbarNav",
aria_expanded="false",
aria_label="Toggle navigation",
),
html.div(
html.ul(
html.li(
html.a(
html.i(
classes=[
"bi",
"bi-house-fill",
"me-1",
],
),
"Home",
href="/",
classes=[
"nav-link",
"active" if is_active("home") else "",
],
),
classes=["nav-item"],
),
html.li(
html.a(
html.i(
classes=[
"bi",
"bi-person-circle",
"me-1",
],
),
"Manage Account",
href="/account",
classes=[
"nav-link",
"active" if is_active("account") else "",
],
),
classes=["nav-item"],
),
html.li(
html.a( # type: ignore[call-arg]
html.i(
classes=[
"bi",
"bi-gear-fill",
"me-1",
],
),
"Admin",
href="#",
id="adminDropdown",
role="button",
data_bs_toggle="dropdown",
aria_expanded="false",
classes=[
"nav-link",
"dropdown-toggle",
"active"
if is_active("admin")
or is_active("admin-users")
else "",
],
),
html.ul( # type: ignore[call-arg]
html.li(
html.a(
html.i(
classes=[
"bi",
"bi-list-task",
"me-2",
],
),
"Queue Status",
href="/admin",
classes=["dropdown-item"],
),
),
html.li(
html.a(
html.i(
classes=[
"bi",
"bi-people-fill",
"me-2",
],
),
"Manage Users",
href="/admin/users",
classes=["dropdown-item"],
),
),
classes=["dropdown-menu"],
aria_labelledby="adminDropdown",
),
classes=["nav-item", "dropdown"],
)
if user and Core.is_admin(user)
else html.span(),
classes=["navbar-nav"],
),
id="navbarNav",
classes=["collapse", "navbar-collapse"],
),
classes=["container-fluid"],
),
classes=[
"navbar",
"navbar-expand-lg",
"bg-body-tertiary",
"rounded",
"mb-4",
],
)
@override
def render(self) -> html.html:
user = self.attrs.get("user")
current_page = self.attrs.get("current_page", "")
error = self.attrs.get("error")
return html.html(
html.head(
html.meta(charset="utf-8"),
html.meta(
name="viewport",
content="width=device-width, initial-scale=1",
),
html.meta(
name="color-scheme",
content="light dark",
),
html.title("PodcastItLater"),
create_htmx_script(),
),
html.body(
create_bootstrap_styles(),
create_auto_dark_mode_style(),
html.div(
html.div(
html.h1(
"PodcastItLater",
classes=["display-4", "mb-2"],
),
html.p(
"Convert web articles to podcast episodes",
classes=["lead", "text-muted"],
),
classes=["text-center", "mb-4", "pt-4"],
),
html.div(
html.div(
html.i(
classes=[
"bi",
"bi-exclamation-triangle-fill",
"me-2",
],
),
error or "",
classes=[
"alert",
"alert-danger",
"d-flex",
"align-items-center",
],
role="alert", # type: ignore[call-arg]
),
)
if error
else html.div(),
self._render_navbar(user, current_page)
if user
else html.div(),
*self.children,
classes=["container", "px-3", "px-md-4"],
style={"max-width": "900px"},
),
create_bootstrap_js(),
),
)
|