Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions .eslint_todo.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
// This configuration was generated by `exe/eslint_autogen`
// on 2026-11-27 23:20:15 UTC.
// on 2026-11-28 00:43:19 UTC.
// The point is for the user to remove these configuration records
// one by one as the offenses are removed from the code base.

import type {Linter} from "eslint";

const config: Linter.Config[] = [
// Offense count: 3
// Offense count: 2
{
files: [
"spec/javascript/controllers/star_toggle_controller_spec.ts",
"spec/javascript/helpers/api_spec.ts",
],
rules: {
"@stylistic/dot-location": "off",
},
},
// Offense count: 78
// Offense count: 60
{
files: [
"app/javascript/controllers/keep_unread_toggle_controller.ts",
"app/javascript/controllers/mark_all_as_read_controller.ts",
"app/javascript/controllers/star_toggle_controller.ts",
"app/javascript/controllers/unread_count_controller.ts",
"app/javascript/helpers/api.ts",
"eslint.config.ts",
"spec/javascript/controllers/mark_all_as_read_controller_spec.ts",
"spec/javascript/controllers/star_toggle_controller_spec.ts",
"spec/javascript/controllers/unread_count_controller_spec.ts",
"spec/javascript/helpers/api_spec.ts",
"types/eslint-plugin-sort-keys-fix.d.ts",
Expand All @@ -47,12 +44,10 @@ const config: Linter.Config[] = [
"@stylistic/max-len": "off",
},
},
// Offense count: 11
// Offense count: 9
{
files: [
"app/javascript/controllers/keep_unread_toggle_controller.ts",
"app/javascript/controllers/star_toggle_controller.ts",
"spec/javascript/controllers/star_toggle_controller_spec.ts",
"spec/javascript/controllers/unread_count_controller_spec.ts",
"spec/javascript/helpers/ensure_spec.ts",
"spec/javascript/support/stimulus.ts",
Expand Down
4 changes: 2 additions & 2 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ li.story > turbo-frame {
display: block;
}

.story-refresh-form {
.story-refresh-form, .story-star-form {
display: inline;
}

.story-refresh {
.story-refresh, .story-starred {
padding: 0;
color: inherit;
background: none;
Expand Down
11 changes: 5 additions & 6 deletions app/components/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# state classes; its contents sit in a Turbo Frame so refreshing the story
# from its feed swaps them in place (see `Components::StoryFrame`).
class Components::Story < Components::Base
# The refresh form submits inside the story's frame: sample stories skip
# it, and a failed refresh leaves the story as it was.
# The star and refresh forms submit inside the story's frame: sample
# stories skip them, and a failed refresh leaves the story as it was.
ACTIONS = [
"keep-unread-toggle:toggled->story#keepUnreadToggled",
"submit->story#refreshSubmitted",
"submit->story#formSubmitted",
"turbo:frame-missing->story#frameMissing"
].join(" ").freeze

Expand All @@ -31,7 +31,7 @@ def classes

def data
{
controller: "story star-toggle keep-unread-toggle",
controller: "story keep-unread-toggle",
action: ACTIONS,
**values,
story_list_target: "story",
Expand All @@ -43,8 +43,7 @@ def values
read_state = { is_read: story.is_read, keep_unread: story.keep_unread }
{
**controller_values("story", **read_state),
**controller_values("keep_unread_toggle", **read_state),
**controller_values("star_toggle", starred: story.is_starred)
**controller_values("keep_unread_toggle", **read_state)
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/components/story_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def view_template
div { span(class: "story-published") { story.pretty_date } }
div(class: "story-actions") do
keep_unread_button
render(Components::StoryStar.new(story:, hotkey: "s"))
render(Components::StoryStar.new(story:, placement: :actions))
refresh_button
permalink_link
end
Expand Down
2 changes: 1 addition & 1 deletion app/components/story_frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def preview
data = { action: "click->story#previewClicked" }
div(class: "row story-preview", data:) do
div(class: "col-md-3") do
render(Components::StoryStar.new(story:))
render(Components::StoryStar.new(story:, placement: :preview))
p(class: "blog-title") { story.source }
end
div(class: "col-md-9") { details }
Expand Down
40 changes: 28 additions & 12 deletions app/components/story_star.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
# frozen_string_literal: true

# A star toggle button for a story. Each story shows two: one in its preview
# row and one in its actions, which also answers the s hotkey.
# A star button for a story. Each story shows two: one in its preview row and
# one in its actions, which also answers the s hotkey.
#
# The button submits a form; the response is a Turbo Stream that replaces both
# buttons (see `Components::StoryStarStream`), so the icon only changes once
# the server has saved the star.
class Components::StoryStar < Components::Base
attr_accessor :story, :hotkey
PLACEMENTS = [:preview, :actions].freeze

def initialize(story:, hotkey: nil)
attr_accessor :story, :placement

def self.dom_id(story, placement)
"story_#{story.id}_star_#{placement}"
end

def initialize(story:, placement:)
super()
self.story = story
self.hotkey = hotkey
self.placement = placement
end

def view_template
div(class: "story-starred", data:) do
icon = story.is_starred ? "fa-star" : "fa-star-o"
i(class: "fa #{icon}", data: { star_toggle_target: "icon" })
url = "/stories/#{story.id}/star"
id = self.class.dom_id(story, placement)
form_with(url:, method: :patch, class: "story-star-form", id:) do
input(type: "hidden", name: "starred", value: (!story.is_starred).to_s)
star_button
end
end

private

def data
data = { action: "click->star-toggle#toggle:stop" }
return data unless hotkey
def star_button
aria_label = t(story.is_starred ? "stories.unstar" : "stories.star")
button(type: "submit", class: "story-starred", aria_label:, data:) do
i(class: "fa #{story.is_starred ? 'fa-star' : 'fa-star-o'}")
end
end

{ **data, hotkeys_target: "click", hotkey: }
def data
placement == :actions ? { hotkeys_target: "click", hotkey: "s" } : {}
end
end
23 changes: 23 additions & 0 deletions app/components/story_star_stream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# The response to starring or unstarring a story: a Turbo Stream that
# replaces both of the story's star buttons with their updated state.
class Components::StoryStarStream < Components::Base
register_element :turbo_stream

attr_accessor :story

def initialize(story:)
super()
self.story = story
end

def view_template
Components::StoryStar::PLACEMENTS.each do |placement|
target = Components::StoryStar.dom_id(story, placement)
turbo_stream(action: "replace", target:) do
template { render(Components::StoryStar.new(story:, placement:)) }
end
end
end
end
12 changes: 12 additions & 0 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def refresh
render(Components::StoryFrame.new(story:), layout: false, status:)
end

# Responds with a Turbo Stream that replaces the story's star buttons.
def star
story = authorization.check(StoryRepository.fetch(params[:id]))
story.update!(is_starred: params[:starred] == "true")

render(
Components::StoryStarStream.new(story:),
layout: false,
content_type: "text/vnd.turbo-stream.html"
)
end

def mark_all_as_read
stories = authorization.scope(Story.where(id: params[:story_ids]))
MarkAllAsRead.call(stories.ids)
Expand Down
3 changes: 0 additions & 3 deletions app/javascript/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ application.register("keep-unread-toggle", KeepUnreadToggleController);
import MarkAllAsReadController from "./mark_all_as_read_controller";
application.register("mark-all-as-read", MarkAllAsReadController);

import StarToggleController from "./star_toggle_controller";
application.register("star-toggle", StarToggleController);

import StoryController from "./story_controller";
application.register("story", StoryController);

Expand Down
34 changes: 0 additions & 34 deletions app/javascript/controllers/star_toggle_controller.ts

This file was deleted.

9 changes: 7 additions & 2 deletions app/javascript/controllers/story_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export default class extends Controller {
}

previewClicked(event: MouseEvent): void {
// The preview's star button submits its own form.
if (event.target instanceof Element && event.target.closest("form")) {
return;
}

if (opensInTab(event)) {
this.openInBackgroundTab();
return;
Expand Down Expand Up @@ -89,8 +94,8 @@ export default class extends Controller {
this.dispatch("read-changed");
}

// Sample stories have no feed to refresh from, so skip the request.
refreshSubmitted(event: Event): void {
// Sample stories aren't saved, so their star and refresh forms do nothing.
formSubmitted(event: Event): void {
if (this.isSample()) {
event.preventDefault();
}
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ en:
stories:
keep_unread: Keep unread
refresh: Refresh story from feed
star: Star
unstar: Unstar
time:
formats:
default: '%b %d, %H:%M'
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
get "/starred", to: "stories#starred"
put "/stories/:id", to: "stories#update"
post "/stories/:id/refresh", to: "stories#refresh"
patch "/stories/:id/star", to: "stories#star"
post "/stories/mark_all_as_read", to: "stories#mark_all_as_read"
end
18 changes: 6 additions & 12 deletions spec/components/story_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,25 @@ def story_li(story)
it "sets the Stimulus controllers on the li" do
li = story_li(create(:story))

expect(li["data-controller"]).to eq("story star-toggle keep-unread-toggle")
expect(li["data-controller"]).to eq("story keep-unread-toggle")
end

def values(element)
names = [
"story-id",
"story-keep-unread",
"keep-unread-toggle-is-read",
"star-toggle-starred"
]
names = ["story-id", "story-keep-unread", "keep-unread-toggle-is-read"]
names.map { |name| element["data-#{name}-value"] }
end

it "sets the Stimulus values on the li" do
story = create(:story, keep_unread: true, is_starred: true)
story = create(:story, keep_unread: true)

expect(values(story_li(story)))
.to eq([story.id.to_s, "true", "false", "true"])
expect(values(story_li(story))).to eq([story.id.to_s, "true", "false"])
end

it "wires the refresh form's submit and missing frame actions" do
it "wires the form submit and missing frame actions" do
li = story_li(create(:story))

expect(li["data-action"].split).to include(
"submit->story#refreshSubmitted",
"submit->story#formSubmitted",
"turbo:frame-missing->story#frameMissing"
)
end
Expand Down
56 changes: 56 additions & 0 deletions spec/components/story_star_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

RSpec.describe Components::StoryStar do
def render_star(story, placement)
html = ApplicationController.render(
described_class.new(story:, placement:),
layout: false
)
Capybara.string(html)
end

it "posts to the story's star path with an id for its placement" do
story = create(:story)
form = render_star(story, :preview).find("form")

expect([form[:action], form[:id]])
.to eq(["/stories/#{story.id}/star", "story_#{story.id}_star_preview"])
end

it "asks to star an unstarred story" do
rendered = render_star(create(:story), :preview)

expect(rendered).to have_field(name: "starred", type: :hidden, with: "true")
end

it "asks to unstar a starred story" do
rendered = render_star(create(:story, :starred), :preview)

expect(rendered)
.to have_field(name: "starred", type: :hidden, with: "false")
end

it "shows a filled star for a starred story" do
expect(render_star(create(:story, :starred), :preview))
.to have_css("button.story-starred .fa-star")
end

it "labels the button with what it will do" do
button = render_star(create(:story, :starred), :preview).find("button")

expect(button["aria-label"]).to eq("Unstar")
end

it "answers the s hotkey from the actions row" do
button = render_star(create(:story), :actions).find("button")

expect([button["data-hotkey"], button["data-hotkeys-target"]])
.to eq(["s", "click"])
end

it "leaves the preview row's star without a hotkey" do
button = render_star(create(:story), :preview).find("button")

expect(button["data-hotkey"]).to be_nil
end
end
Loading
Loading