feat: auto-format JSON display everywhere

- Shared prettyJson/truncate/isJson utility in src/utils/format.ts
- MessageStream: collapsed preview + expanded view both auto-format JSON
- LogPanel: log details with JSON content auto-formatted
- PresetList: payload preview auto-formatted
- Deduplicated isJson/formatPayload logic
This commit is contained in:
Joe Allen
2026-08-18 14:56:00 +08:00
parent 9b7094dcf2
commit 7f6a4f84d8
4 changed files with 43 additions and 19 deletions
+33
View File
@@ -0,0 +1,33 @@
/**
* Pretty-print a JSON string if it's valid JSON.
* Returns the original string if it's not valid JSON.
*/
export function prettyJson(str: string): string {
if (!str || str.trim() === '') return str
try {
const parsed = JSON.parse(str)
return JSON.stringify(parsed, null, 2)
} catch {
return str
}
}
/**
* Check if a string is valid JSON.
*/
export function isJson(str: string): boolean {
try {
JSON.parse(str)
return true
} catch {
return false
}
}
/**
* Truncate a string to a maximum length, adding an ellipsis if truncated.
*/
export function truncate(str: string, maxLen: number): string {
if (str.length <= maxLen) return str
return str.substring(0, maxLen) + '...'
}