From caaeb24f6569adb1e706e5a4b2a7173ac3afdc47 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=A1s=20Tamargo?=
Date: Thu, 8 Sep 2022 12:23:40 +0300
Subject: [PATCH] MBS-12572: Convert cdtoc/info to React
first_track and last_track seemed completely useless,
so I'm just removing them and using a map on track_details.
Since they didn't seem to be used elsewhere either,
this also removes the methods.
---
lib/MusicBrainz/Server/Entity/CDTOC.pm | 42 ++++++-------
root/cdtoc/CDTocInfo.js | 86 ++++++++++++++++++++++++++
root/cdtoc/index.tt | 3 +-
root/cdtoc/info.tt | 60 ------------------
root/cdtoc/set_durations.tt | 3 +-
root/server/components.mjs | 1 +
root/types/medium.js | 13 ++++
7 files changed, 123 insertions(+), 85 deletions(-)
create mode 100644 root/cdtoc/CDTocInfo.js
delete mode 100644 root/cdtoc/info.tt
diff --git a/lib/MusicBrainz/Server/Entity/CDTOC.pm b/lib/MusicBrainz/Server/Entity/CDTOC.pm
index c05b9c2fad5..0328d70d97a 100644
--- a/lib/MusicBrainz/Server/Entity/CDTOC.pm
+++ b/lib/MusicBrainz/Server/Entity/CDTOC.pm
@@ -38,18 +38,6 @@ has 'degraded' => (
isa => 'Boolean'
);
-sub first_track
-{
- return 1;
-}
-
-sub last_track
-{
- my ($self) = @_;
-
- return $self->track_count;
-}
-
sub sectors_to_ms
{
return int($_[0] / 75 * 1000);
@@ -69,14 +57,14 @@ sub track_details
my @track_info;
foreach my $track (0 .. ($self->track_count - 1)) {
my %info;
- $info{start_sectors} = $self->track_offset->[$track];
- $info{start_time} = sectors_to_ms($info{start_sectors});
+ $info{start_sectors} = 0 + $self->track_offset->[$track];
+ $info{start_time} = 0 + sectors_to_ms($info{start_sectors});
$info{end_sectors} = ($track == $self->track_count - 1)
- ? $self->leadout_offset
- : $self->track_offset->[$track + 1];
- $info{end_time} = sectors_to_ms($info{end_sectors});
- $info{length_sectors} = $info{end_sectors} - $info{start_sectors};
- $info{length_time} = sectors_to_ms($info{length_sectors});
+ ? 0 + $self->leadout_offset
+ : 0 + $self->track_offset->[$track + 1];
+ $info{end_time} = 0 + sectors_to_ms($info{end_sectors});
+ $info{length_sectors} = 0 + ($info{end_sectors} - $info{start_sectors});
+ $info{length_time} = 0 + sectors_to_ms($info{length_sectors});
push @track_info, \%info;
}
return \@track_info;
@@ -143,10 +131,18 @@ with 'MusicBrainz::Server::Entity::Role::TOC';
around TO_JSON => sub {
my ($orig, $self) = @_;
- my $json = $self->$orig;
- $json->{discid} = $self->discid;
-
- return $json;
+ my $data = {
+ %{ $self->$orig },
+ discid => $self->discid,
+ freedb_id => $self->freedb_id,
+ leadout_offset => 0 + $self->leadout_offset,
+ length => 0 + $self->length,
+ track_count => 0 + $self->track_count,
+ track_details => $self->track_details,
+ track_offset => [map { 0 + $_ } @{ $self->track_offset }],
+ };
+
+ return $data;
};
diff --git a/root/cdtoc/CDTocInfo.js b/root/cdtoc/CDTocInfo.js
new file mode 100644
index 00000000000..39059251cdc
--- /dev/null
+++ b/root/cdtoc/CDTocInfo.js
@@ -0,0 +1,86 @@
+/*
+ * @flow strict-local
+ * Copyright (C) 2022 MetaBrainz Foundation
+ *
+ * This file is part of MusicBrainz, the open internet music database,
+ * and is licensed under the GPL version 2, or (at your option) any
+ * later version: http://www.gnu.org/licenses/gpl-2.0.txt
+ */
+
+import * as React from 'react';
+
+import formatTrackLength
+ from '../static/scripts/common/utility/formatTrackLength.js';
+
+type Props = {
+ +cdToc: CDTocT,
+};
+
+function calculateFullToc(cdtoc: CDTocT) {
+ const trackOffsets = cdtoc.track_offset.join(' ');
+ return `1 ${cdtoc.track_count} ${cdtoc.leadout_offset} ${trackOffsets}`;
+}
+
+const CDTocInfo = ({cdToc}: Props): React.Element => (
+ <>
+ {l('CD TOC details')}
+
+
+
+ {l('Full TOC:')} |
+ {calculateFullToc(cdToc)} |
+
+
+
+ {l('Disc ID:')} |
+ {cdToc.discid} |
+
+
+ {l('FreeDB:')} |
+ {cdToc.freedb_id} |
+
+
+ {l('Total tracks:')} |
+ {cdToc.track_count} |
+
+
+ {l('Total length:')} |
+ {formatTrackLength(cdToc.length)} |
+
+
+ {l('Track details:')} |
+
+
+
+ {l('Track')} |
+ {l('Start')} |
+ {l('Length')} |
+ {l('End')} |
+
+
+ {l('Time')} |
+ {l('Sectors')} |
+ {l('Time')} |
+ {l('Sectors')} |
+ {l('Time')} |
+ {l('Sectors')} |
+
+ {cdToc.track_details.map((track, index) => (
+
+ {index + 1} |
+ {formatTrackLength(track.start_time)} |
+ {track.start_sectors} |
+ {formatTrackLength(track.length_time)} |
+ {track.length_sectors} |
+ {formatTrackLength(track.end_time)} |
+ {track.end_sectors} |
+
+ ))}
+
+ |
+
+
+ >
+);
+
+export default CDTocInfo;
diff --git a/root/cdtoc/index.tt b/root/cdtoc/index.tt
index 8a90df55ca1..772edbeb969 100644
--- a/root/cdtoc/index.tt
+++ b/root/cdtoc/index.tt
@@ -2,7 +2,8 @@
[% l('Disc ID “{discid}
”', { discid => link_cdtoc(cdtoc) }) %]
- [% INCLUDE 'cdtoc/info.tt' %]
+ [%- cdtoc_json_obj = React.to_json_object(cdtoc) -%]
+ [% React.embed(c, 'cdtoc/CDTocInfo', {cdToc => cdtoc_json_obj}) %]
[% l('Attached to releases') %]
[% INCLUDE 'cdtoc/list.tt' edit_links = 1 %]
diff --git a/root/cdtoc/info.tt b/root/cdtoc/info.tt
deleted file mode 100644
index 106476a85f1..00000000000
--- a/root/cdtoc/info.tt
+++ /dev/null
@@ -1,60 +0,0 @@
- [% l('CD TOC details') %]
-
-
-
- [% l('Full TOC:') %] |
- 1 [% cdtoc.track_count %] [% cdtoc.leadout_offset %]
- [% cdtoc.track_offset.join(' ') %] |
-
-
-
- [% l('Disc ID:') %] |
- [% cdtoc.discid %] |
-
-
- [% l('FreeDB:') %] |
- [% cdtoc.freedb_id %] |
-
-
- [% l('Total tracks:') %] |
- [% cdtoc.last_track %] |
-
-
- [% l('Total length:') %] |
- [% cdtoc.length | format_length %] |
-
-
- [% l('Track details:') %] |
-
-
-
- [% l('Track') %] |
- [% l('Start') %] |
- [% l('Length') %] |
- [% l('End') %] |
-
-
- [% l('Time') %] |
- [% l('Sectors') %] |
- [% l('Time') %] |
- [% l('Sectors') %] |
- [% l('Time') %] |
- [% l('Sectors') %] |
-
- [% FOR i IN [ cdtoc.first_track .. cdtoc.last_track ] %]
- [% index = i - 1 %]
-
- [% i %] |
- [%- track = cdtoc.track_details.$index -%]
- [% track.start_time | format_length %] |
- [% track.start_sectors %] |
- [% track.length_time | format_length %] |
- [% track.length_sectors %] |
- [% track.end_time | format_length %] |
- [% track.end_sectors %] |
-
- [% END %]
-
- |
-
-
diff --git a/root/cdtoc/set_durations.tt b/root/cdtoc/set_durations.tt
index fad389da780..b81f21ac58f 100644
--- a/root/cdtoc/set_durations.tt
+++ b/root/cdtoc/set_durations.tt
@@ -5,7 +5,8 @@
to match that of the below disc ID.') %]
- [% INCLUDE 'cdtoc/info.tt' %]
+ [%- cdtoc_json_obj = React.to_json_object(cdtoc) -%]
+ [% React.embed(c, 'cdtoc/CDTocInfo', {cdToc => cdtoc_json_obj}) %]
[% l('Medium') %]
diff --git a/root/server/components.mjs b/root/server/components.mjs
index 87a4dd01222..41e9d1ba3b4 100644
--- a/root/server/components.mjs
+++ b/root/server/components.mjs
@@ -343,6 +343,7 @@ export default {
*/
'area/AreaHeader': (): Promise => import('../area/AreaHeader.js'),
'artist/ArtistHeader': (): Promise => import('../artist/ArtistHeader.js'),
+ 'cdtoc/CDTocInfo': (): Promise => import('../cdtoc/CDTocInfo.js'),
'collection/CollectionHeader': (): Promise => import('../collection/CollectionHeader.js'),
'components/UserAccountTabs': (): Promise => import('../components/UserAccountTabs.js'),
'edit/components/EditList': (): Promise => import('../edit/components/EditList.js'),
diff --git a/root/types/medium.js b/root/types/medium.js
index e90b06b9df6..2c507ec9edc 100644
--- a/root/types/medium.js
+++ b/root/types/medium.js
@@ -10,6 +10,19 @@
declare type CDTocT = $ReadOnly<{
...EntityRoleT<'cdtoc'>,
+discid: string,
+ +freedb_id: string,
+ +leadout_offset: number,
+ +length: number,
+ +track_count: number,
+ +track_details: $ReadOnlyArray<{
+ +end_sectors: number,
+ +end_time: number,
+ +length_sectors: number,
+ +length_time: number,
+ +start_sectors: number,
+ +start_time: number,
+ }>,
+ +track_offset: $ReadOnlyArray,
}>;
declare type MediumCDTocT = $ReadOnly<{