Skip to content

Commit

Permalink
Fix row refresh on status change for current song
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffvli committed Oct 19, 2023
1 parent 8e2a107 commit 768a88d
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { MutableRefObject, useEffect, useMemo, useRef } from 'react';
import { RowClassRules, RowNode } from '@ag-grid-community/core';
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
import { MutableRefObject, useEffect, useMemo, useRef } from 'react';
import { Song } from '/@/renderer/api/types';
import { useAppFocus } from '/@/renderer/hooks';
import { useCurrentSong, usePlayerStore } from '/@/renderer/store';
import { PlayerStatus } from '/@/renderer/types';

interface UseCurrentSongRowStylesProps {
tableRef: MutableRefObject<AgGridReactType | null>;
Expand Down Expand Up @@ -75,28 +76,25 @@ export const useCurrentSongRowStyles = ({ tableRef }: UseCurrentSongRowStylesPro

// Redraw song rows when the status changes
const unsubStatusChange = usePlayerStore.subscribe(
(state) => state.current.song,
(song, previousSong) => {
(state) => [state.current.status, state.current.song],
(current: (PlayerStatus | Song | undefined)[]) => {
const song = current[1] as Song;

if (tableRef?.current) {
const { api, columnApi } = tableRef?.current || {};
if (api == null || columnApi == null) {
return;
}

const currentNode = song?.id ? api.getRowNode(song.id) : undefined;

const previousNode = previousSong?.id
? api.getRowNode(previousSong?.id)
: undefined;

const rowNodes = [currentNode, previousNode].filter(
(e) => e !== undefined,
) as RowNode<any>[];
const rowNodes = [currentNode].filter((e) => e !== undefined) as RowNode<any>[];

api.redrawRows({ rowNodes });
}
},
{ equalityFn: (a, b) => a?.id === b?.id },
{
equalityFn: (a, b) => (a[0] as PlayerStatus) === (b[0] as PlayerStatus),
},
);

return () => {
Expand Down

1 comment on commit 768a88d

@vercel
Copy link

@vercel vercel bot commented on 768a88d Oct 19, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

feishin – ./

feishin-jeffvli.vercel.app
feishin-git-development-jeffvli.vercel.app
feishin.vercel.app

Please sign in to comment.