// dsh-brain client basic components. import * as React from 'react' import { messageOf } from './api' import { styles } from './styles' const h = React.createElement export function Pill(props) { var tone = styles[props.tone] === undefined ? styles.muted : styles[props.tone] return h( 'span', { style: Object.assign({}, styles.pill, tone) }, h('span', { style: { opacity: 0.75 } }, props.label), h('span', { style: { fontWeight: 600 } }, String(props.value)), ) } export function Banner(props) { var tone = styles[props.tone] === undefined ? styles.muted : styles[props.tone] return h('div', { style: Object.assign({}, styles.banner, tone), role: props.role }, props.children) } export function Field(props) { return h( 'label', { style: styles.field }, h('span', { style: styles.label }, props.label), h('input', { style: styles.input, type: props.type === undefined ? 'text' : props.type, value: props.value, placeholder: props.placeholder === undefined ? '' : props.placeholder, spellCheck: false, autoComplete: 'off', disabled: props.disabled === true, onChange: function (event) { props.onChange(event.target.value) }, }), props.hint === undefined ? null : h('span', { style: styles.hint }, props.hint), ) } export function Btn(props) { var base = props.variant === 'primary' ? styles.buttonPrimary : props.variant === 'danger' ? styles.buttonDanger : props.variant === 'small' ? styles.buttonSmall : styles.button var style = props.disabled === true ? Object.assign({}, base, { opacity: 0.45, cursor: 'not-allowed' }) : base return h( 'button', { type: 'button', style: style, disabled: props.disabled === true, onClick: props.onClick, 'aria-label': props.ariaLabel, }, props.label, ) } export function IconBtn(props) { var style = Object.assign({}, styles.iconBtn, props.danger === true ? styles.iconBtnDanger : null, props.disabled === true ? { opacity: 0.45, cursor: 'not-allowed' } : null) return h( 'button', { type: 'button', style: style, disabled: props.disabled === true, onClick: props.disabled === true ? undefined : props.onClick, 'aria-label': props.ariaLabel, title: props.ariaLabel, }, props.glyph, ) } export function Switch(props) { return h( 'div', { style: styles.tabs, role: 'tablist', 'aria-label': props.ariaLabel }, props.options.map(function (option) { var active = option.value === props.value return h( 'button', { key: option.value, type: 'button', role: 'tab', 'aria-selected': active, style: Object.assign({}, styles.tab, active ? styles.tabActive : null), onClick: function () { props.onChange(option.value) }, }, option.label, ) }), ) } // A real switch, not a checkbox: the tree rows are dense and the state has // to be readable at a glance in both themes. export function Toggle(props) { var on = props.checked === true var disabled = props.disabled === true return h( 'button', { type: 'button', role: 'switch', 'aria-checked': on, 'aria-label': props.label, title: props.label, disabled: disabled, style: { width: '38px', height: '22px', flex: '0 0 auto', padding: '2px', borderRadius: '999px', border: '1px solid ' + (on ? 'rgba(0,0,0,0.25)' : 'rgba(127,127,127,0.45)'), background: on ? '#3f3f46' : 'rgba(127,127,127,0.28)', display: 'flex', alignItems: 'center', justifyContent: on ? 'flex-end' : 'flex-start', cursor: disabled ? 'not-allowed' : 'pointer', opacity: disabled ? 0.45 : 1, transition: 'background 120ms ease', }, onClick: disabled ? undefined : props.onChange, }, h('span', { style: { width: '16px', height: '16px', borderRadius: '999px', background: 'rgba(255,255,255,0.96)', boxShadow: '0 1px 2px rgba(0,0,0,0.25)', flex: '0 0 auto', }, }), ) } export function Section(props) { return h( 'div', { style: styles.section }, h( 'div', { style: styles.sectionHead }, h('h4', { style: styles.sectionTitle }, props.title), props.aside === undefined ? null : h('div', { style: styles.actions }, props.aside), ), props.hint === undefined ? null : h('p', { style: styles.hint }, props.hint), props.children, ) } export function CardBoundary(props) { React.Component.call(this, props) this.state = { failure: null } } CardBoundary.prototype = Object.create(React.Component.prototype) CardBoundary.prototype.constructor = CardBoundary CardBoundary.getDerivedStateFromError = function (failure) { return { failure: failure } } CardBoundary.prototype.render = function () { if (this.state.failure !== null) { return h( 'div', { style: Object.assign({}, styles.root) }, h('div', { style: Object.assign({}, styles.banner, styles.bad) }, '云端记忆设置卡渲染失败:' + messageOf(this.state.failure)), h('p', { style: styles.hint }, '插件本身仍在运行,可改用 /dsh-brain 页面配置。'), ) } return this.props.children }