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
20 changes: 15 additions & 5 deletions components/composed/AccountDropdown/PreviewModeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
"use client";

import { useTransition } from "react";
import { useTransition, useEffect, useRef } from "react";
import { useDropdownContext } from "@/components/atomic/BaseDropdown/BaseDropdown";
import { useProgress } from "@/lib/vendor/react-transition-progress";
import { Link } from "@/components/atomic";
import { enterPreviewMode } from "./actions";

export default function PreviewModeButton({ label }: { label: string }) {
const dropdown = useDropdownContext();

const startProgress = useProgress();

const [, startTransition] = useTransition();
const [isPending, startTransition] = useTransition();
const pendingRef = useRef(false);

const handleClick = () => {
dropdown?.hide();

startTransition(async () => {
startProgress();
await enterPreviewMode();
// Hard reload rather than router.refresh() to recover from notFound()
window.location.reload();
});
};

useEffect(() => {
if (isPending && !pendingRef.current) {
pendingRef.current = true;
return;
}
if (!isPending && pendingRef.current) {
pendingRef.current = false;
window.location.reload();
return;
}
}, [isPending]);

return (
<Link as="button" type="button" onClick={handleClick}>
{label}
Expand Down
2 changes: 0 additions & 2 deletions components/composed/AccountDropdown/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use server";

import { draftMode } from "next/headers";
import { revalidatePath } from "next/cache";
import joinURL from "url-join";
import {
auth,
Expand All @@ -21,7 +20,6 @@ export async function signIn() {

export async function enterPreviewMode() {
(await draftMode()).enable();
revalidatePath("/", "layout");
}

export async function signOut() {
Expand Down
Loading