Skip to content
Merged
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
53 changes: 36 additions & 17 deletions frontend/src/components/Scratch/ScratchToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "@primer/octicons-react";
import clsx from "clsx";
import ContentEditable from "react-contenteditable";
import Link from "next/link";
import { useRouter } from "next/navigation";

import TimeAgo from "@/components/TimeAgo";
Expand Down Expand Up @@ -149,6 +150,40 @@ function ScratchName({
}
}

function NewScratchButton({ isDirty }: { isDirty: boolean }) {
const router = useRouter();

const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
if (
e.metaKey ||
e.ctrlKey ||
e.shiftKey ||
e.altKey ||
e.button !== 0
) {
return;
}

e.preventDefault();

if (
!isDirty ||
confirm(
"This scratch has pending changes, are you sure you want to navigate away?",
)
) {
router.push("/new");
}
};

return (
<Link href="/new" onClick={handleClick}>
<FileIcon />
New
</Link>
);
}

function Actions({
isCompiling,
isDirty,
Expand All @@ -167,8 +202,6 @@ function Actions({
const [isSaving, setIsSaving] = useState(false);
const [isForking, setIsForking] = useState(false);

const router = useRouter();

const canSave = scratch.owner && userIsYou(scratch.owner);

const platform = api.usePlatform(scratch.platform);
Expand All @@ -192,21 +225,7 @@ function Actions({
return (
<ul className={styles.actions} aria-label="Scratch actions">
<li>
<button
onClick={() => {
if (
!isDirty ||
confirm(
"This scratch has pending changes, are you sure you want to navigate away?",
)
) {
router.push("/new");
}
}}
>
<FileIcon />
New
</button>
<NewScratchButton isDirty={isDirty} />
</li>
<li>
<button
Expand Down