Skip to content
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

Feat CTRL + Y to submit the solution #1419

Merged
merged 5 commits into from
May 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions packages/monaco/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import clsx from 'clsx';
import lzstring from 'lz-string';
import type * as monaco from 'monaco-editor';
import { usePathname, useSearchParams } from 'next/navigation';
import React, { useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { useResetEditor } from './editor-hooks';
import SplitEditor, { TESTS_PATH, USER_CODE_PATH } from './split-editor';
import { useLocalStorage } from './useLocalStorage';
Expand Down Expand Up @@ -71,7 +71,7 @@ export function CodePanel(props: CodePanelProps) {
const [userEditorState, setUserEditorState] = useState<monaco.editor.IStandaloneCodeEditor>();
const [monacoInstance, setMonacoInstance] = useState<typeof monaco>();

const handleSubmit = async () => {
const handleSubmit = useCallback(async () => {
const hasErrors = tsErrors?.some((e) => e.length) ?? false;

try {
Expand All @@ -98,9 +98,23 @@ export function CodePanel(props: CodePanelProps) {
action: <ToastAction altText="Dismiss">Dismiss</ToastAction>,
});
}
};
}, [tsErrors]);
const hasFailingTest = tsErrors?.some((e) => e.length) ?? false;

useEffect(() => {
const onSubmit = (e: KeyboardEvent) => {
if ((e.ctrlKey || e.metaKey) && e.code === 'KeyY') {
e.preventDefault();
handleSubmit();
}
};

document.addEventListener('keydown', onSubmit);

return () => {
document.removeEventListener('keydown', onSubmit);
};
}, [handleSubmit]);
return (
<>
<div className="sticky top-0 flex h-[40px] shrink-0 items-center justify-end gap-4 border-b border-zinc-300 bg-white px-3 py-2 dark:border-zinc-700 dark:bg-[#1e1e1e]">
Expand Down Expand Up @@ -289,10 +303,14 @@ export function CodePanel(props: CodePanelProps) {
{disabled && 'Login to '}Submit{tsErrors === undefined && ' (open test cases)'}
</Button>
</TooltipTrigger>
{disabled && (
{disabled ? (
<TooltipContent>
<p>Login to Submit</p>
</TooltipContent>
) : (
<TooltipContent>
<p>Submit (CTRL + Y)</p>
</TooltipContent>
)}
</Tooltip>
</div>
Expand Down