Skip to content

Commit 0e536be

Browse files
committed
fix: shortened filename should not repeat chars
1 parent 84b5bfb commit 0e536be

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

ts/components/conversation/message/message-content/quote/QuoteText.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { GoogleChrome } from '../../../../../util';
1010
import { MessageBody } from '../MessageBody';
1111
import { QuoteProps } from './Quote';
1212
import { tr } from '../../../../../localization/localeTools';
13+
import { collapseString } from '../../../../../shared/string_utils';
1314

1415
const StyledQuoteText = styled.div<{ $isIncoming: boolean }>`
1516
display: -webkit-box;
@@ -72,12 +73,9 @@ export function getShortenedFilename(fileName: string) {
7273
if (!fileName) {
7374
return '';
7475
}
75-
if (fileName?.length < 20) {
76-
return fileName;
77-
}
7876
const charsAround = 15;
7977

80-
return `${fileName.slice(0, charsAround)}${fileName.slice(-charsAround)}`;
78+
return collapseString(fileName, charsAround, charsAround, true);
8179
}
8280

8381
export const QuoteText = (

ts/shared/string_utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ export function uniqFromListOfList<T extends string>(list: Array<Array<T>>): Arr
99

1010
/**
1111
* Collapses a string by replacing characters between the leading and trailing characters with a triple ellipsis unicode character (length 1).
12-
* The final length of the string will be the sum of the leading and trailing characters plus 1.
12+
* The final length of the string will be the sum of the leading and trailing characters plus 1 (or 3 if spacesAroundEllipsis is true).
1313
* @param str - The input string to collapse.
1414
* @param leadingChars - The number of characters to keep at the beginning of the string.
1515
* @param trailingChars - The number of characters to keep at the end of the string.
16+
* @param spacesAroundEllipsis - Whether to add spaces around the ellipsis.
1617
* @param separator - The separator to use between the leading and trailing characters.
1718
* @returns The collapsed string.
1819
*/
1920
export const collapseString = (
2021
str: string,
2122
leadingChars = 6,
2223
trailingChars = 4,
24+
spacesAroundEllipsis = false,
2325
separator = '…'
2426
): string => {
25-
if (str.length <= leadingChars + trailingChars + 3) {
27+
if (str.length <= leadingChars + trailingChars + 1 + (spacesAroundEllipsis ? 2 : 0)) {
2628
return str;
2729
}
28-
return `${str.slice(0, leadingChars)}${separator}${str.slice(-trailingChars)}`;
30+
const ellipsis = spacesAroundEllipsis ? ` ${separator} ` : separator;
31+
return `${str.slice(0, leadingChars)}${ellipsis}${str.slice(-trailingChars)}`;
2932
};

0 commit comments

Comments
 (0)