Skip to content

Commit dc515d1

Browse files
fix time estimate code
1 parent 55d0864 commit dc515d1

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/renderer/components/Main.tsx

+27-10
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,20 @@ export default function Main() {
5959
});
6060
const [importState, setImportState] = useState({
6161
songsImported: 0,
62-
totalSongs: 0,
62+
totalSongs: 1,
6363
estimatedTimeRemainingString: '',
6464
});
6565
const [showAlbumArtMenu, setShowAlbumArtMenu] = useState<AlbumArtMenuState>();
6666

6767
const importNewLibrary = async (rescan = false) => {
6868
setDialogState((prev) => ({ ...prev, showImportingProgress: true }));
6969

70+
setImportState((prev) => ({
71+
...prev,
72+
songsImported: 0,
73+
totalSongs: 1,
74+
}));
75+
7076
window.electron.ipcRenderer.sendMessage('select-library', {
7177
rescan,
7278
});
@@ -78,18 +84,29 @@ export default function Main() {
7884
totalSongs: args.totalSongs,
7985
}));
8086

81-
// Calculate estimated time remaining
82-
const timePerSong = 0.1; // seconds per song (approximate)
83-
const remainingSongs = args.totalSongs - args.songsImported;
84-
const estimatedSeconds = remainingSongs * timePerSong;
85-
const minutes = Math.floor(estimatedSeconds / 60);
86-
const seconds = Math.floor(estimatedSeconds % 60);
87+
// Average time per song based on testing:
88+
// - ~3ms for metadata parsing
89+
// - ~6ms for file copy (if needed)
90+
// - ~1ms overhead
91+
const avgTimePerSong = 10; // milliseconds
92+
const estimatedTimeRemaining = Math.floor(
93+
(args.totalSongs - args.songsImported) * avgTimePerSong,
94+
);
95+
96+
const minutes = Math.floor(estimatedTimeRemaining / 60000);
97+
const seconds = Math.floor((estimatedTimeRemaining % 60000) / 1000);
98+
99+
const timeRemainingString =
100+
// eslint-disable-next-line no-nested-ternary
101+
minutes < 1
102+
? seconds === 0
103+
? 'Processing Metadata...'
104+
: `${seconds}s left`
105+
: `${minutes}m ${seconds}s left`;
87106

88107
setImportState((prev) => ({
89108
...prev,
90-
estimatedTimeRemainingString: `${minutes}:${seconds
91-
.toString()
92-
.padStart(2, '0')}`,
109+
estimatedTimeRemainingString: timeRemainingString,
93110
}));
94111
});
95112

0 commit comments

Comments
 (0)