Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added filename display for audio, non-voice attachments, and filename display when playing audio files #6711

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ts/components/MiniPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { durationToPlaybackText } from '../util/durationToPlaybackText';
import { PlaybackButton } from './PlaybackButton';
import { PlaybackRateButton } from './PlaybackRateButton';
import { UserText } from './UserText';
import { shortenFileName } from '../util/attachments';

export enum PlayerState {
loading = 'loading',
Expand All @@ -18,6 +19,7 @@ export enum PlayerState {
export type Props = Readonly<{
i18n: LocalizerType;
title: string;
fileName: string | undefined
currentTime: number;
// not available until audio has loaded
duration: number | undefined;
Expand All @@ -34,6 +36,7 @@ export type Props = Readonly<{
export function MiniPlayer({
i18n,
title,
fileName,
state,
currentTime,
duration,
Expand Down Expand Up @@ -100,8 +103,9 @@ export function MiniPlayer({
<div className="MiniPlayer__state">
<UserText text={title} />
<span className="MiniPlayer__middot">&middot;</span>
<span>{shortenFileName(fileName, true)}</span>
{duration !== undefined && (
<span>
<span style={{marginLeft: fileName && '6px' || '0px'}}>
{durationToPlaybackText(
state === PlayerState.loading ? duration : currentTime
)}
Expand Down
13 changes: 10 additions & 3 deletions ts/components/conversation/MessageAudio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { WaveformScrubber } from './WaveformScrubber';
import { useComputePeaks } from '../../hooks/useComputePeaks';
import { durationToPlaybackText } from '../../util/durationToPlaybackText';
import { shouldNeverBeCalled } from '../../util/shouldNeverBeCalled';
import { shortenFileName } from '../../util/attachments';

export type OwnProps = Readonly<{
active:
Expand Down Expand Up @@ -167,7 +168,7 @@ export function MessageAudio(props: Props): JSX.Element {
const [isPlayedDotVisible, setIsPlayedDotVisible] = React.useState(!played);

const audioUrl = isDownloaded(attachment) ? attachment.url : undefined;

const fileName = (attachment.fileName && !attachment.isVoiceMessage) ? shortenFileName(attachment.fileName) : undefined;
const { duration, hasPeaks, peaks } = useComputePeaks({
audioUrl,
activeDuration: active?.duration,
Expand Down Expand Up @@ -369,7 +370,6 @@ export function MessageAudio(props: Props): JSX.Element {
)}
</div>
);

return (
<div
className={classNames(
Expand All @@ -381,7 +381,14 @@ export function MessageAudio(props: Props): JSX.Element {
>
<div className={`${CSS_BASE}__button-and-waveform`}>
{button}
{waveform}
<div>
<span className={`module-message__text module-message__text--${direction}`} style={{
fontSize: '12px',
}}>
{fileName}
</span>
{waveform}
</div>
</div>
{metadata}
</div>
Expand Down
3 changes: 3 additions & 0 deletions ts/state/selectors/audioPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type VoiceNoteForPlayback = {
id: string;
// undefined if download is pending
url: string | undefined;
fileName: string | undefined;
type: 'incoming' | 'outgoing';
source: string | undefined;
sourceServiceId: ServiceIdString | undefined;
Expand Down Expand Up @@ -96,11 +97,13 @@ export function extractVoiceNoteForPlayback(
const voiceNoteUrl = attachment.path
? getAttachmentUrlForPath(attachment.path)
: undefined;
const fileName = attachment.fileName;
const status = getMessagePropStatus(message, ourConversationId);

return {
id: message.id,
url: voiceNoteUrl,
fileName,
type,
isPlayed: isPlayed(type, status, message.readStatus),
messageIdForLogging: getMessageIdForLogging(message),
Expand Down
4 changes: 4 additions & 0 deletions ts/state/smart/MiniPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export function SmartMiniPlayer({ shouldFlow }: Props): JSX.Element | null {
const url = AudioPlayerContent.isVoiceNote(content)
? content.current.url
: content.url;
const fileName = AudioPlayerContent.isVoiceNote(content)
? content.current.fileName
: undefined

let state = PlayerState.loading;
if (url) {
Expand All @@ -55,6 +58,7 @@ export function SmartMiniPlayer({ shouldFlow }: Props): JSX.Element | null {
? i18n('icu:you')
: getVoiceNoteTitle(content.current)
}
fileName={fileName}
onPlay={handlePlay}
onPause={handlePause}
onPlaybackRate={setPlaybackRate}
Expand Down
32 changes: 32 additions & 0 deletions ts/util/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,35 @@ export function copyCdnFields(
plaintextHash: uploaded.plaintextHash,
};
}
/**
* Shortens a file name based on specified conditions.
* @param fileName - The original file name.
* @param isPlaying - If true, truncate the file name to maxLength characters and add '...' at the end.
* - If false, show the first 15 characters, add '...', and the last 15 characters.
* @param maxLength - The maximum length of the file name (default: 30).
* @returns The shortened file name.
*/
export function shortenFileName(
fileName: string | undefined,
isPlaying: boolean = false,
maxLength: number = 30
): string {
if (fileName === undefined) return '';
if (fileName.length <= maxLength) {
return fileName;
}

if (!isPlaying) {
const fileNameWithoutExtension = fileName.split('.').slice(0, -1).join('.');
const extension = fileName.split('.').pop();
const shortenedFileName = `${fileNameWithoutExtension.slice(
0,
15
)}...${fileNameWithoutExtension.slice(-15)}`;

return `${shortenedFileName}.${extension}`;
} else {
const truncatedFileName = `${fileName.slice(0, maxLength - 3)}...`;
return truncatedFileName;
}
}