Skip to content

Commit

Permalink
Maybe!
Browse files Browse the repository at this point in the history
  • Loading branch information
dominic-codespoti committed Aug 12, 2024
1 parent 9d5c58f commit 065e42c
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/ranker/ranker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,6 @@ export const Ranker: React.FC<Props> = ({ artistName, limit = 10 }) => {
const [votes, setVotes] = useState<Record<string, number>>({});
const [history, setHistory] = useState<Array<{ rankings: Song[]; currentSong: Song | null; comparisonSong: Song | null; remainingSongs: Song[]; votes: Record<string, number>; }>>([]);

const findInsertionIndex = (song: Song, start: number, end: number): number => {
if (start >= end) {
return start;
}

const mid = Math.floor((start + end) / 2);
const comparison = rankings[mid];

if ((votes[song.trackId] || 0) > (votes[comparison.trackId] || 0) ||
((votes[song.trackId] || 0) === (votes[comparison.trackId] || 0) &&
song.trackName.localeCompare(comparison.trackName) < 0)) {
return findInsertionIndex(song, start, mid);
} else {
return findInsertionIndex(song, mid + 1, end);
}
};

const undo = (): void => {
if (history.length === 0) return;

Expand Down Expand Up @@ -104,6 +87,23 @@ export const Ranker: React.FC<Props> = ({ artistName, limit = 10 }) => {
};

const content = useMemo(() => {
const findInsertionIndex = (song: Song, start: number, end: number): number => {
if (start >= end) {
return start;
}

const mid = Math.floor((start + end) / 2);
const comparison = rankings[mid];

if ((votes[song.trackId] || 0) > (votes[comparison.trackId] || 0) ||
((votes[song.trackId] || 0) === (votes[comparison.trackId] || 0) &&
song.trackName.localeCompare(comparison.trackName) < 0)) {
return findInsertionIndex(song, start, mid);
} else {
return findInsertionIndex(song, mid + 1, end);
}
};

const compare = (preference: ComparisonState): void => {
if (!currentSong || !comparisonSong) return;

Expand Down Expand Up @@ -198,7 +198,7 @@ export const Ranker: React.FC<Props> = ({ artistName, limit = 10 }) => {
</div>
</div>
);
}, [currentSong, comparisonSong, rankings, isComplete, isReady, votes, calculateAlbumRankings]);
}, [currentSong, comparisonSong, rankings, isComplete, isReady, votes, calculateAlbumRankings, remainingSongs]);

useEffect(() => {
const startRanking = (): void => {
Expand Down

0 comments on commit 065e42c

Please sign in to comment.