index.js 1.27 KB
import { storeWarnList } from "~/packageIndex/constant/performance.js";

const DEFAULT_WARN_STYLE = "color: #FA8C16; background: rgba(250, 140, 22, 0.12);";

function keyBy(list, key) {
	return (Array.isArray(list) ? list : []).reduce((result, item) => {
		const targetKey = item && item[key];

		if (targetKey === undefined || targetKey === null || targetKey === "") {
			return result;
		}

		result[targetKey] = item;
		return result;
	}, {});
}

const storeWarnMap = keyBy(storeWarnList, "key");

Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		list: {
			type: Array,
			value: [],
			observer: function (list) {
				this.updateWarnItems(list);
			},
		},
	},

	lifetimes: {
		attached() {
			this.updateWarnItems(this.data.list);
		},
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		warnItems: [],
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		updateWarnItems(list) {
			const warnItems = (Array.isArray(list) ? list : [])
				.map((warn) => {
					const warnKey = String(warn);
					const warnConfig = storeWarnMap[warnKey] || {};

					return {
						key: warnKey,
						label: warnConfig.label || warnKey,
						style: warnConfig.style || DEFAULT_WARN_STYLE,
					};
				})
				.filter((item) => item.label);

			this.setData({
				warnItems,
			});
		},
	},
});