Skip to content

Commit 3ddca27

Browse files
authored
Merge pull request #219 from mp-access/small-fixes-and-improvements
Small fixes and improvements
2 parents 64e0af2 + 3aedd70 commit 3ddca27

4 files changed

Lines changed: 54 additions & 12 deletions

File tree

‎src/components/Hooks.tsx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMonaco } from "@monaco-editor/react"
22
import { useMutation, useQuery, UseQueryOptions } from "@tanstack/react-query"
33
import axios, { AxiosError } from "axios"
4+
import { useMotionValue } from "framer-motion"
45
import { compact, concat, flatten } from "lodash"
56
import * as monaco from "monaco-editor"
67
import {
@@ -523,3 +524,38 @@ export const useLocalStorage = <T,>(
523524

524525
return [value, setValue]
525526
}
527+
528+
export const usePersistentMotionValue = (
529+
key: string,
530+
fallback: number,
531+
delay = 1000,
532+
) => {
533+
const value = useMotionValue(fallback)
534+
535+
useEffect(() => {
536+
const saved = localStorage.getItem(key)
537+
if (saved !== null) {
538+
const parsed = parseFloat(saved)
539+
if (!isNaN(parsed)) value.set(parsed)
540+
}
541+
}, [key, value])
542+
543+
useEffect(() => {
544+
// debounce for local storage
545+
let timeout: NodeJS.Timeout | null = null
546+
547+
const unsubscribe = value.on("change", (latest) => {
548+
if (timeout) clearTimeout(timeout)
549+
timeout = setTimeout(() => {
550+
localStorage.setItem(key, String(latest))
551+
}, delay)
552+
})
553+
554+
return () => {
555+
unsubscribe()
556+
if (timeout) clearTimeout(timeout)
557+
}
558+
}, [key, value, delay])
559+
560+
return value
561+
}

‎src/components/Panels.tsx‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
AnimatePresence,
2828
AnimatePresenceProps,
2929
motion,
30-
useMotionValue,
3130
useTransform,
3231
} from "framer-motion"
3332
import "katex/dist/katex.min.css"
@@ -49,6 +48,7 @@ import { atomOneLight } from "react-syntax-highlighter/dist/cjs/styles/hljs"
4948
import rehypeKatex from "rehype-katex"
5049
import remarkGfm from "remark-gfm"
5150
import RemarkMathPlugin from "remark-math"
51+
import { usePersistentMotionValue } from "./Hooks"
5252

5353
const MotionBox = motion(Box)
5454
const swap = (to: number) => (r: number) => ({
@@ -63,9 +63,8 @@ type TaskViewProps = {
6363

6464
export const TaskView = ({ children }: TaskViewProps) => {
6565
const size = useWindowSize()
66-
const x = useMotionValue(700)
66+
const x = usePersistentMotionValue("TaskView-sidebar-width", size[0] * 0.3)
6767
const width = useTransform(x, (value) => size[0] - 224 - value)
68-
6968
return (
7069
<Flex
7170
flex={1}
@@ -115,8 +114,7 @@ type TaskIOProps = {
115114

116115
export const TaskIO = ({ children }: TaskIOProps) => {
117116
const size = useWindowSize()
118-
const maxHeight = useMotionValue(size[1])
119-
const y = useTransform(maxHeight, (value) => value * 0.7)
117+
const y = usePersistentMotionValue("TaskIO-height", size[0] * 0.3)
120118

121119
return (
122120
<>
@@ -130,7 +128,11 @@ export const TaskIO = ({ children }: TaskIOProps) => {
130128
}}
131129
>
132130
<motion.div
133-
style={{ height: y, display: "flex", position: "relative" }}
131+
style={{
132+
height: y,
133+
display: "flex",
134+
position: "relative",
135+
}}
134136
>
135137
{children[1]}
136138
{children[2]}
@@ -144,7 +146,7 @@ export const TaskIO = ({ children }: TaskIOProps) => {
144146
bg="purple.600"
145147
w="full"
146148
h={2}
147-
dragConstraints={{ top: 200, bottom: size[1] - 150 }}
149+
dragConstraints={{ top: 100, bottom: size[1] - 150 }}
148150
style={{ y }}
149151
cursor="row-resize"
150152
key={JSON.stringify(size)}
@@ -237,8 +239,10 @@ const MarkdownOrderedList = ({ children }: OrderedListProps) => (
237239
const MarkdownBlock = ({ children }: { children: React.ReactNode }) => (
238240
<Stack bg="blackAlpha.100" p={2} m={2} children={children} />
239241
)
240-
const MarkdownCode = ({ inline, children, className }: CodeProps) =>
241-
inline ? (
242+
const MarkdownCode = ({ inline, children, className }: CodeProps) => {
243+
const language = className?.replace("language-", "") || ""
244+
245+
return inline ? (
242246
<Code
243247
colorScheme="gray"
244248
bg="gray.175"
@@ -249,11 +253,12 @@ const MarkdownCode = ({ inline, children, className }: CodeProps) =>
249253
<SyntaxHighlighter
250254
children={String(children).replace(/\n$/, "")}
251255
style={atomOneLight}
252-
language={className}
256+
language={language}
253257
wrapLines
254258
customStyle={{ fontSize: "85%" }}
255259
/>
256260
)
261+
}
257262
const MarkdownListItem = ({ checked, children }: LiProps) => (
258263
<ListItem pb={1} display="flex">
259264
<ListIcon as={checked === null ? RxDotFilled : Checkbox} mt={1} />

‎src/pages/PrivateDashboard.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ export function PrivateDashboard() {
10961096
</Tabs>
10971097
</Flex>
10981098
<Flex gap={2} flexDirection={"column"} minWidth={0} flex={2}>
1099-
<Flex direction={"column"} flex={1}>
1099+
<Flex direction={"column"} flex={1} overflow={"auto"}>
11001100
{!submissions || submissions.length < 1 ? (
11011101
<TaskDescription
11021102
instructionContent={instructionsContent}
@@ -1132,6 +1132,7 @@ export function PrivateDashboard() {
11321132
alignContent={"space-between"}
11331133
p={2}
11341134
overflow={"auto"}
1135+
minH={"70px"}
11351136
>
11361137
<GeneralInformation
11371138
exampleState={exampleState}

‎src/pages/Task.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ import { TaskController } from "./Supervisor"
8484
export default function Task({ type }: { type: "task" | "example" }) {
8585
const { i18n, t } = useTranslation()
8686
const currentLanguage = i18n.language
87-
const editor = useCodeEditor({ disablePasting: type === "example" })
87+
const editor = useCodeEditor({ disablePasting: false })
8888
const navigate = useNavigate()
8989
const toast = useToast()
9090
const { courseSlug } = useParams()

0 commit comments

Comments
 (0)