fix: 修复 truncate 函数接收到 undefined/null 时崩溃的问题

BackgroundTask 组件渲染时传入的 task 属性(description、title、command 等)
可能为 undefined,导致 str.indexOf('\n') 抛出 TypeError。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-28 09:15:58 +08:00
parent de9dbcdcbb
commit b8b48bf7ed

View File

@@ -132,10 +132,11 @@ export function truncateToWidthNoEllipsis(
* @returns The truncated string with ellipsis if needed
*/
export function truncate(
str: string,
str: string | undefined | null,
maxWidth: number,
singleLine: boolean = false,
): string {
if (str == null) return ''
let result = str
// If singleLine is true, truncate at first newline