import $ from 'jquery'; import yaml from 'js-yaml'; import React from 'react'; import { HexColorInput, HexColorPicker } from 'react-colorful'; import { createRoot } from 'react-dom/client'; import { ActionDialog } from 'vj/components/dialog'; import Notification from 'vj/components/notification'; import { NamedPage } from 'vj/misc/Page'; import { i18n, pjax, request, tpl, } from 'vj/utils'; function Balloon({ tdoc, val }) { const [color, setColor] = React.useState(''); const [now, setNow] = React.useState(''); return (
{tdoc.pids.map((pid) => { const { color: c, name } = val[+pid]; return ( {tdoc.pids.indexOf(+pid) === 0 && } ); })}
{i18n('Problem')} {i18n('Color')} {i18n('Name')}
{now === pid ? ({String.fromCharCode(65 + tdoc.pids.indexOf(+pid))}) : ({String.fromCharCode(65 + tdoc.pids.indexOf(+pid))})} { setNow(pid); setColor(c); }} onChange={(e) => { val[+pid].color = e; setColor(e); }} /> { setNow(pid); setColor(c); }} onChange={(e) => { val[+pid].name = e.target.value; }} /> {now && { val[+now].color = e; setColor(e); }} style={{ padding: '1rem' }} />}
); } async function handleSetColor(tdoc) { let val = tdoc.balloon; if (!val) { val = {}; for (const pid of tdoc.pids) val[+pid] = { color: '#ffffff', name: '' }; } Notification.info(i18n('Loading...')); const promise = new ActionDialog({ $body: tpl`

${i18n('Set Color')}

`, }).open(); createRoot($('#balloon').get(0)).render( , ); const action = await promise; if (action !== 'ok') return; Notification.info(i18n('Updating...')); try { await request.post('', { operation: 'set_color', color: yaml.dump(val) }); } catch (e) { Notification.error(`${e.message} ${e.params?.[0]}`); } Notification.info(i18n('Successfully updated.')); pjax.request({ url: '', push: false }); } const page = new NamedPage('contest_balloon', () => { const { tdoc } = UiContext; const beginAt = new Date(tdoc.beginAt).getTime(); const endAt = new Date(tdoc.endAt).getTime(); function update() { const now = Date.now(); if (beginAt <= now && now <= endAt) pjax.request({ url: '', push: false }); } $('[name="set_color"]').on('click', () => handleSetColor(tdoc)); setInterval(update, 60000); $(document).on('click', '[value="done"]', async (ev) => { ev.preventDefault(); const balloon = $(ev.currentTarget).data('balloon'); try { await request.post('', { balloon, operation: 'done' }); } catch (e) { Notification.error(`${e.message} ${e.params?.[0]}`); } Notification.info(i18n('Successfully updated.')); pjax.request({ url: '', push: false }); }); }); export default page;