don't break if a frontend html-event returns a promise
Massimo Melina committed
Mar 2, 2024 at 13:44 UTC
6ca82e1685f28aa68de7038c36ffe4974c888fe1
1 file changed
+4
-5
frontend/src/components.ts
+4
-5
@@ -1,6 +1,6 @@
1
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3
-import { getHFS, hfsEvent, hIcon, prefix } from './misc'
3
+import { getHFS, hfsEvent, hIcon, isPrimitive, onlyTruthy, prefix } from './misc'
4
import { ButtonHTMLAttributes, ChangeEvent, createElement as h, CSSProperties, FC, forwardRef, Fragment,
5
HTMLAttributes, InputHTMLAttributes, isValidElement, MouseEventHandler, ReactNode, SelectHTMLAttributes,
6
useMemo, useState, ComponentPropsWithoutRef } from 'react'
@@ -75,11 +75,10 @@ export function Html({ code, ...rest }: { code:string } & HTMLAttributes<any>) {
75
76
export function CustomCode({ name, props, ifEmpty }: { name: string, props?: any, ifEmpty?: FC }) {
77
const children = useMemo(() => {
78
- const ret = hfsEvent(name, props)
79
- .filter(x => x === 0 || x)
78
+ const ret = onlyTruthy(hfsEvent(name, props)
79
.map((x, key) => isValidElement(x) ? h(Fragment, { key }, x)
81
- : typeof x === 'string' ? h(Html, { key, code: x })
82
- : h('span', { key }, x))
80
+ : x === 0 || x && isPrimitive(x) ? h(Html, { key, code: String(x) })
81
+ : null))
82
const html = getHFS().customHtml?.[name]
83
if (html?.trim?.())
84
ret.push(h(Html, { key: 'x', code: html }))