Truncates text for toast descriptions and provides inline expansion capability.
Preserves single newlines while collapsing multiple spaces/tabs to single spaces.
Source
exportconsttruncateToastMessage = ( text: string, maxLength: number = 200, ): { isTruncated:boolean; truncatedText: string; fullText: string; component: React.ReactNode; } => { // Early return for whitespace-only strings if (text.trim().length === 0) { return { isTruncated:false, truncatedText:text, fullText:text, component:text, }; }
// Normalize consecutive spaces/tabs to single spaces, but preserve newlines constnormalizedText = text .replaceAll(/[ \t]+/g, " ") // Collapse spaces and tabs .replaceAll(/\n\n+/g, "\n"); // Collapse multiple newlines to single newline
// Find last space before maxLength for word boundary truncation lettruncateIndex = maxLength; constlastSpaceIndex = normalizedText.lastIndexOf(" ", maxLength);
if (lastSpaceIndex > 0 && lastSpaceIndex > maxLength - 50) { // Use word boundary if space is close to maxLength truncateIndex = lastSpaceIndex; }
Truncates text for toast descriptions and provides inline expansion capability. Preserves single newlines while collapsing multiple spaces/tabs to single spaces.
Source