Skip to content

Commit

Permalink
Merge pull request #230 from lovegaoshi/dev
Browse files Browse the repository at this point in the history
fix: append writing to lrc
  • Loading branch information
lovegaoshi authored Nov 7, 2023
2 parents c863a5a + dce08b7 commit 69b8127
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
73 changes: 37 additions & 36 deletions src/components/player/Lyric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,44 +98,45 @@ export const LyricView = ({
const setLyricMapping = useNoxSetting(state => state.setLyricMapping);

useEffect(() => {
if (track !== undefined && track.title !== '') {
logger.log('Initiating Lyric with new track...');
setCurrentTimeOffset(0);
setLrcOption(null);
setLrc('正在加载歌词...');
setSearchText(track.title || '');
const lrcOptionPromise = fetchAndSetLyricOptions();
// Initialize from storage if not new
// HACK: probably needs to be refactored because teh state updates
// but if it works it works
if (hasLrcFromLocal()) {
logger.log('[lyric] Loading Lrc from localStorage...');
const lrcDetail = lyricMapping.get(track?.song.id);
if (lrcDetail === undefined) return;
setLrcOption({ key: lrcDetail?.lyricKey });
setCurrentTimeOffset(lrcDetail!.lyricOffset);
readTxtFile(lrcDetail.lyric, 'lrc/').then(readlrc => {
if (readlrc) {
logger.debug('[lrc] read local lrc and loading...');
setLrc(readlrc);
} else if (lrcDetail.lyric.endsWith('.txt')) {
logger.debug('[lrc] local lrc no longer exists, fetching new...');
lrcOptionPromise.then(
val => val && searchAndSetCurrentLyric(undefined, val)
);
} else {
logger.debug(
'[lrc] local lrc seems to be the content itself, loading that...'
);
setLrc(lrcDetail.lyric);
}
});
} else {
if (track === undefined || track.title === '') return;
logger.log('Initiating Lyric with new track...');
setCurrentTimeOffset(0);
setLrcOption(null);
setLrc('正在加载歌词...');
setSearchText(track.title || '');
const lrcOptionPromise = fetchAndSetLyricOptions();
const loadLocalLrc = async () => {
if (!hasLrcFromLocal()) return false;
logger.log('[lyric] Loading Lrc from localStorage...');
const lrcDetail = lyricMapping.get(track?.song.id);
if (lrcDetail === undefined) return false;
setLrcOption({ key: lrcDetail?.lyricKey });
setCurrentTimeOffset(lrcDetail!.lyricOffset);
if (lrcDetail.lyric.endsWith('.txt')) {
const readlrc = await readTxtFile(lrcDetail.lyric, 'lrc/');
if (readlrc) {
logger.debug('[lrc] read local lrc and loading...');
setLrc(readlrc);
return true;
} else if (lrcDetail.lyric.endsWith('.txt')) {
logger.debug('[lrc] local lrc no longer exists, fetching new...');
}
return false;
}
logger.debug(
'[lrc] local lrc seems to be the content itself, loading that...'
);
setLrc(lrcDetail.lyric);
return true;
};
// Initialize from storage if not new
loadLocalLrc().then(
localLrcLoaded =>
!localLrcLoaded &&
lrcOptionPromise.then(
val => val && searchAndSetCurrentLyric(undefined, val)
);
}
}
)
);
}, [track]);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const writeTxtFile = async (
// encoding, should be one of `base64`, `utf8`, `ascii`
'utf8',
// should data append to existing content ?
true
false
)
.then(stream => Promise.all(content.map(val => stream.write(val))))
// Use array destructuring to get the stream object from the first item of the array we get from Promise.all()
Expand Down

1 comment on commit 69b8127

@vercel
Copy link

@vercel vercel bot commented on 69b8127 Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.