-
Notifications
You must be signed in to change notification settings - Fork 7
fix(publish): deliver overflowing waves and deck threads to the composer #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { PREFIX } from "@/utils/local-storage"; | ||
| import { useCallback } from "react"; | ||
| import useLocalStorage from "react-use/lib/useLocalStorage"; | ||
| import useMount from "react-use/lib/useMount"; | ||
|
|
||
| /** | ||
| * One-shot channel for handing content to the publish composer from another | ||
| * surface, currently a wave or a deck thread that outgrew its character limit | ||
| * and has to become a post instead. | ||
| * | ||
| * It deliberately does not reuse the submit page's local draft key. That key | ||
| * holds a full PostBase which the submit page rewrites on every keystroke, so | ||
| * writing a partial object into it both corrupted that draft's shape (a draft | ||
| * with no title crashed the submit page in applyTitle) and risked eating a | ||
| * post the user had in progress there. | ||
| */ | ||
| export interface PublishHandoff { | ||
| body: string; | ||
| } | ||
|
|
||
| export const PUBLISH_HANDOFF_KEY = PREFIX + "_pub_handoff"; | ||
|
|
||
| /** | ||
| * Stage content for the composer. The caller is expected to open /publish | ||
| * right after, in this tab or a new one. | ||
| */ | ||
| export function usePublishHandoffWriter() { | ||
| const [, setHandoff] = useLocalStorage<PublishHandoff>(PUBLISH_HANDOFF_KEY); | ||
|
|
||
| return useCallback((body: string) => setHandoff({ body }), [setHandoff]); | ||
| } | ||
|
|
||
| /** | ||
| * Consume anything staged for the composer, exactly once. The entry is dropped | ||
| * before it is handed over so that content which the editor cannot swallow | ||
| * fails a single time rather than on every subsequent visit. | ||
| */ | ||
| export function usePublishHandoff(onReceive: (body: string) => void) { | ||
| const [handoff, , removeHandoff] = useLocalStorage<PublishHandoff>(PUBLISH_HANDOFF_KEY); | ||
|
|
||
| useMount(() => { | ||
| const body = handoff?.body; | ||
|
|
||
| // Dropped before anything else, so that an entry the composer cannot use, | ||
| // whether malformed or simply empty, is cleared rather than re-examined on | ||
| // every later visit, and content it chokes on fails once rather than on | ||
| // every one. | ||
| removeHandoff(); | ||
|
|
||
| if (!body) { | ||
| return; | ||
| } | ||
|
|
||
| onReceive(body); | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { | ||
| PUBLISH_HANDOFF_KEY, | ||
| usePublishHandoff, | ||
| usePublishHandoffWriter | ||
| } from "@/app/publish/_hooks/use-publish-handoff"; | ||
| import { PREFIX } from "@/utils/local-storage"; | ||
| import { act, renderHook } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const LOCAL_DRAFT_KEY = PREFIX + "_local_draft"; | ||
|
|
||
| // A wave or deck thread over its character limit stages its content and opens | ||
| // /publish. That handoff used to be written into the submit page's local draft | ||
| // key, which nothing on /publish reads, so the content was dropped, and the | ||
| // partial write left that key holding a draft with no title that crashed | ||
| // /submit on its next visit (ECENCY-NEXT-1GJC). | ||
| describe("publish handoff", () => { | ||
| beforeEach(() => { | ||
| localStorage.clear(); | ||
| }); | ||
|
|
||
| it("stages content under its own key and leaves the submit draft alone", () => { | ||
| const draft = { title: "in progress", tags: ["hive"], body: "a body", description: null }; | ||
| localStorage.setItem(LOCAL_DRAFT_KEY, JSON.stringify(draft)); | ||
|
|
||
| const { result } = renderHook(() => usePublishHandoffWriter()); | ||
| act(() => result.current("an overflowing wave")); | ||
|
|
||
| expect(JSON.parse(localStorage.getItem(PUBLISH_HANDOFF_KEY)!)).toEqual({ | ||
| body: "an overflowing wave" | ||
| }); | ||
| expect(JSON.parse(localStorage.getItem(LOCAL_DRAFT_KEY)!)).toEqual(draft); | ||
| }); | ||
|
|
||
| it("delivers staged content to the composer", () => { | ||
| localStorage.setItem(PUBLISH_HANDOFF_KEY, JSON.stringify({ body: "an overflowing wave" })); | ||
|
|
||
| const onReceive = vi.fn(); | ||
| renderHook(() => usePublishHandoff(onReceive)); | ||
|
|
||
| expect(onReceive).toHaveBeenCalledWith("an overflowing wave"); | ||
| }); | ||
|
|
||
| it("delivers once, so a later visit opens an empty composer", () => { | ||
| localStorage.setItem(PUBLISH_HANDOFF_KEY, JSON.stringify({ body: "an overflowing wave" })); | ||
|
|
||
| const first = vi.fn(); | ||
| renderHook(() => usePublishHandoff(first)).unmount(); | ||
| expect(first).toHaveBeenCalledTimes(1); | ||
| expect(localStorage.getItem(PUBLISH_HANDOFF_KEY)).toBeNull(); | ||
|
|
||
| const second = vi.fn(); | ||
| renderHook(() => usePublishHandoff(second)); | ||
| expect(second).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does nothing when nothing is staged", () => { | ||
| const onReceive = vi.fn(); | ||
| renderHook(() => usePublishHandoff(onReceive)); | ||
|
|
||
| expect(onReceive).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("drops a staged entry with an empty body instead of leaving it behind", () => { | ||
| localStorage.setItem(PUBLISH_HANDOFF_KEY, JSON.stringify({ body: "" })); | ||
|
|
||
| const onReceive = vi.fn(); | ||
| renderHook(() => usePublishHandoff(onReceive)); | ||
|
|
||
| expect(onReceive).not.toHaveBeenCalled(); | ||
| // Nothing to deliver is still something to clean up, otherwise the entry is | ||
| // re-examined on every later visit and never consumed. | ||
| expect(localStorage.getItem(PUBLISH_HANDOFF_KEY)).toBeNull(); | ||
| }); | ||
|
|
||
| it("drops a staged entry it cannot read a body from", () => { | ||
| localStorage.setItem(PUBLISH_HANDOFF_KEY, JSON.stringify({ note: "not a handoff" })); | ||
|
|
||
| const onReceive = vi.fn(); | ||
| renderHook(() => usePublishHandoff(onReceive)); | ||
|
|
||
| expect(onReceive).not.toHaveBeenCalled(); | ||
| expect(localStorage.getItem(PUBLISH_HANDOFF_KEY)).toBeNull(); | ||
| }); | ||
|
|
||
| it("round-trips from writer to composer", () => { | ||
| const writer = renderHook(() => usePublishHandoffWriter()); | ||
| act(() => writer.result.current("wave text<br>")); | ||
|
|
||
| const onReceive = vi.fn(); | ||
| renderHook(() => usePublishHandoff(onReceive)); | ||
|
|
||
| expect(onReceive).toHaveBeenCalledWith("wave text<br>"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.