Skip to content

Commit 817deb3

Browse files
authored
WEB-145 Add preview button to save toast (#126)
- Add preview button to save toast - Consolidate preview URL building logic into shared helper
2 parents bbfa5e9 + c821887 commit 817deb3

5 files changed

Lines changed: 42 additions & 11 deletions

File tree

src/app/editor/[id]/[slug]/SaveButton.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useTransition } from "react";
55
import { toast } from "sonner";
66
import { useDocumentContext } from "./client";
77
import { saveVersionAction } from "../../../../lib/documents/actions";
8-
import { getEditorUrl } from "../../../../lib/editor-url";
8+
import { getEditorUrl, getPreviewUrl } from "../../../../lib/editor-url";
99
import { runAction } from "../../runAction";
1010
import { useDialogs } from "@/components/ui/dialog-provider";
1111

@@ -29,7 +29,17 @@ export function SaveButton() {
2929

3030
addVersion(result.data.version);
3131
window.history.replaceState(null, "", getEditorUrl(documentId, documentName, result.data.version.id));
32-
toast.success("Saved");
32+
33+
const previewUrl = getPreviewUrl(documentId, documentName, result.data.version.id);
34+
toast.success("Saved", {
35+
action: {
36+
label: "Preview",
37+
38+
onClick: () => {
39+
window.open(previewUrl, "_blank");
40+
},
41+
},
42+
});
3343
});
3444
};
3545

src/app/editor/[id]/[slug]/VersionListPanel.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface VersionListPanelProps {
99
publishedVersionId?: number | null;
1010
onLoadVersion: (versionId: number) => void;
1111
onPublishVersion: (versionId: number) => void;
12-
previewBaseUrl: string;
12+
getPreviewUrlForVersion: (versionId: number) => string;
1313
isPublishing?: boolean;
1414
isPublishDisabled?: boolean;
1515
}
@@ -27,7 +27,7 @@ function VersionEntry({
2727
isPublished,
2828
onLoad,
2929
onPublish,
30-
previewBaseUrl,
30+
getPreviewUrlForVersion,
3131
isPublishing,
3232
isPublishDisabled,
3333
}: {
@@ -36,7 +36,7 @@ function VersionEntry({
3636
isPublished: boolean;
3737
onLoad: () => void;
3838
onPublish: () => void;
39-
previewBaseUrl: string;
39+
getPreviewUrlForVersion: (versionId: number) => string;
4040
isPublishing?: boolean;
4141
isPublishDisabled?: boolean;
4242
}) {
@@ -79,7 +79,7 @@ function VersionEntry({
7979
</button>
8080
)}
8181
<a
82-
href={`${previewBaseUrl}/${version.id}/preview`}
82+
href={getPreviewUrlForVersion(version.id)}
8383
target="_blank"
8484
rel="noopener noreferrer"
8585
onClick={(e) => e.stopPropagation()}
@@ -101,7 +101,7 @@ export function VersionListPanel({
101101
publishedVersionId,
102102
onLoadVersion,
103103
onPublishVersion,
104-
previewBaseUrl,
104+
getPreviewUrlForVersion,
105105
isPublishing,
106106
isPublishDisabled,
107107
}: VersionListPanelProps) {
@@ -132,7 +132,7 @@ export function VersionListPanel({
132132
isPublished={version.id === publishedVersionId}
133133
onLoad={() => onLoadVersion(version.id)}
134134
onPublish={() => onPublishVersion(version.id)}
135-
previewBaseUrl={previewBaseUrl}
135+
getPreviewUrlForVersion={getPreviewUrlForVersion}
136136
isPublishing={isPublishing}
137137
isPublishDisabled={isPublishDisabled}
138138
/>

src/app/editor/[id]/[slug]/VersionPluginContainer.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useTransition } from "react";
44
import { useRouter } from "next/navigation";
55
import { useDocumentContext } from "./client";
66
import { publishVersionAction } from "../../../../lib/documents/actions";
7-
import { getEditorUrl } from "../../../../lib/editor-url";
7+
import { getEditorUrl, getPreviewUrl } from "../../../../lib/editor-url";
88
import { runAction } from "../../runAction";
99
import { VersionListPanel } from "./VersionListPanel";
1010
import { useDialogs } from "@/components/ui/dialog-provider";
@@ -43,7 +43,9 @@ export function VersionPluginContainer() {
4343
publishedVersionId={publishedVersionId}
4444
onLoadVersion={handleLoadVersion}
4545
onPublishVersion={handlePublishVersion}
46-
previewBaseUrl={getEditorUrl(documentId, documentName)}
46+
getPreviewUrlForVersion={(targetVersionId) =>
47+
getPreviewUrl(documentId, documentName, targetVersionId)
48+
}
4749
isPublishing={isPublishing}
4850
isPublishDisabled={isArchived}
4951
/>

src/lib/editor-url.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import { getEditorSlug, getEditorUrl } from "./editor-url";
2+
import { getEditorSlug, getEditorUrl, getPreviewUrl } from "./editor-url";
33

44
describe("getEditorSlug", () => {
55
it.each<[string | null, string]>([
@@ -29,3 +29,14 @@ describe("getEditorUrl", () => {
2929
expect(getEditorUrl(id, name, versionId)).toBe(expected);
3030
});
3131
});
32+
33+
describe("getPreviewUrl", () => {
34+
it.each<[number, string | null, number | undefined, string]>([
35+
[4, "Hello World", undefined, "/editor/4/hello-world/preview"],
36+
[4, "Hello World", 23, "/editor/4/hello-world/23/preview"],
37+
[4, null, undefined, "/editor/4/untitled/preview"],
38+
[4, null, 10, "/editor/4/untitled/10/preview"],
39+
])("getPreviewUrl(%j, %j, %j) → %j", (id, name, versionId, expected) => {
40+
expect(getPreviewUrl(id, name, versionId)).toBe(expected);
41+
});
42+
});

src/lib/editor-url.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ export function getEditorUrl(
3131
const base = `/editor/${documentId}/${slug}`;
3232
return versionId !== undefined ? `${base}/${versionId}` : base;
3333
}
34+
35+
export function getPreviewUrl(
36+
documentId: number,
37+
documentName: string | null,
38+
versionId?: number,
39+
): string {
40+
return `${getEditorUrl(documentId, documentName, versionId)}/preview`;
41+
}

0 commit comments

Comments
 (0)