Skip to content

Commit e2df30a

Browse files
committed
Move live banner data to database
1 parent 28728c2 commit e2df30a

File tree

7 files changed

+100
-20
lines changed

7 files changed

+100
-20
lines changed

app/controllers/application_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ class ApplicationController < ActionController::Base
22
include AllowBrowser, RackMiniProfilerAuthorization, Authentication, Authorization, SetCurrentRequest, SetPlatform, TrackedRoomVisit, VersionHeaders, FragmentCache, Sidebar
33
include Turbo::Streams::Broadcasts, Turbo::Streams::StreamName
44

5+
before_action :load_current_live_event
6+
57
private
68

9+
def load_current_live_event
10+
@current_live_event = LiveEvent.current
11+
end
12+
713
def inertia_request?
814
request.headers['X-Inertia'].present?
915
end

app/frontend/controllers/countdown_banner_controller.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default class extends Controller {
55
static classes = [ "urgent" ]
66
static values = {
77
targetTime: String,
8-
durationHours: Number
8+
durationHours: Number,
9+
showEarlyHours: { type: Number, default: 24 }
910
}
1011

1112
connect() {
@@ -49,11 +50,11 @@ export default class extends Controller {
4950
}
5051

5152
const diff = this.targetDate - now;
52-
const twentyFourHoursInMillis = 24 * 60 * 60 * 1000;
53+
const showEarlyInMillis = this.showEarlyHoursValue * 60 * 60 * 1000;
5354
const twoHoursInMillis = 2 * 60 * 60 * 1000;
54-
55-
// Hide if more than 24 hours away or if event is over
56-
if (diff > twentyFourHoursInMillis || now >= this.endDate) {
55+
56+
// Hide if more than showEarlyHours away or if event is over
57+
if (diff > showEarlyInMillis || now >= this.endDate) {
5758
this.hideBanner();
5859
if (now >= this.endDate && this.interval) {
5960
clearInterval(this.interval); // Stop interval only if event ended
@@ -75,21 +76,32 @@ export default class extends Controller {
7576
this.updateCountdown(now);
7677
this.element.classList.add(this.urgentClass);
7778
} else {
78-
// Between 2 and 24 hours left: Show day + time
79+
// Between 2 hours and showEarlyHours left: Show day + time
7980
const targetLocalDate = this.targetDate; // Already a Date object
8081
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
8182
const tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
8283
const targetDay = new Date(targetLocalDate.getFullYear(), targetLocalDate.getMonth(), targetLocalDate.getDate());
8384

84-
let dayPrefix = "Live on X"; // Default/Fallback
85+
let statusText;
8586
if (targetDay.getTime() === today.getTime()) {
86-
dayPrefix = "Live on X today";
87+
const formattedTime = targetLocalDate.toLocaleString(undefined, { hour: 'numeric', minute: '2-digit' });
88+
statusText = `Live today at ${formattedTime}`;
8789
} else if (targetDay.getTime() === tomorrow.getTime()) {
88-
dayPrefix = "Live on X tomorrow";
89-
} // Future dates > tomorrow are handled by the 24h check above
90-
91-
const formattedTime = targetLocalDate.toLocaleString(undefined, { hour: 'numeric', minute: '2-digit' });
92-
this.statusTarget.textContent = `${dayPrefix} at ${formattedTime}`;
90+
const formattedTime = targetLocalDate.toLocaleString(undefined, { hour: 'numeric', minute: '2-digit' });
91+
statusText = `Live tomorrow at ${formattedTime}`;
92+
} else {
93+
// Future dates beyond tomorrow
94+
const formattedDate = targetLocalDate.toLocaleString(undefined, {
95+
weekday: 'short',
96+
month: 'short',
97+
day: 'numeric',
98+
hour: 'numeric',
99+
minute: '2-digit'
100+
});
101+
statusText = `Live on ${formattedDate}`;
102+
}
103+
104+
this.statusTarget.textContent = statusText;
93105
this.element.classList.remove(this.urgentClass);
94106
}
95107
}

app/models/live_event.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class LiveEvent < ApplicationRecord
2+
validates :title, :url, :target_time, :duration_hours, :show_early_hours, presence: true
3+
validates :duration_hours, :show_early_hours, numericality: { greater_than: 0 }
4+
5+
scope :active, -> { where(active: true) }
6+
scope :displayable, -> {
7+
active.where("target_time >= ?", Time.current - duration_hours.hours)
8+
}
9+
10+
def self.current
11+
active
12+
.where("target_time >= ?", Time.current)
13+
.order(:target_time)
14+
.first ||
15+
active
16+
.where("target_time < ? AND target_time >= ?", Time.current, Time.current - 7.days)
17+
.where("datetime(target_time, '+' || duration_hours || ' hours') >= datetime('now')")
18+
.order(target_time: :desc)
19+
.first
20+
end
21+
22+
def expired?
23+
end_time < Time.current
24+
end
25+
26+
def end_time
27+
target_time + duration_hours.hours
28+
end
29+
end

app/views/layouts/application.html.erb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,19 @@
5151
</head>
5252

5353
<body class="<%= body_classes %>" data-controller="local-time lightbox page-title">
54-
<% if controller_name == "rooms" && action_name == "show" %>
55-
<a
54+
<% if controller_name == "rooms" && action_name == "show" && @current_live_event.present? %>
55+
<a
5656
id="special-event-banner"
57-
href="https://x.com/nestersk/status/1973501168312783190"
57+
href="<%= @current_live_event.url %>"
5858
target="_blank"
5959
style="font-weight: 600; letter-spacing: -0.02em; text-align: center; padding: 0.5rem; display: none; width: 100%; cursor: pointer; text-decoration: none; box-sizing: border-box;"
6060
data-controller="countdown-banner"
6161
data-countdown-banner-urgent-class="banner-urgent"
62-
data-countdown-banner-target-time-value="2025-10-02T16:00:00Z"
63-
data-countdown-banner-duration-hours-value="2"
62+
data-countdown-banner-target-time-value="<%= @current_live_event.target_time.iso8601 %>"
63+
data-countdown-banner-duration-hours-value="<%= @current_live_event.duration_hours %>"
64+
data-countdown-banner-show-early-hours-value="<%= @current_live_event.show_early_hours %>"
6465
>
65-
The Weekly Yap<span data-countdown-banner-target="status"></span>
66+
<%= @current_live_event.title %><span data-countdown-banner-target="status"></span>
6667
</a>
6768
<% end %>
6869
<a href="#main-content" class="skip-navigation btn" data-turbo="false">Skip to main content</a>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class CreateLiveEvents < ActiveRecord::Migration[7.2]
2+
def change
3+
create_table :live_events do |t|
4+
t.string :title, null: false
5+
t.string :url, null: false
6+
t.datetime :target_time, null: false
7+
t.integer :duration_hours, null: false, default: 2
8+
t.integer :show_early_hours, null: false, default: 24
9+
t.boolean :active, null: false, default: true
10+
11+
t.timestamps
12+
end
13+
14+
add_index :live_events, :active
15+
add_index :live_events, :target_time
16+
end
17+
end

db/structure.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ FOREIGN KEY ("user_id")
156156
);
157157
CREATE UNIQUE INDEX "index_library_watch_histories_on_session_and_user" ON "library_watch_histories" ("library_session_id", "user_id");
158158
CREATE INDEX "index_messages_on_room_id_and_mentions_everyone" ON "messages" ("room_id", "mentions_everyone") WHERE mentions_everyone = true;
159+
CREATE TABLE IF NOT EXISTS "live_events" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "url" varchar NOT NULL, "target_time" datetime(6) NOT NULL, "duration_hours" integer DEFAULT 2 NOT NULL, "show_early_hours" integer DEFAULT 24 NOT NULL, "active" boolean DEFAULT 1 NOT NULL, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL);
160+
CREATE INDEX "index_live_events_on_active" ON "live_events" ("active");
161+
CREATE INDEX "index_live_events_on_target_time" ON "live_events" ("target_time");
159162
INSERT INTO "schema_migrations" (version) VALUES
163+
('20251022001753'),
160164
('20251021014520'),
161165
('20251013024845'),
162166
('20251013024703'),

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)