chore(quality): resolve SonarQube code smells across website
Clears the 124 CODE_SMELL findings from the SonarQube scan (server, client, and bot). All changes are behaviour-preserving refactors — no route, protocol, schema, or config changes — verified against the full server (381) and client (43) test suites plus a clean client build. By rule: - S3776 (20, cognitive complexity): extract helpers/handlers so each function drops under the threshold — shard model upsert builders, page/wiki update, block validation, notification stream mapping (dispatch table), SSO mobile login, shard ingest deps, uo-link socket backfill/connect, the bot slash- command dispatchers + discord manager, and the Shard/UserDetail/HeroEditor/ CharacterStats React components. - S4624 (34, nested template literals): pull inner templates into locals / a withQs() helper; rewrite shardEvents.describe() as a formatter table. - S3358 (35, nested ternaries): lift to if/else vars, lookup maps, small components, or guarded JSX expressions. - S6479 (12, array-index React keys): key by stable content instead of index (two in-editor lists left as-is; index matches their by-index edit model). - S6353 (6): [0-9]/[^0-9] -> \d/\D. S125 (5): reword state-shape comments that parsed as code. S3800/S3782 (botScore): JSDoc-type PATH_WEIGHTS tuples. - S6481 (2): memoize Auth/Site context values (and SiteContext brand). - S4144: dedupe HeroEditor upload handler into useImageUpload(). - S1126 (2), S6035, S5869 (redundant A-Z under /i), S5843 (town-name regex -> prefix list): assorted one-liners. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,34 @@ function tooLargeToUpload(size) {
|
||||
)
|
||||
}
|
||||
|
||||
// Label for an image-upload button: busy, replace-existing, or first upload.
|
||||
function uploadLabel(up, hasSrc) {
|
||||
if (up) return 'Uploading…'
|
||||
return hasSrc ? 'Replace' : 'Upload'
|
||||
}
|
||||
|
||||
// Shared image-upload behaviour for the element panels that point props.src at
|
||||
// the uploaded URL (moon + image). Returns the busy flag and file <input> handler.
|
||||
function useImageUpload(onProps) {
|
||||
const [up, setUp] = useState(false)
|
||||
async function onFile(e) {
|
||||
const f = e.target.files?.[0]
|
||||
e.target.value = ''
|
||||
if (!f) return
|
||||
if (tooLargeToUpload(f.size)) return
|
||||
setUp(true)
|
||||
try {
|
||||
const { url } = await api.admin.upload(f)
|
||||
onProps({ src: url })
|
||||
} catch {
|
||||
/* ignore */
|
||||
} finally {
|
||||
setUp(false)
|
||||
}
|
||||
}
|
||||
return { up, onFile }
|
||||
}
|
||||
|
||||
function newElement(type, z) {
|
||||
const base = { id: genId(), type, x: 50, y: 50, z, anchor: 'center' }
|
||||
if (type === 'text_block') {
|
||||
@@ -53,6 +81,23 @@ function scaleFontSize(v, ratio) {
|
||||
return v
|
||||
}
|
||||
|
||||
// The props patch for a resize drag, per element type: image width is a % of the
|
||||
// canvas, moon size is px, and a text_block resizes its box and scales every
|
||||
// line's font proportionally. `ctx` carries the drag origin + measured geometry.
|
||||
function resizePatch(el, ctx) {
|
||||
const { orig, dxPx, dxLogical, rectWidth, baseWidth, baseLines } = ctx
|
||||
if (el.type === 'image') {
|
||||
return { width: Math.round(clamp(orig + (dxPx / rectWidth) * 100, 5, 100)) } // %
|
||||
}
|
||||
if (el.type === 'moon') {
|
||||
return { size: Math.round(clamp(orig + dxLogical, 24, 400)) } // px
|
||||
}
|
||||
const width = Math.round(clamp(orig + dxLogical, 120, 1180))
|
||||
const ratio = baseWidth ? width / baseWidth : 1
|
||||
const lines = baseLines.map((l) => ({ ...l, fontSize: scaleFontSize(l.fontSize, ratio) }))
|
||||
return { width, lines }
|
||||
}
|
||||
|
||||
export default function HeroEditor() {
|
||||
const [layout, setLayout] = useState(null)
|
||||
const [live, setLive] = useState(null)
|
||||
@@ -203,7 +248,9 @@ export default function HeroEditor() {
|
||||
if (!dim) return
|
||||
const rect = canvasRef.current.getBoundingClientRect()
|
||||
const sx = e.clientX
|
||||
const orig = el.props?.[dim] ?? (dim === 'width' && el.type === 'image' ? 40 : dim === 'width' ? 600 : 64)
|
||||
let defaultDim = 64
|
||||
if (dim === 'width') defaultDim = el.type === 'image' ? 40 : 600
|
||||
const orig = el.props?.[dim] ?? defaultDim
|
||||
// Snapshot the starting width + lines for text blocks so font scaling is always
|
||||
// computed against the drag origin (no rounding drift as the pointer moves).
|
||||
const baseWidth = el.type === 'text_block' ? orig : 0
|
||||
@@ -217,17 +264,7 @@ export default function HeroEditor() {
|
||||
const move = (ev) => {
|
||||
const dxPx = ev.clientX - sx
|
||||
const dxLogical = dxPx / scale // client px → stage px
|
||||
if (el.type === 'image') {
|
||||
updateProps(el.id, { width: Math.round(clamp(orig + (dxPx / rect.width) * 100, 5, 100)) }) // %
|
||||
} else if (el.type === 'moon') {
|
||||
updateProps(el.id, { size: Math.round(clamp(orig + dxLogical, 24, 400)) }) // px
|
||||
} else {
|
||||
// text_block: resize the box and scale every line's font proportionally.
|
||||
const width = Math.round(clamp(orig + dxLogical, 120, 1180))
|
||||
const ratio = baseWidth ? width / baseWidth : 1
|
||||
const lines = baseLines.map((l) => ({ ...l, fontSize: scaleFontSize(l.fontSize, ratio) }))
|
||||
updateProps(el.id, { width, lines })
|
||||
}
|
||||
updateProps(el.id, resizePatch(el, { orig, dxPx, dxLogical, rectWidth: rect.width, baseWidth, baseLines }))
|
||||
}
|
||||
const up = () => {
|
||||
node.removeEventListener('pointermove', move)
|
||||
@@ -534,24 +571,7 @@ const swatch = { width: '100%', height: 38, padding: 2, border: '1px solid var(-
|
||||
|
||||
function MoonPanel({ element, onProps }) {
|
||||
const p = element.props || {}
|
||||
const [up, setUp] = useState(false)
|
||||
// Reuses the shared admin upload endpoint (same as the image/background panels);
|
||||
// a successful upload just points props.src at the returned URL.
|
||||
async function onFile(e) {
|
||||
const f = e.target.files?.[0]
|
||||
e.target.value = ''
|
||||
if (!f) return
|
||||
if (tooLargeToUpload(f.size)) return
|
||||
setUp(true)
|
||||
try {
|
||||
const { url } = await api.admin.upload(f)
|
||||
onProps({ src: url })
|
||||
} catch {
|
||||
/* ignore */
|
||||
} finally {
|
||||
setUp(false)
|
||||
}
|
||||
}
|
||||
const { up, onFile } = useImageUpload(onProps)
|
||||
return (
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
|
||||
<div>
|
||||
@@ -562,7 +582,7 @@ function MoonPanel({ element, onProps }) {
|
||||
<p className="sans dim" style={{ margin: '0 0 8px', fontSize: '0.8rem' }}>Using the default moon from the hero artwork.</p>
|
||||
)}
|
||||
<label className="btn btn-ghost btn-sq" style={{ display: 'inline-block', cursor: 'pointer' }}>
|
||||
{up ? 'Uploading…' : p.src ? 'Replace' : 'Upload'}
|
||||
{uploadLabel(up, !!p.src)}
|
||||
<input type="file" accept="image/*" onChange={onFile} hidden disabled={up} />
|
||||
</label>
|
||||
{p.src && (
|
||||
@@ -613,29 +633,14 @@ function BadgePanel({ element, onProps }) {
|
||||
|
||||
function ImagePanel({ element, onProps }) {
|
||||
const p = element.props || {}
|
||||
const [up, setUp] = useState(false)
|
||||
async function onFile(e) {
|
||||
const f = e.target.files?.[0]
|
||||
e.target.value = ''
|
||||
if (!f) return
|
||||
if (tooLargeToUpload(f.size)) return
|
||||
setUp(true)
|
||||
try {
|
||||
const { url } = await api.admin.upload(f)
|
||||
onProps({ src: url })
|
||||
} catch {
|
||||
/* ignore */
|
||||
} finally {
|
||||
setUp(false)
|
||||
}
|
||||
}
|
||||
const { up, onFile } = useImageUpload(onProps)
|
||||
return (
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
|
||||
<div>
|
||||
<span className="field-label">Image</span>
|
||||
{p.src && <img src={p.src} alt="" style={{ width: '100%', maxHeight: 90, objectFit: 'contain', borderRadius: 6, border: '1px solid var(--line)', marginBottom: 8 }} />}
|
||||
<label className="btn btn-ghost btn-sq" style={{ display: 'inline-block', cursor: 'pointer' }}>
|
||||
{up ? 'Uploading…' : p.src ? 'Replace' : 'Upload'}
|
||||
{uploadLabel(up, !!p.src)}
|
||||
<input type="file" accept="image/*" onChange={onFile} hidden disabled={up} />
|
||||
</label>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user