From 15c5cd5f6e8e859605a5fe32bf8a03777726d80f Mon Sep 17 00:00:00 2001 From: catlog22 Date: Thu, 11 Dec 2025 21:33:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor(notifications):=20=E4=BC=98=E5=8C=96JS?= =?UTF-8?q?ON=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=8F=AF=E8=AF=BB=E6=80=A7=E5=92=8C=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard-js/components/notifications.js | 77 +++++++++++++++---- 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/ccw/src/templates/dashboard-js/components/notifications.js b/ccw/src/templates/dashboard-js/components/notifications.js index c412b7a5..83eae9b5 100644 --- a/ccw/src/templates/dashboard-js/components/notifications.js +++ b/ccw/src/templates/dashboard-js/components/notifications.js @@ -5,37 +5,80 @@ /** * Format JSON object for display in notifications - * @param {Object} obj - Object to format - * @param {number} maxLen - Max string length - * @returns {string} Formatted string + * Parses JSON strings and formats objects into readable key-value pairs + * @param {Object|string} obj - Object or JSON string to format + * @param {number} maxLen - Max string length (unused, kept for compatibility) + * @returns {string} Formatted string with key: value pairs */ function formatJsonDetails(obj, maxLen = 150) { - if (!obj || typeof obj !== 'object') return String(obj); + // Handle null/undefined + if (obj === null || obj === undefined) return ''; - // Try pretty format first - try { - const formatted = JSON.stringify(obj, null, 2); - if (formatted.length <= maxLen) { - return formatted; + // If it is a string, try to parse as JSON + if (typeof obj === 'string') { + // Check if it looks like JSON + const trimmed = obj.trim(); + if ((trimmed.startsWith('{') && trimmed.endsWith('}')) || + (trimmed.startsWith('[') && trimmed.endsWith(']'))) { + try { + obj = JSON.parse(trimmed); + } catch (e) { + // Not valid JSON, return as-is + return obj; + } + } else { + // Plain string, return as-is + return obj; } + } - // For longer content, show key-value pairs on separate lines + // Handle non-objects (numbers, booleans, etc.) + if (typeof obj !== 'object') return String(obj); + + // Handle arrays + if (Array.isArray(obj)) { + if (obj.length === 0) return '(empty array)'; + return obj.slice(0, 5).map((item, i) => { + const itemStr = typeof item === 'object' ? JSON.stringify(item) : String(item); + return `[${i}] ${itemStr.length > 50 ? itemStr.substring(0, 47) + '...' : itemStr}`; + }).join('\n') + (obj.length > 5 ? `\n... +${obj.length - 5} more` : ''); + } + + // Handle objects - format as readable key: value pairs + try { const entries = Object.entries(obj); - if (entries.length === 0) return '{}'; + if (entries.length === 0) return '(empty object)'; - const lines = entries.slice(0, 5).map(([key, val]) => { - let valStr = typeof val === 'object' ? JSON.stringify(val) : String(val); - if (valStr.length > 50) valStr = valStr.substring(0, 47) + '...'; + // Format each entry with proper value display + const lines = entries.slice(0, 8).map(([key, val]) => { + let valStr; + if (val === null) { + valStr = 'null'; + } else if (val === undefined) { + valStr = 'undefined'; + } else if (typeof val === 'boolean') { + valStr = val ? 'true' : 'false'; + } else if (typeof val === 'number') { + valStr = String(val); + } else if (typeof val === 'object') { + valStr = JSON.stringify(val); + if (valStr.length > 40) valStr = valStr.substring(0, 37) + '...'; + } else { + valStr = String(val); + if (valStr.length > 50) valStr = valStr.substring(0, 47) + '...'; + } return `${key}: ${valStr}`; }); - if (entries.length > 5) { - lines.push(`... +${entries.length - 5} more`); + if (entries.length > 8) { + lines.push(`... +${entries.length - 8} more fields`); } return lines.join('\n'); } catch (e) { - return JSON.stringify(obj).substring(0, maxLen) + '...'; + // Fallback to stringified version + const str = JSON.stringify(obj); + return str.length > 200 ? str.substring(0, 197) + '...' : str; } }