Skip to content

Commit

Permalink
fix(fe): change language's storekey to include problemId and contestId (
Browse files Browse the repository at this point in the history
  • Loading branch information
Kohminchae authored Oct 10, 2024
1 parent a1febb4 commit 20453e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/frontend/components/EditorHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function Editor({
contestId,
templateString
}: ProblemEditorProps) {
const { language, setLanguage } = useLanguageStore()
const { language, setLanguage } = useLanguageStore(problem.id, contestId)()
const { code, setCode } = createCodeStore((state) => state)
const testResultStore = useContext(TestResultsContext)
if (!testResultStore) throw new Error('TestResultsContext is not provided')
Expand Down
10 changes: 8 additions & 2 deletions apps/frontend/components/EditorResizablePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function EditorMainResizablePanel({
}: ProblemEditorProps) {
const pathname = usePathname()
const base = contestId ? `/contest/${contestId}` : ''
const { language, setLanguage } = useLanguageStore()
const { language, setLanguage } = useLanguageStore(problem.id, contestId)()
const testResultStore = useContext(TestResultsContext)
if (!testResultStore) throw new Error('TestResultsContext is not provided')
const { testResults } = useStore(testResultStore)
Expand Down Expand Up @@ -123,6 +123,8 @@ export default function EditorMainResizablePanel({
>
<ScrollArea className="h-full bg-[#121728]">
<CodeEditorInEditorResizablePanel
problemId={problem.id}
contestId={contestId}
enableCopyPaste={enableCopyPaste}
/>
<ScrollBar orientation="horizontal" />
Expand All @@ -148,13 +150,17 @@ export default function EditorMainResizablePanel({
}

interface CodeEditorInEditorResizablePanelProps {
problemId: number
contestId?: number
enableCopyPaste: boolean
}

function CodeEditorInEditorResizablePanel({
problemId,
contestId,
enableCopyPaste
}: CodeEditorInEditorResizablePanelProps) {
const { language } = useLanguageStore()
const { language } = useLanguageStore(problemId, contestId)()
const { code, setCode } = createCodeStore()

return (
Expand Down
25 changes: 14 additions & 11 deletions apps/frontend/stores/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ interface LanguageStore {
setLanguage: (language: Language) => void
}

export const useLanguageStore = create(
persist<LanguageStore>(
(set) => ({
language: 'C',
setLanguage: (language) => {
set({ language })
export const useLanguageStore = (problemId: number, contestId?: number) => {
const languageKey = `${problemId}${contestId ? `_${contestId}` : ''}_language`
return create(
persist<LanguageStore>(
(set) => ({
language: 'C',
setLanguage: (language) => {
set({ language })
}
}),
{
name: languageKey
}
}),
{
name: 'language'
}
)
)
)
}
interface CodeState {
code: string
setCode: (code: string) => void
Expand Down

0 comments on commit 20453e9

Please sign in to comment.