Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

[#478] feat: reset button (#489)

Helgeb32f4f917434
diff --git a/apps/web/src/app/challenge/_components/reset-editor-button.tsx b/apps/web/src/app/challenge/_components/reset-editor-button.tsx
index 2aeff3f8..bff4762d 100644
--- a/apps/web/src/app/challenge/_components/reset-editor-button.tsx
+++ b/apps/web/src/app/challenge/_components/reset-editor-button.tsx
@@ -1,17 +1,33 @@
-import { Tooltip, TooltipContent, TooltipTrigger } from '@repo/ui';
+import { useResetEditor } from '@repo/monaco/editor-hooks';
+import {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+ AlertDialogTrigger,
+} from '@repo/ui';
import { RotateCcw } from '@repo/ui/icons';
-export function ResetEditorButton() {
+const ResetEditorButton = () => {
+ const { dispatch } = useResetEditor();
return (
- <Tooltip>
- <TooltipTrigger asChild>
- <button className="focus:outline-none focus-visible:ring-2">
- <RotateCcw className="stroke-zinc-500 stroke-1 hover:stroke-zinc-400" size={20} />
- </button>
- </TooltipTrigger>
- <TooltipContent>
- <p>Reset Editor</p>
- </TooltipContent>
- </Tooltip>
+ <AlertDialog>
+ <AlertDialogTrigger>
+ <RotateCcw className="stroke-zinc-500 stroke-1 hover:stroke-zinc-400" size={20} />
+ </AlertDialogTrigger>
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>Reset code?</AlertDialogTitle>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel>Cancel</AlertDialogCancel>
+ <AlertDialogAction onClick={() => dispatch('resetCode')}>Continue</AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
);
-}
+};
+export { ResetEditorButton };
diff --git a/packages/monaco/package.json b/packages/monaco/package.json
index c718d419..7b70f778 100644
--- a/packages/monaco/package.json
+++ b/packages/monaco/package.json
@@ -8,7 +8,8 @@
"./settings-store": "./src/settings-store.ts",
"./vim-mode": "./src/vim-mode.tsx",
"./twoslash": "./src/twoslash.ts",
- "./prettier": "./src/prettier.ts"
+ "./prettier": "./src/prettier.ts",
+ "./editor-hooks": "./src/editor-hooks.ts"
},
"typesVersions": {
"*": {
@@ -29,6 +30,9 @@
],
"prettier": [
"./src/prettier.ts"
+ ],
+ "editor-hooks": [
+ "./src/editor-hooks.ts"
]
}
},
diff --git a/packages/monaco/src/editor-hooks.ts b/packages/monaco/src/editor-hooks.ts
new file mode 100644
index 00000000..240e0174
--- /dev/null
+++ b/packages/monaco/src/editor-hooks.ts
@@ -0,0 +1,29 @@
+import { useEffect } from 'react';
+
+class EditorEvents extends EventTarget {
+ dispatch(ev: string) {
+ this.dispatchEvent(new CustomEvent(ev));
+ }
+}
+
+const event = new EditorEvents();
+type EventNames = 'resetCode';
+export function useResetEditor(): {
+ subscribe: (eventName: EventNames, cb: () => void) => void;
+ dispatch: (eventName: EventNames) => void;
+} {
+ function subscribe(eventName: EventNames, cb: () => void) {
+ useEffect(() => {
+ const fnc = () => {
+ cb();
+ };
+ event.addEventListener(eventName, fnc);
+ return () => event.removeEventListener(eventName, fnc);
+ }, []);
+ }
+
+ function dispatch(eventName: EventNames) {
+ event.dispatch(eventName);
+ }
+ return { subscribe, dispatch };
+}
diff --git a/packages/monaco/src/index.tsx b/packages/monaco/src/index.tsx
index 826036ea..e8617018 100644
--- a/packages/monaco/src/index.tsx
+++ b/packages/monaco/src/index.tsx
@@ -6,13 +6,14 @@ import { Loader2 } from '@repo/ui/icons';
import type * as monaco from 'monaco-editor';
import dynamic from 'next/dynamic';
import { useRouter, useSearchParams } from 'next/navigation';
-import React, { useRef, useState } from 'react';
+import React, { useState } from 'react';
import lzstring from 'lz-string';
import { Button, ToastAction, useToast, Tooltip, TooltipContent, TooltipTrigger } from '@repo/ui';
import { useLocalStorage } from './useLocalStorage';
import SplitEditor, { TESTS_PATH } from './split-editor';
import { createTwoslashInlayProvider } from './twoslash';
import { PrettierFormatProvider } from './prettier';
+import { useResetEditor } from './editor-hooks';
const VimStatusBar = dynamic(() => import('./vim-mode').then((v) => v.VimStatusBar), {
ssr: false,
@@ -60,6 +61,10 @@ export function CodePanel(props: CodePanelProps) {
};
const [code, setCode] = useState(() => getDefaultCode());
+ useResetEditor().subscribe('resetCode', () => {
+ setCode(props.challenge.code);
+ setLocalStorageCode(props.challenge.code);
+ });
const [testEditorState, setTestEditorState] = useState<monaco.editor.IStandaloneCodeEditor>();
const [userEditorState, setUserEditorState] = useState<monaco.editor.IStandaloneCodeEditor>();
diff --git a/packages/ui/package.json b/packages/ui/package.json
index e0736527..ef571d67 100644
--- a/packages/ui/package.json
+++ b/packages/ui/package.json
@@ -28,6 +28,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
+ "@radix-ui/react-alert-dialog": "^1.0.4",
"@radix-ui/react-avatar": "^1.0.3",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.4",
diff --git a/packages/ui/src/components/alert-dialog.tsx b/packages/ui/src/components/alert-dialog.tsx
new file mode 100644
index 00000000..f1b87dc1
--- /dev/null
+++ b/packages/ui/src/components/alert-dialog.tsx
@@ -0,0 +1,121 @@
+'use client';
+
+import * as React from 'react';
+import * as AlertDialogPrimitive from '@radix-ui/react-alert-dialog';
+
+import { cn } from '../cn';
+import { buttonVariants } from './button';
+
+const AlertDialog = AlertDialogPrimitive.Root;
+
+const AlertDialogTrigger = AlertDialogPrimitive.Trigger;
+
+const AlertDialogPortal = ({
+ className,
+ ...props
+}: AlertDialogPrimitive.AlertDialogPortalProps) => (
+ <AlertDialogPrimitive.Portal className={cn(className)} {...props} />
+);
+AlertDialogPortal.displayName = AlertDialogPrimitive.Portal.displayName;
+
+const AlertDialogOverlay = React.forwardRef<
+ React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
+ React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
+>(({ className, children, ...props }, ref) => (
+ <AlertDialogPrimitive.Overlay
+ className={cn(
+ 'bg-background/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 backdrop-blur-sm',
+ className,
+ )}
+ {...props}
+ ref={ref}
+ />
+));
+AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName;
+
+const AlertDialogContent = React.forwardRef<
+ React.ElementRef<typeof AlertDialogPrimitive.Content>,
+ React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
+>(({ className, ...props }, ref) => (
+ <AlertDialogPortal>
+ <AlertDialogOverlay />
+ <AlertDialogPrimitive.Content
+ ref={ref}
+ className={cn(
+ 'bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border p-6 shadow-lg duration-200 sm:rounded-lg md:w-full',
+ className,
+ )}
+ {...props}
+ />
+ </AlertDialogPortal>
+));
+AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName;
+
+const AlertDialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
+ <div className={cn('flex flex-col space-y-2 text-center sm:text-left', className)} {...props} />
+);
+AlertDialogHeader.displayName = 'AlertDialogHeader';
+
+const AlertDialogFooter = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
+ <div
+ className={cn('flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2', className)}
+ {...props}
+ />
+);
+AlertDialogFooter.displayName = 'AlertDialogFooter';
+
+const AlertDialogTitle = React.forwardRef<
+ React.ElementRef<typeof AlertDialogPrimitive.Title>,
+ React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Title>
+>(({ className, ...props }, ref) => (
+ <AlertDialogPrimitive.Title
+ ref={ref}
+ className={cn('text-lg font-semibold', className)}
+ {...props}
+ />
+));
+AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName;
+
+const AlertDialogDescription = React.forwardRef<
+ React.ElementRef<typeof AlertDialogPrimitive.Description>,
+ React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Description>
+>(({ className, ...props }, ref) => (
+ <AlertDialogPrimitive.Description
+ ref={ref}
+ className={cn('text-muted-foreground text-sm', className)}
+ {...props}
+ />
+));
+AlertDialogDescription.displayName = AlertDialogPrimitive.Description.displayName;
+
+const AlertDialogAction = React.forwardRef<
+ React.ElementRef<typeof AlertDialogPrimitive.Action>,
+ React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action>
+>(({ className, ...props }, ref) => (
+ <AlertDialogPrimitive.Action ref={ref} className={cn(buttonVariants(), className)} {...props} />
+));
+AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName;
+
+const AlertDialogCancel = React.forwardRef<
+ React.ElementRef<typeof AlertDialogPrimitive.Cancel>,
+ React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Cancel>
+>(({ className, ...props }, ref) => (
+ <AlertDialogPrimitive.Cancel
+ ref={ref}
+ className={cn(buttonVariants({ variant: 'outline' }), 'mt-2 sm:mt-0', className)}
+ {...props}
+ />
+));
+AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName;
+
+export {
+ AlertDialog,
+ AlertDialogTrigger,
+ AlertDialogContent,
+ AlertDialogHeader,
+ AlertDialogFooter,
+ AlertDialogTitle,
+ AlertDialogDescription,
+ AlertDialogAction,
+ AlertDialogCancel,
+};
diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts
index 301da5af..491f2adf 100644
--- a/packages/ui/src/index.ts
+++ b/packages/ui/src/index.ts
@@ -1,5 +1,6 @@
export * from './components/action-menu';
export * from './components/alert';
+export * from './components/alert-dialog';
export * from './components/avatar';
export * from './components/badge';
export * from './components/button';
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index baf80d8e..bc7ba9aa 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1,9 +1,5 @@
lockfileVersion: '6.0'
-settings:
- autoInstallPeers: true
- excludeLinksFromLockfile: false
-
patchedDependencies:
'@monaco-editor/[email protected]':
hash: xep4qb27qzc6gvlhv2fxe3r2ka
@@ -547,6 +543,9 @@ importers:
packages/ui:
dependencies:
+ '@radix-ui/react-alert-dialog':
+ specifier: ^1.0.4
'@radix-ui/react-avatar':
specifier: ^1.0.3
@@ -664,7 +663,7 @@ importers:
dependencies:
'@next/eslint-plugin-next':
specifier: canary
- version: 13.4.13-canary.13
+ version: 13.4.4-canary.8
'@repo/tsconfig':
specifier: workspace:*
version: link:../config-typescript
@@ -676,7 +675,7 @@ importers:
'@vercel/style-guide':
specifier: ^4.0.2
eslint:
specifier: ^8.40.0
version: 8.45.0
@@ -1728,9 +1727,8 @@ packages:
glob: 7.1.7
dev: true
-
- resolution: {integrity: sha512-lEXi0QLWUuXlMQRLSk/6JUUoOcjACOKbJYPKe0R1XyF5Kf7/Czdm072UJb3betXqiWw5v54GoOOmyDaUDCn5Rw==}
+ resolution: {integrity: sha512-hgFwPEJ5vr66yjRAfYxhKXBxpgmusZF7ztS8GpBS7HsDb05NHiMho843INE+0ToTv+S+2GTGr3YQHgjBSqzE0g==}
dependencies:
glob: 7.1.7
dev: false
@@ -2247,6 +2245,31 @@ packages:
'@babel/runtime': 7.22.6
dev: false
+ resolution: {integrity: sha512-jbfBCRlKYlhbitueOAv7z74PXYeIQmWpKwm3jllsdkw7fGWNkxqP3v0nY9WmOzcPqpQuoorNtvViBgL46n5gVg==}
+ peerDependencies:
+ '@types/react': '*'
+ '@types/react-dom': '*'
+ react: ^16.8 || ^17.0 || ^18.0
+ react-dom: ^16.8 || ^17.0 || ^18.0
+ peerDependenciesMeta:
+ '@types/react':
+ optional: true
+ '@types/react-dom':
+ optional: true
+ dependencies:
+ '@babel/runtime': 7.22.6
+ '@radix-ui/primitive': 1.0.1
+ '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/react-context': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/react-dialog': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-slot': 1.0.2(@types/[email protected])([email protected])
+ '@types/react': 18.2.20
+ react: 18.2.0
+ react-dom: 18.2.0([email protected])
+ dev: false
+
resolution: {integrity: sha512-wSP+pHsB/jQRaL6voubsQ/ZlrGBHHrOjmBnr19hxYgtS0WvAFwZhK2WP/YY5yF9uKECCEEDGxuLxq1NBK51wFA==}
peerDependencies:
@@ -4118,7 +4141,7 @@ packages:
yoga-wasm-web: 0.3.0
dev: false
resolution: {integrity: sha512-FroL+oOePzhw7n/I+f7zr4WNroGHT/+2TlW6WH9+CVSjMNsEyu7Qstj2mI5gWIBjT1Y2ZImKPppCzI2cIYmNZw==}
engines: {node: '>=16'}
peerDependencies:
@@ -4138,14 +4161,14 @@ packages:
dependencies:
'@babel/core': 7.22.10
'@babel/eslint-parser': 7.22.10(@babel/[email protected])([email protected])
- '@next/eslint-plugin-next': 13.4.13-canary.13
+ '@next/eslint-plugin-next': 13.4.4-canary.8
'@rushstack/eslint-patch': 1.3.2
'@typescript-eslint/eslint-plugin': 5.59.1(@typescript-eslint/[email protected])([email protected])([email protected])
'@typescript-eslint/parser': 5.62.0([email protected])([email protected])
eslint: 8.45.0
eslint-config-prettier: 8.10.0([email protected])
eslint-import-resolver-alias: 1.1.2([email protected])
- eslint-import-resolver-typescript: 3.5.5(@typescript-eslint/[email protected])([email protected])([email protected])
+ eslint-import-resolver-typescript: 3.5.5(@typescript-eslint/[email protected])([email protected])([email protected])([email protected])
eslint-plugin-eslint-comments: 3.2.0([email protected])
eslint-plugin-import: 2.27.5(@typescript-eslint/[email protected])([email protected])([email protected])
eslint-plugin-jest: 27.2.3(@typescript-eslint/[email protected])([email protected])([email protected])
@@ -5742,30 +5765,6 @@ packages:
- eslint-import-resolver-webpack
- supports-color
- resolution: {integrity: sha512-TdJqPHs2lW5J9Zpe17DZNQuDnox4xo2o+0tE7Pggain9Rbc19ik8kFtXdxZ250FVx2kF4vlt2RSf4qlUpG7bhw==}
- engines: {node: ^14.18.0 || >=16.0.0}
- peerDependencies:
- eslint: '*'
- eslint-plugin-import: '*'
- dependencies:
- debug: 4.3.4
- enhanced-resolve: 5.15.0
- eslint: 8.45.0
- eslint-module-utils: 2.8.0(@typescript-eslint/[email protected])([email protected])([email protected])
- eslint-plugin-import: 2.27.5(@typescript-eslint/[email protected])([email protected])([email protected])
- get-tsconfig: 4.6.2
- globby: 13.2.2
- is-core-module: 2.12.1
- is-glob: 4.0.3
- synckit: 0.8.5
- transitivePeerDependencies:
- - '@typescript-eslint/parser'
- - eslint-import-resolver-node
- - eslint-import-resolver-webpack
- - supports-color
- dev: false
-
resolution: {integrity: sha512-aWajIYfsqCKRDgUfjEXNN/JlrzauMuSEy5sbd7WXbtW3EH6A6MpwEh42c7qD+MqQo9QMJ6fWLAeIJynx0g6OAw==}
engines: {node: '>=4'}
@@ -5795,35 +5794,6 @@ packages:
transitivePeerDependencies:
- supports-color
- resolution: {integrity: sha512-aWajIYfsqCKRDgUfjEXNN/JlrzauMuSEy5sbd7WXbtW3EH6A6MpwEh42c7qD+MqQo9QMJ6fWLAeIJynx0g6OAw==}
- engines: {node: '>=4'}
- peerDependencies:
- '@typescript-eslint/parser': '*'
- eslint: '*'
- eslint-import-resolver-node: '*'
- eslint-import-resolver-typescript: '*'
- eslint-import-resolver-webpack: '*'
- peerDependenciesMeta:
- '@typescript-eslint/parser':
- optional: true
- eslint:
- optional: true
- eslint-import-resolver-node:
- optional: true
- eslint-import-resolver-typescript:
- optional: true
- eslint-import-resolver-webpack:
- optional: true
- dependencies:
- '@typescript-eslint/parser': 5.62.0([email protected])([email protected])
- debug: 3.2.7
- eslint: 8.45.0
- eslint-import-resolver-typescript: 3.5.5(@typescript-eslint/[email protected])([email protected])([email protected])
- transitivePeerDependencies:
- - supports-color
- dev: false
-
resolution: {integrity: sha512-0jkOl0hfojIHHmEHgmNdqv4fmh7300NdpA9FFpF7zaoLvB/QeXOGNLIo86oAveJFrfB1p05kC8hpEMHM8DwWVQ==}
engines: {node: '>=6.5.0'}
@@ -10474,3 +10444,7 @@ packages:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
dev: false
+
+settings:
+ autoInstallPeers: true
+ excludeLinksFromLockfile: false