@@ -18,6 +18,7 @@ import {
1818} from "@primer/octicons-react" ;
1919import clsx from "clsx" ;
2020import ContentEditable from "react-contenteditable" ;
21+ import Link from "next/link" ;
2122import { useRouter } from "next/navigation" ;
2223
2324import TimeAgo from "@/components/TimeAgo" ;
@@ -149,6 +150,40 @@ function ScratchName({
149150 }
150151}
151152
153+ function NewScratchButton ( { isDirty } : { isDirty : boolean } ) {
154+ const router = useRouter ( ) ;
155+
156+ const handleClick = ( e : React . MouseEvent < HTMLAnchorElement > ) => {
157+ if (
158+ e . metaKey ||
159+ e . ctrlKey ||
160+ e . shiftKey ||
161+ e . altKey ||
162+ e . button !== 0
163+ ) {
164+ return ;
165+ }
166+
167+ e . preventDefault ( ) ;
168+
169+ if (
170+ ! isDirty ||
171+ confirm (
172+ "This scratch has pending changes, are you sure you want to navigate away?" ,
173+ )
174+ ) {
175+ router . push ( "/new" ) ;
176+ }
177+ } ;
178+
179+ return (
180+ < Link href = "/new" onClick = { handleClick } >
181+ < FileIcon />
182+ New
183+ </ Link >
184+ ) ;
185+ }
186+
152187function Actions ( {
153188 isCompiling,
154189 isDirty,
@@ -167,8 +202,6 @@ function Actions({
167202 const [ isSaving , setIsSaving ] = useState ( false ) ;
168203 const [ isForking , setIsForking ] = useState ( false ) ;
169204
170- const router = useRouter ( ) ;
171-
172205 const canSave = scratch . owner && userIsYou ( scratch . owner ) ;
173206
174207 const platform = api . usePlatform ( scratch . platform ) ;
@@ -192,21 +225,7 @@ function Actions({
192225 return (
193226 < ul className = { styles . actions } aria-label = "Scratch actions" >
194227 < li >
195- < button
196- onClick = { ( ) => {
197- if (
198- ! isDirty ||
199- confirm (
200- "This scratch has pending changes, are you sure you want to navigate away?" ,
201- )
202- ) {
203- router . push ( "/new" ) ;
204- }
205- } }
206- >
207- < FileIcon />
208- New
209- </ button >
228+ < NewScratchButton isDirty = { isDirty } />
210229 </ li >
211230 < li >
212231 < button
0 commit comments