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
35 changes: 2 additions & 33 deletions .eslint_todo.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
// This configuration was generated by `exe/eslint_autogen`
// on 2026-11-28 02:54:57 UTC.
// on 2026-11-28 04:52:47 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: 2
{
files: [
"spec/javascript/helpers/api_spec.ts",
],
rules: {
"@stylistic/dot-location": "off",
},
},
// Offense count: 52
// Offense count: 36
{
files: [
"app/javascript/controllers/mark_all_as_read_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/unread_count_controller_spec.ts",
"spec/javascript/helpers/api_spec.ts",
"types/eslint-plugin-sort-keys-fix.d.ts",
"vitest.config.ts",
],
Expand Down Expand Up @@ -62,17 +51,6 @@ const config: Linter.Config[] = [
"@stylistic/quote-props": "off",
},
},
// Offense count: 5
{
files: [
"app/javascript/controllers/story_controller.ts",
"spec/javascript/controllers/story_controller_spec.ts",
"spec/javascript/helpers/api_spec.ts",
],
rules: {
"camelcase": "off",
},
},
// Offense count: 2
{
files: [
Expand All @@ -91,15 +69,6 @@ const config: Linter.Config[] = [
"max-len": "off",
},
},
// Offense count: 1
{
files: [
"spec/javascript/helpers/api_spec.ts",
],
rules: {
"vitest/no-hooks": "off",
},
},
];

export default config;
1 change: 0 additions & 1 deletion app/components/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def data

def values
{
story_id_value: story.id.to_s,
story_is_read_value: story.is_read.to_s,
story_keep_unread_value: story.keep_unread.to_s
}
Expand Down
1 change: 1 addition & 0 deletions app/components/story_frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def initialize(story:)
def view_template
turbo_frame(id: "story_#{story.id}") do
preview
render(Components::StoryMarkRead.new(story:))
div(class: "story-body-container") do
story_body
render(Components::StoryActions.new(story:))
Expand Down
24 changes: 24 additions & 0 deletions app/components/story_mark_read.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# A form with no visible fields that the story controller submits when the
# story opens. The response is a Turbo Stream that replaces the keep unread
# form, whose saved state then marks the story read (see
# `Components::StoryKeepUnread`).
class Components::StoryMarkRead < Components::Base
attr_accessor :story

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

# The empty block matters: without one, form_with leaves the form open.
def view_template
form_with(
url: "/stories/#{story.id}/read",
method: :patch,
class: "story-mark-read-form",
data: { story_target: "markRead" }
) { nil }
end
end
19 changes: 10 additions & 9 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ def index
render(Views::Stories::Index.new(stories:))
end

def update
json_params = JSON.parse(request.body.read, symbolize_names: true)

story = authorization.check(StoryRepository.fetch(params[:id]))
story.update!(json_params.slice(:is_read, :is_starred, :keep_unread))

head(:no_content)
end

def refresh
story = authorization.check(StoryRepository.fetch(params[:id]))

Expand Down Expand Up @@ -52,6 +43,16 @@ def keep_unread
render_replacements(Components::StoryKeepUnread.replacements(story))
end

# Marks the story read when it opens, unless it's kept unread. Responds
# with a Turbo Stream that replaces the keep unread form with the saved
# state, which the story controller copies onto the story.
def read
story = authorization.check(StoryRepository.fetch(params[:id]))
story.update!(is_read: true) unless story.keep_unread

render_replacements(Components::StoryKeepUnread.replacements(story))
end

def mark_all_as_read
stories = authorization.scope(Story.where(id: params[:story_ids]))
MarkAllAsRead.call(stories.ids)
Expand Down
41 changes: 15 additions & 26 deletions app/javascript/controllers/story_controller.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { Controller } from "@hotwired/stimulus";

import { updateStory } from "helpers/api";

// Cmd, ctrl and middle clicks open the story in a background tab.
function opensInTab(event: MouseEvent): boolean {
return event.metaKey || event.ctrlKey || event.button === 1;
}

/*
* Owns a story's open and read state: the "open" and "read" classes on its
* element, saving read state to the server, and announcing changes as
* bubbling "story:opened" and "story:read-changed" events.
* element, and bubbling "story:opened" and "story:read-changed" events.
* Saving goes through forms in the story's Turbo Frame; the controller never
* talks to the server itself.
*/
export default class extends Controller {
static override values = { id: String, isRead: Boolean, keepUnread: Boolean };
static override values = { isRead: Boolean, keepUnread: Boolean };

/*
* The permalink sits inside the story's Turbo Frame, so a refresh updates
* it. The readState target is the keep unread form, which the server
* replaces with the saved read and keep unread state.
* it. The markRead form saves read state when the story opens. The
* readState target is the keep unread form, which the server replaces with
* the saved read and keep unread state.
*/
static override targets = ["permalink", "readState"];

declare idValue: string;
static override targets = ["permalink", "markRead", "readState"];

declare permalinkTarget: HTMLAnchorElement;

declare markReadTarget: HTMLFormElement;

declare isReadValue: boolean;

declare keepUnreadValue: boolean;
Expand Down Expand Up @@ -72,22 +72,16 @@ export default class extends Controller {
}
}

