fix no fields

This commit is contained in:
Robonau 2024-11-11 20:46:46 +00:00 committed by shamoon
parent 15d44acf93
commit 1fe9723959
2 changed files with 23 additions and 8 deletions

View File

@ -4,10 +4,27 @@ import Container from "components/services/widget/container";
import Block from "components/services/widget/block"; import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api"; import useWidgetAPI from "utils/proxy/use-widget-api";
/**
* @param {string[]|null} Fields
* @returns {string[]}
*/
function makeFields(Fields = []) {
let fields = Fields ?? [];
if (fields.length === 0) {
fields = ["download", "nondownload", "read", "unread"];
}
if (fields.length > 4) {
fields.length = 4;
}
fields = fields.map((f) => f.toLowerCase());
return fields;
}
export default function Component({ service }) { export default function Component({ service }) {
const { t } = useTranslation(); const { t } = useTranslation();
/** @type {{widget: { fields: string[] }}} */ /** @type {{widget: { fields: string[]|null }}} */
const { widget } = service; const { widget } = service;
/** @type { { data: { label: string, count: number }[], error: unk }} */ /** @type { { data: { label: string, count: number }[], error: unk }} */
@ -18,9 +35,7 @@ export default function Component({ service }) {
} }
if (!suwayomiData) { if (!suwayomiData) {
if (widget.fields.length > 4) { widget.fields = makeFields(widget.fields);
widget.fields.length = 4;
}
return ( return (
<Container service={service}> <Container service={service}>
{widget.fields.map((Field) => { {widget.fields.map((Field) => {

View File

@ -173,11 +173,11 @@ function extractCounts(responseJSON, fields) {
} }
/** /**
* @param {string[]} Fields * @param {string[]|null} Fields
* @returns {string[]} * @returns {string[]}
*/ */
function makeFields(Fields) { function makeFields(Fields = []) {
let fields = Fields; let fields = Fields ?? [];
if (fields.length === 0) { if (fields.length === 0) {
fields = ["download", "nondownload", "read", "unread"]; fields = ["download", "nondownload", "read", "unread"];
} }
@ -193,7 +193,7 @@ function makeFields(Fields) {
* @typedef {object} widget * @typedef {object} widget
* @property {string} username * @property {string} username
* @property {string} password * @property {string} password
* @property {string[]} fields * @property {string[]|null} fields
* @property {string|number|undefined} category * @property {string|number|undefined} category
* @property {keyof typeof widgets} type * @property {keyof typeof widgets} type
*/ */