sanity-io / visual-editing

https://visual-editing-studio.sanity.build
MIT License
37 stars 20 forks source link

feat: add `useIsLivePreview` & `useIsPresentationTool` hooks #2050

Closed stipsan closed 1 month ago

stipsan commented 1 month ago

The new hooks are higher level, and tailored to the use cases I've found is needed in most templates:

  1. I want to show a UI warning that the app is in draft mode when outside of Presentation Tool, so editors don't freak out and think they've accidentally published some content.
  2. In production it's awesome to show a "A new post is available" toast with a refresh button so that my visitors can consume my content in peace and decide when to see the new content. In live preview however I don't want my editors to constantly hit a refresh button toast every time they make a draft edit before they can see it.

I want to only show a Draft Mode message if I'm not in the Presentation Tool:

"use client";

import { useIsPresentationTool } from "next-sanity/hooks";
import { useEffect } from "react";
import { toast } from "sonner";
import { disableDraftMode } from "./actions";

export default function DraftModeToast() {
  const isPresentationTool = useIsPresentationTool();

  useEffect(() => {
    if(isPresentationTool === false) {
      toast("Draft Mode Enabled", {
        action: {
          label: "Disable",
          onClick: () => disableDraftMode(),
        },
      });
    }
  }, [isPresentationTool]);

  return null;
}

I want to delay layout shift in production, and passthrough in live preview:

"use client";

import { useIsLivePreview } from "next-sanity/hooks";
import { useEffect } from "react";
import { toast } from "sonner";
import { useDeferredLayoutShift } from "./use-deferred-transition";

/**
 * Suspends layout shift for the more stories section when a new post is published.
 * On changes it'll require opt-in form the user before the post is shown.
 * If the post itself is edited, it'll refresh automatically to allow fixing typos.
 */

export function MoreStoriesLayoutShift(props: {
  children: React.ReactNode;
  ids: string[];
}) {
  const [children, pending, startViewTransition] = useDeferredLayoutShift(
    props.children,
    props.ids,
  );
  const isLivePreview = useIsLivePreview();

  /**
   * We need to suspend layout shift for user opt-in.
   */
  useEffect(() => {
    if(pending && isLivePreview === false) {
      toast("More stories have been published", {
        id: "more-stories-layout-shift",
        duration: Infinity,
        action: {
          label: "Refresh",
          onClick: () => startViewTransition(),
        },
      });
    }
  }, [pending, isLivePreview, startViewTransition]);

  /**
   * If we're in Live Preview we want our editors to see their changes right away,
   * without requiring any user interactions, so they can preview on many screens and devices all at once
   */
  if(isLivePreview) {
    return props.children
  }

  return children;
}
vercel[bot] commented 1 month ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
live-visual-editing-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 24, 2024 2:38pm
visual-editing-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 24, 2024 2:38pm
visual-editing-page-builder-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 24, 2024 2:38pm
visual-editing-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 24, 2024 2:38pm
6 Skipped Deployments | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **visual-editing-astro** | ⬜️ Skipped ([Inspect](https://vercel.com/sanity-sandbox/visual-editing-astro/AG2s3GCo8FVUL3aeBRMfZG7fUCPu)) | | | Oct 24, 2024 2:38pm | | **visual-editing-next-with-i18n** | ⬜️ Skipped ([Inspect](https://vercel.com/sanity-sandbox/visual-editing-next-with-i18n/HuWDNrEMHuxEMSZiTMA3VKBm6k7v)) | | | Oct 24, 2024 2:38pm | | **visual-editing-nuxt** | ⬜️ Skipped ([Inspect](https://vercel.com/sanity-sandbox/visual-editing-nuxt/GvkRvjaXnZpuASKW7gYoZjWxjELK)) | | | Oct 24, 2024 2:38pm | | **visual-editing-remix** | ⬜️ Skipped ([Inspect](https://vercel.com/sanity-sandbox/visual-editing-remix/8G9F8cZBVBcvogn29P7WnnZ56Yjz)) | | | Oct 24, 2024 2:38pm | | **visual-editing-studio** | ⬜️ Skipped ([Inspect](https://vercel.com/sanity-sandbox/visual-editing-studio/EseBS5qAzgp5MuoyyMohMDrA61eB)) | | | Oct 24, 2024 2:38pm | | **visual-editing-svelte** | ⬜️ Skipped ([Inspect](https://vercel.com/sanity-sandbox/visual-editing-svelte/D5ouw8zTCNn7CD4n6JYUQLTc3nwX)) | | | Oct 24, 2024 2:38pm |