Skip to content

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

adds basic indicators for code submission (#38)

bautistaaa6dc8567124e2
diff --git a/src/components/challenge/description-panel.tsx b/src/components/challenge/description-panel.tsx
index 4a1c1975..2887e147 100644
--- a/src/components/challenge/description-panel.tsx
+++ b/src/components/challenge/description-panel.tsx
@@ -63,12 +63,8 @@ export function DescriptionPanel({ challenge }: Props) {
<div className="flex-1 overflow-y-auto rounded-md bg-white dark:bg-zinc-800">
<Tabs defaultValue="description" className="w-full">
<TabsList className="grid w-full grid-cols-2">
- <TabsTrigger value="description" className="hover:bg-zinc-200 dark:hover:bg-zinc-700">
- Description
- </TabsTrigger>
- <TabsTrigger value="solutions" className="hover:bg-zinc-200 dark:hover:bg-zinc-700">
- Solutions
- </TabsTrigger>
+ <TabsTrigger value="description">Description</TabsTrigger>
+ <TabsTrigger value="solutions">Solutions</TabsTrigger>
</TabsList>
<TabsContent value="description" className="mt-0">
<div className="h-full p-5">
diff --git a/src/components/challenge/editor/index.tsx b/src/components/challenge/editor/index.tsx
index 2d98e507..7ac1038c 100644
--- a/src/components/challenge/editor/index.tsx
+++ b/src/components/challenge/editor/index.tsx
@@ -3,7 +3,7 @@ import Editor from '@monaco-editor/react';
import { Settings } from 'lucide-react';
import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import { useTheme } from 'next-themes';
-import { useMemo } from 'react';
+import { useMemo, useState } from 'react';
import {
Dialog,
DialogContent,
@@ -15,6 +15,10 @@ import { useEditorSettingsStore } from '../settings-store';
import { createTwoslashInlayProvider } from './twoslash';
import { VimStatusBar, loadVim } from './vimMode';
import { SettingsForm } from '../settings-form';
+import { Button } from '~/components/ui/button';
+import clsx from 'clsx';
+import { useToast } from '~/components/ui/use-toast';
+import { ToastAction } from '~/components/ui/toast';
declare global {
// eslint-disable-next-line no-var
@@ -83,7 +87,8 @@ declare function Extends<A, B>(...args: A extends B ? [] : [msg: [A, "doesn't ex
const libUri = 'ts:filename/checking.d.ts';
const onMount =
- (value: string) => async (editor: monaco.editor.IStandaloneCodeEditor, monaco: Monaco) => {
+ (value: string, onError: (v: boolean) => void) =>
+ async (editor: monaco.editor.IStandaloneCodeEditor, monaco: Monaco) => {
const numLines = value.split('\n').length;
const lastLineLength = value.split('\n').at(-1)?.length || 1;
@@ -114,7 +119,9 @@ const onMount =
ts.getCompilerOptionsDiagnostics(filename),
] as const);
- console.log(errors);
+ const hasErrors = errors.some((e) => e.length);
+
+ onError(hasErrors);
};
let fixingStart = false;
@@ -135,6 +142,8 @@ const onMount =
}
});
+ editor.getModel()?.onDidChangeContent(() => _runCommand());
+
monaco.languages.registerInlayHintsProvider(
'typescript',
createTwoslashInlayProvider(monaco, ts),
@@ -145,9 +154,12 @@ interface Props {
prompt: string;
}
export const CodePanel = ({ prompt }: Props) => {
+ const { toast } = useToast();
const { theme } = useTheme();
const { settings } = useEditorSettingsStore();
+ const [hasErrors, setHasErrors] = useState(false);
+ const [code, setCode] = useState(prompt);
const editorTheme = theme === 'light' ? 'vs' : 'vs-dark';
const editorOptions = useMemo(() => {
const options = {
@@ -161,35 +173,61 @@ export const CodePanel = ({ prompt }: Props) => {
};
}, [settings]);
+ const handleSubmit = () => {
+ if (hasErrors) {
+ toast({
+ variant: 'destructive',
+ title: 'Uh oh! You still have errors.',
+ action: <ToastAction altText="Try again">Try again</ToastAction>,
+ });
+ } else {
+ toast({
+ variant: 'success',
+ title: 'Good job!',
+ description: 'You completed this challenge.',
+ action: <ToastAction altText="Try again">Dismiss</ToastAction>,
+ });
+ }
+ };
+
return (
- <div className="flex h-full flex-1 flex-col">
- <div className="container flex h-[40px] items-center space-y-2 bg-muted">
- <div className="ml-auto flex w-full space-x-2 sm:justify-end">
- <Dialog>
- <DialogTrigger>
- <Settings size={20} className="stroke-gray-500 hover:stroke-gray-400" />
- </DialogTrigger>
- <DialogContent className="w-[200px]">
- <DialogHeader>
- <DialogTitle>Settings</DialogTitle>
- <div className="py-4">
- <SettingsForm />
- </div>
- </DialogHeader>
- </DialogContent>
- </Dialog>
- </div>
+ <div className="flex flex-1 flex-col overflow-hidden rounded-md">
+ <div className="container flex h-[40px] flex-row-reverse items-center space-y-2 border border-b-zinc-300 bg-muted dark:border-b-zinc-700">
+ <Dialog>
+ <DialogTrigger>
+ <Settings size={20} className="stroke-gray-500 hover:stroke-gray-400" />
+ </DialogTrigger>
+ <DialogContent className="w-[200px]">
+ <DialogHeader>
+ <DialogTitle>Settings</DialogTitle>
+ <div className="py-4">
+ <SettingsForm />
+ </div>
+ </DialogHeader>
+ </DialogContent>
+ </Dialog>
+ </div>
+ <div className="flex-1">
+ <Editor
+ theme={editorTheme}
+ options={editorOptions}
+ defaultLanguage="typescript"
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ onMount={onMount(prompt, setHasErrors)}
+ value={code}
+ onChange={(code) => setCode(code ?? '')}
+ />
+ </div>
+ <div className="flex items-center justify-between bg-muted p-2">
+ <VimStatusBar />
+ <Button
+ size="sm"
+ className="bg-green-300 hover:bg-green-400 dark:hover:bg-green-200"
+ onClick={handleSubmit}
+ >
+ Submit
+ </Button>
</div>
- <Editor
- theme={editorTheme}
- options={editorOptions}
- defaultLanguage="typescript"
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
- onMount={onMount(prompt)}
- defaultValue={prompt}
- className="flex-1"
- />
- <VimStatusBar />
</div>
);
};
diff --git a/src/components/challenge/editor/vimMode.tsx b/src/components/challenge/editor/vimMode.tsx
index 039917f7..f61851b5 100644
--- a/src/components/challenge/editor/vimMode.tsx
+++ b/src/components/challenge/editor/vimMode.tsx
@@ -12,7 +12,7 @@ export const VimStatusBar = () => {
deactivateVimMode();
}
return (
- <div className="flex">
+ <div className="flex w-full">
<div id={STATUS_BAR_ID} className="font-mono" />
</div>
);
diff --git a/src/components/challenge/settings-form.tsx b/src/components/challenge/settings-form.tsx
index c092779d..a24ca2f4 100644
--- a/src/components/challenge/settings-form.tsx
+++ b/src/components/challenge/settings-form.tsx
@@ -21,8 +21,8 @@ import {
SelectTrigger,
SelectValue,
} from '~/components/ui/select';
-import { toast } from '~/components/ui/use-toast';
import { useEditorSettingsStore } from './settings-store';
+import { useToast } from '../ui/use-toast';
const formSchema = z.object({
fontSize: z.string(),
@@ -38,6 +38,8 @@ export const DEFAULT_SETTINGS = {
tabSize: '2',
};
export function SettingsForm() {
+ const { toast } = useToast();
+
const { settings, updateSettings } = useEditorSettingsStore();
const form = useForm<FormSchema>({
resolver: zodResolver(formSchema),
diff --git a/src/components/challenge/share-form.tsx b/src/components/challenge/share-form.tsx
index 1abef49a..045f9962 100644
--- a/src/components/challenge/share-form.tsx
+++ b/src/components/challenge/share-form.tsx
@@ -13,7 +13,7 @@ export function ShareForm() {
return (
<div>
<p>Copy this challenge url so to share with your friends!</p>
- <Button className="block ml-auto mt-4" onClick={copyToClipboard}>
+ <Button className="ml-auto mt-4 block" onClick={copyToClipboard}>
Copy to Clipboard
</Button>
</div>
diff --git a/src/components/challenge/skeleton.tsx b/src/components/challenge/skeleton.tsx
index 82500594..3a2dc77e 100644
--- a/src/components/challenge/skeleton.tsx
+++ b/src/components/challenge/skeleton.tsx
@@ -1,6 +1,6 @@
import { DescriptionPanelSkeleton } from './description-panel-skeleton';
-export async function ChallengeSkeleton() {
+export function ChallengeSkeleton() {
return (
<div className="flex h-full gap-2 p-4">
<DescriptionPanelSkeleton />
diff --git a/src/components/ui/toast.tsx b/src/components/ui/toast.tsx
index 8e1a7469..d4c50540 100644
--- a/src/components/ui/toast.tsx
+++ b/src/components/ui/toast.tsx
@@ -30,6 +30,7 @@ const toastVariants = cva(
default: 'border bg-background',
destructive:
'destructive group border-destructive bg-destructive text-destructive-foreground',
+ success: 'bg-green-700 group border-destructive text-destructive-foreground',
},
},
defaultVariants: {