/*
* Asks the server to mark the story read. The response replaces the
* readState target, which applies the "read" class once it's saved.
*/
markAsRead(): void {
if (this.isReadValue || this.keepUnreadValue) {
return;
}

this.isReadValue = true;
this.element.classList.add("read");
this.dispatch("read-changed");

if (this.isSample()) {
return;
}

updateStory(this.idValue, { is_read: true }).catch(() => {
// Optimistic UI — ignore server errors
});
this.markReadTarget.requestSubmit();
}

// Copies the saved state from a newly rendered keep unread form.
Expand Down Expand Up @@ -116,11 +110,6 @@ export default class extends Controller {
}
}

// Sample stories on the tutorial page have fake, non-positive ids.
private isSample(): boolean {
return Number(this.idValue) <= 0;
}

private openInBackgroundTab(): void {
window.open(this.permalinkTarget.href);
window.focus();
Expand Down
42 changes: 33 additions & 9 deletions app/javascript/controllers/tutorial_stories_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,44 @@ function setIcon(form: HTMLFormElement, icon: string): void {
}

/*
* Swaps in an updated copy of the keep unread form, like the keep unread
* stream does. The story controller copies the new state onto the story
* Swaps in an updated copy of the keep unread form, like the keep unread and
* read streams do. The story controller copies the new state onto the story
* when the copy connects.
*/
function replaceReadState(
form: HTMLFormElement,
update: (copy: HTMLFormElement) => void,
): void {
const copy = form.cloneNode(true);
if (copy instanceof HTMLFormElement) {
update(copy);
form.replaceWith(copy);
}
}

function keepUnread(form: HTMLFormElement): void {
const kept = hiddenValue(form, "keep_unread").value === "true";
const copy = form.cloneNode(true);
if (!(copy instanceof HTMLFormElement)) {

replaceReadState(form, (copy) => {
copy.dataset.keepUnread = String(kept);
copy.dataset.isRead = String(!kept);
hiddenValue(copy, "keep_unread").value = String(!kept);
setIcon(copy, keepUnreadIcon(kept));
});
}

// Marks the story read through its keep unread form, like the read stream.
function markRead(form: HTMLFormElement): void {
const story = form.closest("li.story");
const keepUnreadForm =
story?.querySelector<HTMLFormElement>(".story-keep-unread-form");
if (!keepUnreadForm) {
return;
}

copy.dataset.keepUnread = String(kept);
copy.dataset.isRead = String(!kept);
hiddenValue(copy, "keep_unread").value = String(!kept);
setIcon(copy, keepUnreadIcon(kept));
form.replaceWith(copy);
replaceReadState(keepUnreadForm, (copy) => {
copy.dataset.isRead = "true";
});
}

// Spins the refresh icon for a moment, as a real refresh does.
Expand Down Expand Up @@ -79,6 +101,8 @@ export default class extends Controller {
this.star(form);
} else if (form.classList.contains("story-keep-unread-form")) {
keepUnread(form);
} else if (form.classList.contains("story-mark-read-form")) {
markRead(form);
} else if (form.classList.contains("story-refresh-form")) {
refresh(form);
}
Expand Down
22 changes: 0 additions & 22 deletions app/javascript/helpers/api.ts

This file was deleted.

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
get "/setup/password", to: "passwords#new"
get "/setup/tutorial", to: "tutorials#index"
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"
patch "/stories/:id/keep_unread", to: "stories#keep_unread"
patch "/stories/:id/read", to: "stories#read"
post "/stories/mark_all_as_read", to: "stories#mark_all_as_read"
end
23 changes: 20 additions & 3 deletions spec/components/story_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,31 @@ def story_li(story)
end

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

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

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

it "includes a hidden form for marking the story read" do
story = create(:story)
form = render_story(story).find("form.story-mark-read-form", visible: :all)

expect([form[:action], form["data-story-target"]])
.to eq(["/stories/#{story.id}/read", "markRead"])
end

it "doesn't nest the story's other forms inside the mark read form" do
rendered = render_story(create(:story))

expect(rendered).to have_no_css(
"form.story-mark-read-form form",
visible: :all
)
end

it "keeps the story in place when a refresh fails" do
Expand Down Expand Up @@ -141,7 +158,7 @@ def values(element)

it "posts the refresh form to the story's refresh path" do
story = create(:story)
form = render_story(story).find("form:has(button.story-refresh)")
form = render_story(story).find("form.story-refresh-form")

expect(form[:action]).to eq("/stories/#{story.id}/refresh")
end
Expand Down
Loading
Loading