-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fe): persist language and code datas (#1720)
* feat(fe): persist language and code datas * fix(fe): change a code store
- Loading branch information
Showing
4 changed files
with
73 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,52 @@ | ||
import type { Language } from '@/types/type' | ||
import { create } from 'zustand' | ||
import { createContext } from 'react' | ||
import { create, createStore } from 'zustand' | ||
import { persist } from 'zustand/middleware' | ||
|
||
interface EditorStore { | ||
code: string | ||
interface LanguageStore { | ||
language: Language | ||
setLanguage: (language: Language) => void | ||
} | ||
|
||
export const useLanguageStore = create( | ||
persist<LanguageStore>( | ||
(set) => ({ | ||
language: 'C', | ||
setLanguage: (language) => { | ||
set({ language }) | ||
} | ||
}), | ||
{ | ||
name: 'language' | ||
} | ||
) | ||
) | ||
interface CodeState { | ||
code: string | ||
setCode: (code: string) => void | ||
clearCode: () => void | ||
} | ||
|
||
const useEditorStore = create<EditorStore>((set) => ({ | ||
code: '', | ||
language: 'C', | ||
setLanguage: (language) => { | ||
localStorage.setItem('programming_lang', language) | ||
set({ language }) | ||
}, | ||
setCode: (code: string) => set({ code }), | ||
clearCode: () => set({ code: '' }) | ||
})) | ||
type CodeStore = ReturnType<typeof createCodeStore> | ||
|
||
export const createCodeStore = ( | ||
language: Language, | ||
problemId: number, | ||
contestId?: number | ||
) => { | ||
const problemKey = `${problemId}${contestId ? `_${contestId}` : ''}_${language}` | ||
return createStore<CodeState>()( | ||
persist<CodeState>( | ||
(set) => ({ | ||
code: '', | ||
setCode: (code) => { | ||
set({ code }) | ||
} | ||
}), | ||
{ | ||
name: problemKey | ||
} | ||
) | ||
) | ||
} | ||
|
||
export default useEditorStore | ||
export const CodeContext = createContext<CodeStore | null>(null) |