@@ -2,17 +2,19 @@ import {
22 ButtonGroup ,
33 HStack ,
44 IconButton ,
5+ Select ,
56 Text ,
67 Tooltip ,
78} from "@chakra-ui/react" ;
9+ import { useEffect , useState } from "react" ;
810import {
911 TbDownload ,
1012 TbHelp ,
1113 TbRepeat ,
1214 TbTrash ,
1315 TbUpload ,
1416} from "react-icons/tb" ;
15- import { baseUrl } from "../../api/ToolboxAPI" ;
17+ import { baseUrl , fetchExampleProblems } from "../../api/ToolboxAPI" ;
1618import { chooseFile } from "./FileInput" ;
1719
1820export interface EditorControlsProps {
@@ -40,6 +42,11 @@ export interface EditorControlsProps {
4042 * If omitted, the documentation link will point to the API docs.
4143 */
4244 documentationLink ?: string ;
45+
46+ /**
47+ * Problem type id.
48+ */
49+ problemTypeId : string ;
4350}
4451
4552/**
@@ -75,15 +82,40 @@ const upload = async (onUpload: (uploadContent: string) => void) => {
7582 * messages, an upload, a download and a help button.
7683 */
7784export const EditorControls = ( props : EditorControlsProps ) => {
78- const documentationLink = props . documentationLink || baseUrl ( ) ;
85+ const [ examples , setExamples ] = useState < string [ ] > ( [ ] ) ;
86+
87+ const documentationLink = props . documentationLink ?? baseUrl ( ) ;
88+
89+ useEffect ( ( ) => {
90+ fetchExampleProblems ( props . problemTypeId ) . then ( ( json ) => setExamples ( json ) ) ;
91+ } , [ props . problemTypeId ] ) ;
7992
8093 return (
8194 < HStack justifyContent = { "space-between" } width = "100%" >
82- { props . errorText ? (
83- < Text textColor = "tomato" > { props . errorText } </ Text >
84- ) : (
85- < Text as = "i" > { props . idleText } </ Text >
86- ) }
95+ < HStack >
96+ { examples . length > 0 && (
97+ < Select
98+ placeholder = "Load example"
99+ overflow = "hidden"
100+ textOverflow = "ellipsis"
101+ width = "10rem"
102+ onChange = { ( e ) => props . setEditorContent ( e . target . value ) }
103+ >
104+ { examples . map ( ( example ) => (
105+ < option key = { example } value = { example } >
106+ { example . length > 100 ? example . slice ( 0 , 100 ) + "..." : example }
107+ </ option >
108+ ) ) }
109+ </ Select >
110+ ) }
111+
112+ { props . errorText ? (
113+ < Text textColor = "tomato" > { props . errorText } </ Text >
114+ ) : (
115+ < Text as = "i" > { props . idleText } </ Text >
116+ ) }
117+ </ HStack >
118+
87119 < ButtonGroup isAttached variant = "outline" colorScheme = "teal" >
88120 < Tooltip label = "Download problem from editor" >
89121 < IconButton
0 commit comments