mirror of
https://github.com/streamyfin/streamyfin.git
synced 2025-08-20 18:37:18 +02:00
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { useEffect } from "react";
|
|
import { Alert } from "react-native";
|
|
import { useRouter } from "expo-router";
|
|
import { useWebSocketContext } from "@/providers/WebSocketProvider";
|
|
|
|
interface UseWebSocketProps {
|
|
isPlaying: boolean;
|
|
togglePlay: () => void;
|
|
stopPlayback: () => void;
|
|
offline: boolean;
|
|
}
|
|
|
|
export const useWebSocket = ({
|
|
isPlaying,
|
|
togglePlay,
|
|
stopPlayback,
|
|
offline,
|
|
}: UseWebSocketProps) => {
|
|
const router = useRouter();
|
|
const { ws } = useWebSocketContext();
|
|
|
|
useEffect(() => {
|
|
if (!ws) return;
|
|
if (offline) return;
|
|
|
|
ws.onmessage = (e) => {
|
|
const json = JSON.parse(e.data);
|
|
const command = json?.Data?.Command;
|
|
|
|
console.log("[WS] ~ ", json);
|
|
|
|
if (command === "PlayPause") {
|
|
console.log("Command ~ PlayPause");
|
|
togglePlay();
|
|
} else if (command === "Stop") {
|
|
console.log("Command ~ Stop");
|
|
stopPlayback();
|
|
router.canGoBack() && router.back();
|
|
} else if (json?.Data?.Name === "DisplayMessage") {
|
|
console.log("Command ~ DisplayMessage");
|
|
const title = json?.Data?.Arguments?.Header;
|
|
const body = json?.Data?.Arguments?.Text;
|
|
Alert.alert("Message from server: " + title, body);
|
|
}
|
|
};
|
|
|
|
return () => {
|
|
ws.onmessage = null;
|
|
};
|
|
}, [ws, stopPlayback, togglePlay, isPlaying, router]);
|
|
};
|