From cf69ef8c1ccd20a8b24d180505bec51d7317dee4 Mon Sep 17 00:00:00 2001 From: undefined Date: Thu, 16 Mar 2023 11:16:53 +0800 Subject: [PATCH] ui: problemconfig: force set subtaskId --- .../problemconfig/reducer/config.ts | 44 +++++++++++++++++-- .../problemconfig/tree/SubtaskSettings.tsx | 33 ++++++++------ packages/ui-default/package.json | 2 +- 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/packages/ui-default/components/problemconfig/reducer/config.ts b/packages/ui-default/components/problemconfig/reducer/config.ts index 65addaf51..d8c834fbb 100644 --- a/packages/ui-default/components/problemconfig/reducer/config.ts +++ b/packages/ui-default/components/problemconfig/reducer/config.ts @@ -19,7 +19,28 @@ export default function reducer(state = { } as State, action: any = {}): State { switch (action.type) { case 'CONFIG_LOAD_FULFILLED': { - return { ...state, ...yaml.load(action.payload.config) as object, __loaded: true }; + try { + const data = yaml.load(action.payload.config) as any; + if (!validate(data)) { + return { ...state, __valid: false, __errors: validate.errors.map((i) => `${i.instancePath}: ${i.message}`) }; + } + const subtasks = (data as any).subtasks; + for (const subtask of subtasks) { + if (typeof subtask.id !== 'number') { + for (let i = 1; ; i++) { + if (!subtasks.find((s) => s.id === s)) { + subtask.id = i; + break; + } + } + } + } + return { + ...state, ...data as any, __valid: true, __errors: [], __loaded: true, + }; + } catch (e) { + return { ...state, __valid: false, __errors: [e.message] }; + } } case 'CONFIG_FORM_UPDATE': { const next = { ...state, [action.key]: action.value }; @@ -32,13 +53,20 @@ export default function reducer(state = { try { const data = yaml.load(action.payload); if (!validate(data)) { - return { ...state, __valid: false, __errors: validate.errors.map((i) => `${i.instancePath}: ${i.message}`) }; + return { + ...state, + __valid: false, + __errors: validate.errors.map((i) => `${i.instancePath}: ${i.message}`), + __loaded: true, + }; } return { - ...state, ...data as object, __valid: true, __errors: [], + ...state, ...data as object, __valid: true, __errors: [], __loaded: true, }; } catch (e) { - return { ...state, __valid: false, __errors: [e.message] }; + return { + ...state, __valid: false, __errors: [e.message], __loaded: true, + }; } } case 'CONFIG_AUTOCASES_UPDATE': { @@ -49,6 +77,14 @@ export default function reducer(state = { if (subtask.memory === parseMemoryMB(state.memory || '256m')) delete subtask.memory; if (subtask.time) subtask.time += 'ms'; if (subtask.memory) subtask.memory += 'MB'; + if (typeof subtask.id !== 'number') { + for (let i = 1; ; i++) { + if (!subtasks.find((s) => s.id === s)) { + subtask.id = i; + break; + } + } + } } if (subtasks.length === 0) next.subtasks = []; else { diff --git a/packages/ui-default/components/problemconfig/tree/SubtaskSettings.tsx b/packages/ui-default/components/problemconfig/tree/SubtaskSettings.tsx index 8177d2df8..7ab89ba12 100644 --- a/packages/ui-default/components/problemconfig/tree/SubtaskSettings.tsx +++ b/packages/ui-default/components/problemconfig/tree/SubtaskSettings.tsx @@ -1,8 +1,9 @@ import { Button, ControlGroup, Dialog, DialogBody, DialogFooter, - Icon, InputGroup, + Icon, InputGroup, Tag, } from '@blueprintjs/core'; +import { parseMemoryMB, parseTimeMS } from '@hydrooj/utils/lib/common'; import React from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { i18n } from 'vj/utils'; @@ -47,15 +48,17 @@ export function SubtaskSettings(props: SubtaskSettingsProps) { } - onChange={dispatcher(setTime, 'time')} - placeholder={`Inherit (${props.time || '1s'})`} - value={ctime || ''} + rightElement={ms} + onChange={(ev) => setTime(`${ev.currentTarget.value}ms`)} + placeholder={`Inherit (${parseTimeMS(props.time, false) || '1000'})`} + value={ctime ? parseTimeMS(ctime, false).toString() || '' : ''} /> } - onChange={dispatcher(setMemory, 'memory')} - placeholder={`Inherit (${props.memory || '256m'})`} - value={cmemory || ''} + rightElement={MB} + onChange={(ev) => setMemory(`${ev.currentTarget.value}MB`)} + placeholder={`Inherit (${parseMemoryMB(props.memory, false) || '256'})`} + value={cmemory ? parseMemoryMB(cmemory, false).toString() || '' : ''} /> } @@ -112,15 +115,17 @@ export function GlobalSettings() { } - onChange={(ev) => setTime(ev.currentTarget.value)} - placeholder="1s" - value={ctime || ''} + rightElement={ms} + onChange={(ev) => setTime(`${ev.currentTarget.value}ms`)} + placeholder="1000" + value={ctime ? parseTimeMS(ctime, false).toString() || '' : ''} /> } - onChange={(ev) => setMemory(ev.currentTarget.value)} - placeholder="256m" - value={cmemory || ''} + rightElement={MB} + onChange={(ev) => setMemory(`${ev.currentTarget.value}MB`)} + placeholder="256" + value={cmemory ? parseMemoryMB(cmemory, false).toString() || '' : ''} /> @@ -133,7 +138,7 @@ export function GlobalSettings() { {time || '1s'} {' '} - {memory || '256m'} + {memory || '256MB'} ); diff --git a/packages/ui-default/package.json b/packages/ui-default/package.json index b1a745051..0520d8375 100644 --- a/packages/ui-default/package.json +++ b/packages/ui-default/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/ui-default", - "version": "4.48.1", + "version": "4.48.2", "author": "undefined ", "license": "AGPL-3.0", "main": "index.ts",