|
@@ -1,9 +1,8 @@
|
|
|
-import React, { useCallback, useEffect } from 'react';
|
|
|
|
|
-import { ActionTypeLocal } from '../../constants/actions';
|
|
|
|
|
-import { useGlobalState } from '../../hooks/reducer';
|
|
|
|
|
|
|
+import React, { useCallback, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
+import { stateSet } from '../../actions';
|
|
|
import { useKeepalive } from '../../hooks/socket';
|
|
import { useKeepalive } from '../../hooks/socket';
|
|
|
-import { MusicPlayer } from '../../types/state';
|
|
|
|
|
|
|
+import { useGlobalState } from '../../hooks/state';
|
|
|
import { ClientList } from '../client-list';
|
|
import { ClientList } from '../client-list';
|
|
|
|
|
|
|
|
export type Props = {
|
|
export type Props = {
|
|
@@ -11,54 +10,55 @@ export type Props = {
|
|
|
socket: WebSocket;
|
|
socket: WebSocket;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-const testSongId = 7954; // TODO
|
|
|
|
|
-
|
|
|
|
|
export const Gmus: React.FC<Props> = ({ myClientName, socket }) => {
|
|
export const Gmus: React.FC<Props> = ({ myClientName, socket }) => {
|
|
|
useKeepalive(socket);
|
|
useKeepalive(socket);
|
|
|
|
|
|
|
|
- const [state, dispatch] = useGlobalState(socket);
|
|
|
|
|
|
|
+ const [{ clientList, player }, dispatch] = useGlobalState(socket);
|
|
|
|
|
+
|
|
|
|
|
+ const [tempSongId, setTempSongId] = useState<number>(0);
|
|
|
|
|
|
|
|
- const playSong = React.useCallback(() => {
|
|
|
|
|
- dispatch({
|
|
|
|
|
- type: ActionTypeLocal.StateSet,
|
|
|
|
|
- payload: {
|
|
|
|
|
- songId: testSongId,
|
|
|
|
|
|
|
+ const playSong = useCallback((): void => {
|
|
|
|
|
+ if (!tempSongId) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ dispatch(
|
|
|
|
|
+ stateSet({
|
|
|
|
|
+ songId: tempSongId,
|
|
|
playTimeSeconds: 0,
|
|
playTimeSeconds: 0,
|
|
|
playing: true,
|
|
playing: true,
|
|
|
currentClient: myClientName,
|
|
currentClient: myClientName,
|
|
|
- },
|
|
|
|
|
- });
|
|
|
|
|
- }, [myClientName]);
|
|
|
|
|
-
|
|
|
|
|
- const pauseSong = React.useCallback(() => {
|
|
|
|
|
- setLocalState((last) => {
|
|
|
|
|
- const next: MusicPlayer = { ...last, playing: false };
|
|
|
|
|
|
|
+ }),
|
|
|
|
|
+ );
|
|
|
|
|
+ }, [dispatch, tempSongId, myClientName]);
|
|
|
|
|
|
|
|
- socket.send(
|
|
|
|
|
- JSON.stringify({
|
|
|
|
|
- type: 'STATE_SET',
|
|
|
|
|
- payload: JSON.stringify(next),
|
|
|
|
|
- }),
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- return next;
|
|
|
|
|
- });
|
|
|
|
|
- }, [ws]);
|
|
|
|
|
|
|
+ const playPause = useCallback(() => {
|
|
|
|
|
+ dispatch(
|
|
|
|
|
+ stateSet({
|
|
|
|
|
+ ...player,
|
|
|
|
|
+ playing: !player.playing,
|
|
|
|
|
+ }),
|
|
|
|
|
+ );
|
|
|
|
|
+ }, [dispatch, player]);
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<div>
|
|
<div>
|
|
|
<div>
|
|
<div>
|
|
|
- <button onClick={playSong}>Play!</button>
|
|
|
|
|
- <button onClick={pauseSong}>Pause!</button>
|
|
|
|
|
|
|
+ <button onClick={playPause}>{player.playing ? 'Pause' : 'Play'}</button>
|
|
|
</div>
|
|
</div>
|
|
|
- <ClientList clients={clientList} />
|
|
|
|
|
<div>
|
|
<div>
|
|
|
- <h6>Local State</h6>
|
|
|
|
|
- <pre>{JSON.stringify(localState, null, 2)}</pre>
|
|
|
|
|
|
|
+ <input
|
|
|
|
|
+ onChange={({ target: { value } }): void => setTempSongId(Number(value))}
|
|
|
|
|
+ type="number"
|
|
|
|
|
+ min={0}
|
|
|
|
|
+ step={1}
|
|
|
|
|
+ />
|
|
|
|
|
+ <button onClick={playSong}>Change track</button>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+ <ClientList clients={clientList} />
|
|
|
<div>
|
|
<div>
|
|
|
- <h6>Remote State</h6>
|
|
|
|
|
- <pre>{JSON.stringify(remoteState, null, 2)}</pre>
|
|
|
|
|
|
|
+ <h6>Player State</h6>
|
|
|
|
|
+ <pre>{JSON.stringify(player, null, 2)}</pre>
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
);
|
|
);
|