-
Notifications
You must be signed in to change notification settings - Fork 113
Warn users before leaving playground with unsaved changes #456
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
Open
shubhamjrd4559-sudo
wants to merge
1
commit into
piyushdotcomm:main
Choose a base branch
from
shubhamjrd4559-sudo:codex/unsaved-changes-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,67 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| import { renderHook } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { useUnsavedChangesWarning } from "./useUnsavedChangesWarning"; | ||
|
|
||
| describe("useUnsavedChangesWarning", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| document.body.innerHTML = ""; | ||
| }); | ||
|
|
||
| it("does not block unload when there are no unsaved changes", () => { | ||
| renderHook(() => useUnsavedChangesWarning(false)); | ||
|
|
||
| const event = new Event("beforeunload", { cancelable: true }); | ||
|
|
||
| expect(window.dispatchEvent(event)).toBe(true); | ||
| expect(event.defaultPrevented).toBe(false); | ||
| }); | ||
|
|
||
| it("blocks unload when there are unsaved changes", () => { | ||
| renderHook(() => useUnsavedChangesWarning(true)); | ||
|
|
||
| const event = new Event("beforeunload", { cancelable: true }); | ||
|
|
||
| expect(window.dispatchEvent(event)).toBe(false); | ||
| expect(event.defaultPrevented).toBe(true); | ||
| }); | ||
|
|
||
| it("prevents guarded navigation when the user cancels", () => { | ||
| const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(false); | ||
| renderHook(() => useUnsavedChangesWarning(true)); | ||
|
|
||
| const link = document.createElement("a"); | ||
| link.href = "/dashboard"; | ||
| document.body.appendChild(link); | ||
|
|
||
| const event = new MouseEvent("click", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| button: 0, | ||
| }); | ||
|
|
||
| expect(link.dispatchEvent(event)).toBe(false); | ||
| expect(event.defaultPrevented).toBe(true); | ||
| expect(confirmSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("returns a confirmNavigation helper for imperative exits", () => { | ||
| const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(false); | ||
| const { result } = renderHook(() => useUnsavedChangesWarning(true)); | ||
|
|
||
| expect(result.current.confirmNavigation()).toBe(false); | ||
| expect(confirmSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("allows imperative exits when the user confirms", () => { | ||
| const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(true); | ||
| const { result } = renderHook(() => useUnsavedChangesWarning(true)); | ||
|
|
||
| expect(result.current.confirmNavigation()).toBe(true); | ||
| expect(confirmSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
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,80 @@ | ||
| "use client"; | ||
|
|
||
| import { useCallback, useEffect } from "react"; | ||
|
|
||
| const UNSAVED_CHANGES_MESSAGE = | ||
| "You have unsaved changes. Are you sure you want to leave the playground?"; | ||
|
|
||
| export function useUnsavedChangesWarning(hasUnsavedChanges: boolean) { | ||
| const confirmNavigation = useCallback(() => { | ||
| if (!hasUnsavedChanges) return true; | ||
| return window.confirm(UNSAVED_CHANGES_MESSAGE); | ||
| }, [hasUnsavedChanges]); | ||
|
|
||
| useEffect(() => { | ||
| if (!hasUnsavedChanges) return; | ||
|
|
||
| const handleBeforeUnload = (event: BeforeUnloadEvent) => { | ||
| event.preventDefault(); | ||
| event.returnValue = UNSAVED_CHANGES_MESSAGE; | ||
| return UNSAVED_CHANGES_MESSAGE; | ||
| }; | ||
|
|
||
| window.addEventListener("beforeunload", handleBeforeUnload); | ||
| return () => window.removeEventListener("beforeunload", handleBeforeUnload); | ||
| }, [hasUnsavedChanges]); | ||
|
|
||
| useEffect(() => { | ||
| if (!hasUnsavedChanges) return; | ||
|
|
||
| const handleDocumentClick = (event: MouseEvent) => { | ||
| if ( | ||
| event.defaultPrevented || | ||
| event.button !== 0 || | ||
| event.metaKey || | ||
| event.ctrlKey || | ||
| event.shiftKey || | ||
| event.altKey | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const target = event.target instanceof Element ? event.target : null; | ||
| const anchor = target?.closest<HTMLAnchorElement>("a[href]"); | ||
|
|
||
| if ( | ||
| !anchor || | ||
| anchor.target === "_blank" || | ||
| anchor.hasAttribute("download") | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const rawHref = anchor.getAttribute("href"); | ||
| if ( | ||
| !rawHref || | ||
| rawHref.startsWith("#") || | ||
| rawHref.startsWith("mailto:") || | ||
| rawHref.startsWith("tel:") | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const nextUrl = new URL(anchor.href, window.location.href); | ||
| const currentUrl = new URL(window.location.href); | ||
|
|
||
| if (nextUrl.href === currentUrl.href || confirmNavigation()) { | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| event.stopImmediatePropagation(); | ||
| }; | ||
|
|
||
| document.addEventListener("click", handleDocumentClick, true); | ||
| return () => document.removeEventListener("click", handleDocumentClick, true); | ||
| }, [confirmNavigation, hasUnsavedChanges]); | ||
|
|
||
| return { confirmNavigation }; | ||
| } | ||
|
|
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.