Skip to content

Commit fa69858

Browse files
committed
Track and album length
1 parent e8a4116 commit fa69858

File tree

7 files changed

+57
-4
lines changed

7 files changed

+57
-4
lines changed

packages/components/albums/src/album-detail.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ export class AlbumDetail extends LitElement {
5959
padding-bottom: 1rem;
6060
font-size: 1rem;
6161
}
62+
63+
h6 {
64+
margin: 0;
65+
padding-bottom: 1rem;
66+
font-size: 0.8rem;
67+
color: var(--secondary-text-color);
68+
}
69+
70+
div.track {
71+
display: flex;
72+
justify-content: space-between;
73+
}
74+
75+
div.track > .duration {
76+
color: var(--secondary-text-color);
77+
}
6278
`;
6379

6480
constructor() {
@@ -82,17 +98,47 @@ export class AlbumDetail extends LitElement {
8298
? html`(${this.album.releaseYear.value})`
8399
: nothing}
84100
</h5>
101+
<h6>${this._formatAlbumDuration()}</h6>
85102
</div>
86103
87104
<div class="track-list-container" slot="right-column">
88105
<h2>Tracks</h2>
89106
<ol class="track-list">
90-
${map(this.album.tracks, (track) => html`<li>${track.name}</li>`)}
107+
${map(
108+
this.album.tracks,
109+
(track) =>
110+
html`<div class="track">
111+
<li>${track.name}</li>
112+
<span class="duration"
113+
>${this._formatDuration(track.durationInSeconds)}</span
114+
>
115+
</div>`,
116+
)}
91117
</ol>
92118
</div>
93119
</two-column-layout>
94120
`;
95121
}
122+
123+
private _formatDuration(durationInSeconds: number): string {
124+
if (durationInSeconds === 0) return "";
125+
126+
const date = new Date(0);
127+
date.setSeconds(durationInSeconds);
128+
return date.toLocaleTimeString("en-US", {
129+
minute: "2-digit",
130+
second: "2-digit",
131+
});
132+
}
133+
134+
private _formatAlbumDuration(): string {
135+
const durationInSeconds = this.album.tracks.reduce(
136+
(acc, track) => acc + track.durationInSeconds,
137+
0,
138+
);
139+
const durationInMinutes = Math.floor(durationInSeconds / 60);
140+
return `${durationInMinutes} min`;
141+
}
96142
}
97143

98144
@customElement("album-detail-page")

packages/components/ui-atoms/src/layouts/two-column.layout.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class TwoColumnLayout extends LitElement {
99
static styles = css`
1010
div.container {
1111
display: flex;
12-
height: 100vh;
12+
padding-bottom: 2rem;
1313
}
1414
1515
.left-column {

packages/core/types/src/model/track.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export type Track = {
6969

7070
/**
7171
* Duration of the track in milliseconds. It must be greater than zero.
72-
* TODO: We would need to download the entire track to get this information. Consider re-adding this field when we have a way to get this information.
7372
*/
74-
// durationInMilliseconds: number,
73+
durationInSeconds: number;
7574
};

packages/core/types/src/services/metadata-provider.ts

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export type TrackMetadata = {
5959
* Keywords to reflect the mood of the audio, e.g. 'Romantic' or 'Sad'
6060
*/
6161
mood?: string | undefined;
62+
63+
/**
64+
* Length of the track in seconds.
65+
*/
66+
lengthInSeconds?: number | undefined;
6267
};
6368

6469
/**

packages/infrastructure/mmb-metadata-provider/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const mmbMetadataProvider = MetadataProvider.of({
4848
trackNumber: metadata.common.track.no ?? undefined,
4949
embeddedCover,
5050
year: metadata.common.year,
51+
lengthInSeconds: metadata.format.duration,
5152
};
5253
}),
5354
),

packages/infrastructure/spotify-provider/src/apis/list-albums-api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ const toTrackSchema = (
120120
},
121121
secondaryArtists: [],
122122
trackNumber: spotifyTrack.track_number,
123+
durationInSeconds: spotifyTrack.duration_ms / 1000,
123124
});
124125

125126
const downloadImage = (url?: string) =>

packages/workers/media-provider/src/sync/file-based-sync.ts

+1
Original file line numberDiff line numberDiff line change
@@ -421,5 +421,6 @@ const createTrack = (
421421
provider: FileBasedProviderId.OneDrive /* TODO: Take from metadata. */,
422422
fileId: file.id,
423423
},
424+
durationInSeconds: metadata.lengthInSeconds ?? 0,
424425
};
425426
});

0 commit comments

Comments
 (0)