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
14 changes: 5 additions & 9 deletions apps/web/src/app/decks/_components/deck-threads-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Alert } from "@ui/alert";
import { Entry } from "@/entities";
import { useGlobalStore } from "@/core/global-store";
import { PollsContext } from "@/features/polls";
import { usePublishHandoffWriter } from "@/app/publish/_hooks";
import { PREFIX } from "@/utils/local-storage";
import i18next from "i18next";
import { AvailableCredits, UserAvatar } from "@/features/shared";
Expand Down Expand Up @@ -50,10 +51,7 @@ export const DeckThreadsForm = ({
const { setShow, create, createReply } = useContext(DeckThreadsFormContext);
const { clearActivePoll } = useContext(PollsContext);

const [localDraft, setLocalDraft] = useLocalStorage<Record<string, any>>(
PREFIX + "_local_draft",
{}
);
const stageForPublish = usePublishHandoffWriter();
const [threadHost, setThreadHost] = useLocalStorage(PREFIX + "_dtf_th", "ecency.waves");
const [text, setText, clearText] = useLocalStorage(PREFIX + "_dtf_t", "");
const [image, setImage, clearImage] = useLocalStorage<string>(PREFIX + "_dtf_i");
Expand Down Expand Up @@ -109,12 +107,10 @@ export const DeckThreadsForm = ({
content = `${content}<br>${video}`;
}

// Push to draft built content with attachments
// Too long for a thread, so hand the built content with its attachments
// to the publish composer instead.
if (text!!.length > 250) {
setLocalDraft({
...localDraft,
body: content
});
stageForPublish(content);
window.open("/publish", "_blank");
return;
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/app/publish/_hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./use-publish-autosave";
export * from "./use-upload-tracker";
export * from "./use-upload-and-insert-images";
export * from "./use-draft-tab-coordinator";
export * from "./use-publish-handoff";
56 changes: 56 additions & 0 deletions apps/web/src/app/publish/_hooks/use-publish-handoff.ts
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);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
greptile-apps[bot] marked this conversation as resolved.
});
}
21 changes: 20 additions & 1 deletion apps/web/src/app/publish/_page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
PublishValidatePost,
PublishMultiTabWarning
} from "@/app/publish/_components";
import { usePublishAutosave, usePublishEditor, usePublishState } from "@/app/publish/_hooks";
import {
usePublishAutosave,
usePublishEditor,
usePublishHandoff,
usePublishState
} from "@/app/publish/_hooks";
import type { ImportResult } from "@/app/publish/_components/publish-import-dialog";
import { isCommunity } from "@/utils";
import routes from "@/routes";
Expand Down Expand Up @@ -55,6 +60,20 @@ export default function Publish() {
);
const appliedCommunityRef = useRef<string | null>(null);

// A wave or deck thread that outgrew its character limit stages its content
// here and opens this page. setContent alone is not enough: the editor
// prefills itself from publish state only if it initialises after this runs,
// so seed both and let whichever lands second win with the same value.
usePublishHandoff(
useCallback(
(body: string) => {
setContent(body);
setEditorContent(body);
},
[setContent, setEditorContent]
)
);

const { isActiveTab, lastSaved, draftId } = usePublishAutosave();

useEffect(() => {
Expand Down
16 changes: 5 additions & 11 deletions apps/web/src/features/waves/hooks/use-wave-submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { Entry, WaveEntry } from "@/entities";
import { DecentMemesPayload } from "@/api/decentmemes";
import { useWaveCreate } from "@/features/waves/components/wave-form/api";
import { useWaveCreateReply } from "@/features/waves/components/wave-form/api/use-wave-create-reply";
import { useLocalStorage } from "react-use";
import { PREFIX } from "@/utils/local-storage";
import { usePublishHandoffWriter } from "@/app/publish/_hooks";
import { useGlobalStore } from "@/core/global-store";
import { useMutation } from "@tanstack/react-query";
import { error, success } from "@/features/shared";
Expand Down Expand Up @@ -37,10 +36,7 @@ export function useWaveSubmit(
const { mutateAsync: create } = useWaveCreate();
const { mutateAsync: createReply } = useWaveCreateReply();

const [localDraft, setLocalDraft] = useLocalStorage<Record<string, any>>(
PREFIX + "_local_draft",
{}
);
const stageForPublish = usePublishHandoffWriter();

return useMutation({
mutationKey: ["wave-form-submit", username, replySource, editingEntry],
Expand Down Expand Up @@ -87,12 +83,10 @@ export function useWaveSubmit(
content = `${content}<br>${video}`;
}

// Push to draft built content with attachments
// Too long for a wave, so hand the built content with its attachments to
// the publish composer instead.
if (!isReply && textLength > characterLimit) {
setLocalDraft({
...localDraft,
body: content
});
stageForPublish(content);
window.open("/publish", "_blank");
return;
}
Expand Down
95 changes: 95 additions & 0 deletions apps/web/src/specs/app/publish/publish-handoff.spec.ts
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>![](https://images.ecency.com/x.png)"));

const onReceive = vi.fn();
renderHook(() => usePublishHandoff(onReceive));

expect(onReceive).toHaveBeenCalledWith("wave text<br>![](https://images.ecency.com/x.png)");
});
});
Loading