Save code to local storage
This commit is contained in:
@@ -3,6 +3,7 @@ import Editor from '@monaco-editor/react';
|
|||||||
import { upload } from '../../../assets/icons/input';
|
import { upload } from '../../../assets/icons/input';
|
||||||
import { cn } from '../../../lib/cn';
|
import { cn } from '../../../lib/cn';
|
||||||
import { DropDownList } from '../../../components/input/DropDownList';
|
import { DropDownList } from '../../../components/input/DropDownList';
|
||||||
|
import { useParams } from 'react-router-dom';
|
||||||
|
|
||||||
const languageMap: Record<string, string> = {
|
const languageMap: Record<string, string> = {
|
||||||
c: 'cpp',
|
c: 'cpp',
|
||||||
@@ -23,19 +24,21 @@ const CodeEditor: React.FC<CodeEditorProps> = ({
|
|||||||
onChange,
|
onChange,
|
||||||
onChangeLanguage,
|
onChangeLanguage,
|
||||||
}) => {
|
}) => {
|
||||||
const [language, setLanguage] = useState<string>('C++');
|
const { missionId } = useParams<{ missionId: string }>();
|
||||||
|
|
||||||
const [code, setCode] = useState<string>('');
|
const [code, setCode] = useState<string>('');
|
||||||
const [isDragging, setIsDragging] = useState<boolean>(false);
|
const [isDragging, setIsDragging] = useState<boolean>(false);
|
||||||
|
|
||||||
const items = [
|
const items = [
|
||||||
{ value: 'c', text: 'C' },
|
|
||||||
{ value: 'C++', text: 'C++' },
|
{ value: 'C++', text: 'C++' },
|
||||||
{ value: 'java', text: 'Java' },
|
{ value: 'java', text: 'Java' },
|
||||||
{ value: 'python', text: 'Python' },
|
{ value: 'python', text: 'Python' },
|
||||||
{ value: 'pascal', text: 'Pascal' },
|
|
||||||
{ value: 'kotlin', text: 'Kotlin' },
|
{ value: 'kotlin', text: 'Kotlin' },
|
||||||
{ value: 'csharp', text: 'C#' },
|
{ value: 'csharp', text: 'C#' },
|
||||||
];
|
];
|
||||||
|
const [language, setLanguage] = useState<string>('C++');
|
||||||
|
|
||||||
|
const getCodeKey = (missionId?: string) => `mission_${missionId}_code`;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
onChange(code);
|
onChange(code);
|
||||||
@@ -44,6 +47,24 @@ const CodeEditor: React.FC<CodeEditorProps> = ({
|
|||||||
onChangeLanguage(language);
|
onChangeLanguage(language);
|
||||||
}, [language]);
|
}, [language]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!missionId || !code) return;
|
||||||
|
|
||||||
|
localStorage.setItem(getCodeKey(missionId), code);
|
||||||
|
}, [code, missionId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!missionId) return;
|
||||||
|
|
||||||
|
const savedCode = localStorage.getItem(getCodeKey(missionId));
|
||||||
|
|
||||||
|
if (savedCode !== null) {
|
||||||
|
setCode(savedCode);
|
||||||
|
} else {
|
||||||
|
setCode('');
|
||||||
|
}
|
||||||
|
}, [missionId]);
|
||||||
|
|
||||||
const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const file = e.target.files?.[0];
|
const file = e.target.files?.[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
@@ -95,7 +116,6 @@ const CodeEditor: React.FC<CodeEditorProps> = ({
|
|||||||
onChange={(v) => {
|
onChange={(v) => {
|
||||||
setLanguage(v);
|
setLanguage(v);
|
||||||
}}
|
}}
|
||||||
defaultState={{ value: 'C++', text: 'C++' }}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<label
|
<label
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { CopyIcon } from '../../../assets/icons/missions';
|
|||||||
// import FullLatexRenderer from "./FullLatexRenderer";
|
// import FullLatexRenderer from "./FullLatexRenderer";
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
import { toastSuccess } from '../../../lib/toastNotification';
|
||||||
|
|
||||||
interface CopyableDivPropd {
|
interface CopyableDivPropd {
|
||||||
content: string;
|
content: string;
|
||||||
@@ -16,7 +17,7 @@ const CopyableDiv: FC<CopyableDivPropd> = ({ content }) => {
|
|||||||
const handleCopy = async () => {
|
const handleCopy = async () => {
|
||||||
try {
|
try {
|
||||||
await navigator.clipboard.writeText(content);
|
await navigator.clipboard.writeText(content);
|
||||||
alert('Скопировано!');
|
toastSuccess('Скопировано!');
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user