43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import type { Model } from '@/bindings.gen';
|
|
import { bindings } from '@/main';
|
|
import { useEffect, useState } from 'react';
|
|
import useCommand from './useCommand';
|
|
|
|
function useRecentCluster() {
|
|
const result = useCommand('getClusters', bindings.core.get_clusters);
|
|
const [cluster, setCluster] = useState<Model | undefined>();
|
|
|
|
useEffect(() => {
|
|
if (!result.data)
|
|
return;
|
|
|
|
let mostRecentCluster: Model | undefined;
|
|
|
|
for (const c of result.data) {
|
|
if (!mostRecentCluster) {
|
|
mostRecentCluster = c;
|
|
continue;
|
|
}
|
|
|
|
const currentPlayed = mostRecentCluster.last_played;
|
|
const newPlayed = c.last_played;
|
|
|
|
if (typeof currentPlayed !== 'string' && typeof newPlayed === 'string') {
|
|
mostRecentCluster = c;
|
|
}
|
|
else if (typeof currentPlayed === 'string' && typeof newPlayed === 'string') {
|
|
const playedAt = new Date(currentPlayed);
|
|
const clusterPlayedAt = new Date(newPlayed);
|
|
|
|
if (clusterPlayedAt > playedAt)
|
|
mostRecentCluster = c;
|
|
}
|
|
}
|
|
|
|
setCluster(mostRecentCluster);
|
|
}, [result.data]);
|
|
|
|
return cluster;
|
|
}
|
|
|
|
export default useRecentCluster;
|