2026-08-25 07:51:38 +02:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
|
import { toast } from "sonner";
|
2026-08-31 17:31:57 +02:00
|
|
|
import { Card, CardContent } from "@/components/ui/card";
|
2026-08-25 07:51:38 +02:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Slider } from "@/components/ui/slider";
|
|
|
|
|
import { DeviceScanPanel } from "@/components/control/DeviceScanPanel";
|
|
|
|
|
import { DeviceRemapDialog } from "./DeviceRemapDialog";
|
2026-08-25 21:26:18 +02:00
|
|
|
import { disconnectAll, getButtplugClientHandle, getDevice, startScanning, stopScanning } from "@/lib/buttplug/client";
|
2026-08-27 21:38:51 +02:00
|
|
|
import { SessionPlayer, type SessionEventRow } from "@/lib/buttplug/player";
|
2026-08-25 07:51:38 +02:00
|
|
|
import { useButtplugStore } from "@/lib/buttplug/store";
|
2026-08-27 21:38:51 +02:00
|
|
|
import type { ActuatorInfo, ReplayDeviceSlot } from "@/lib/buttplug/types";
|
2026-09-01 19:02:32 +02:00
|
|
|
import { Info, Pause, Play } from "lucide-react";
|
2026-09-01 19:06:28 +02:00
|
|
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-27 21:38:51 +02:00
|
|
|
interface ReplayDataResponse {
|
|
|
|
|
session: {
|
2026-08-25 07:51:38 +02:00
|
|
|
id: number;
|
2026-08-27 21:38:51 +02:00
|
|
|
name: string | null;
|
2026-08-25 07:51:38 +02:00
|
|
|
durationMs: number;
|
|
|
|
|
};
|
2026-08-27 21:38:51 +02:00
|
|
|
deviceSlots: ReplayDeviceSlot[];
|
|
|
|
|
events: SessionEventRow[];
|
2026-08-25 07:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatTime(ms: number): string {
|
|
|
|
|
const totalSeconds = Math.floor(ms / 1000);
|
|
|
|
|
return `${Math.floor(totalSeconds / 60)}:${(totalSeconds % 60).toString().padStart(2, "0")}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 21:38:51 +02:00
|
|
|
export function ReplayPlayer({ sessionId }: { sessionId: number }) {
|
2026-08-25 07:51:38 +02:00
|
|
|
const scanning = useButtplugStore((s) => s.scanning);
|
|
|
|
|
const devices = useButtplugStore((s) => s.devices);
|
2026-08-25 21:26:18 +02:00
|
|
|
const setActuatorValue = useButtplugStore((s) => s.setActuatorValue);
|
|
|
|
|
const removeDevice = useButtplugStore((s) => s.removeDevice);
|
2026-08-25 07:51:38 +02:00
|
|
|
const connectedDevices = useMemo(() => Object.values(devices), [devices]);
|
|
|
|
|
|
2026-08-27 21:38:51 +02:00
|
|
|
const [data, setData] = useState<ReplayDataResponse | null>(null);
|
2026-08-25 07:51:38 +02:00
|
|
|
const [showRemap, setShowRemap] = useState(false);
|
2026-08-25 21:26:18 +02:00
|
|
|
const [starting, setStarting] = useState(false);
|
2026-08-27 21:38:51 +02:00
|
|
|
const [player, setPlayer] = useState<SessionPlayer | null>(null);
|
2026-08-25 07:51:38 +02:00
|
|
|
const [playing, setPlaying] = useState(false);
|
|
|
|
|
const [elapsedMs, setElapsedMs] = useState(0);
|
2026-08-25 21:26:18 +02:00
|
|
|
const [replayTargets, setReplayTargets] = useState<{ slotLabel: string; deviceName: string }[]>([]);
|
|
|
|
|
const [replayDeviceIndexes, setReplayDeviceIndexes] = useState<number[]>([]);
|
2026-08-25 07:51:38 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-08-27 21:38:51 +02:00
|
|
|
fetch(`/api/sessions/${sessionId}/replay`)
|
2026-08-25 07:51:38 +02:00
|
|
|
.then((r) => r.json())
|
|
|
|
|
.then(setData)
|
2026-08-27 21:38:51 +02:00
|
|
|
.catch(() => toast.error("Could not load session"));
|
|
|
|
|
}, [sessionId]);
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-25 21:26:18 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
|
|
|
|
void disconnectAll();
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
async function resetReplayDevices() {
|
|
|
|
|
await Promise.all(
|
|
|
|
|
replayDeviceIndexes.map(async (deviceIndex) => {
|
|
|
|
|
const liveDevice = await getDevice(deviceIndex);
|
|
|
|
|
await liveDevice?.stop().catch(() => {});
|
|
|
|
|
devices[deviceIndex]?.actuators.forEach((a) => setActuatorValue(deviceIndex, a.featureIndex, 0));
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-25 07:51:38 +02:00
|
|
|
async function handleConfirmRemap(mapping: Map<number, number>) {
|
|
|
|
|
if (!data) return;
|
|
|
|
|
setShowRemap(false);
|
2026-08-25 21:26:18 +02:00
|
|
|
setStarting(true);
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-25 21:26:18 +02:00
|
|
|
try {
|
2026-08-27 21:38:51 +02:00
|
|
|
const orderedSlots = data.deviceSlots.filter((s) => mapping.has(s.sourceSessionDeviceId));
|
2026-08-25 21:26:18 +02:00
|
|
|
const targets = orderedSlots.map((slot) => {
|
2026-08-25 07:51:38 +02:00
|
|
|
const deviceIndex = mapping.get(slot.sourceSessionDeviceId)!;
|
|
|
|
|
const device = connectedDevices.find((d) => d.index === deviceIndex)!;
|
2026-08-25 21:26:18 +02:00
|
|
|
return { slotLabel: slot.slotLabel, deviceName: device.displayName ?? device.name };
|
|
|
|
|
});
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-27 21:38:51 +02:00
|
|
|
// Replaying creates no session or events of its own - it just plays this
|
|
|
|
|
// session's already-recorded events back onto the mapped devices, and
|
|
|
|
|
// marks it as replayed (playCount/lastPlayedAt).
|
|
|
|
|
void fetch(`/api/sessions/${data.session.id}/replay`, { method: "POST" }).catch(() => {});
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-27 21:38:51 +02:00
|
|
|
// `mapping` is already keyed by the source session's session_device id
|
|
|
|
|
// (see DeviceRemapDialog), exactly what SessionPlayer needs.
|
|
|
|
|
const sessionDeviceIdToDeviceIndex = mapping;
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-25 21:26:18 +02:00
|
|
|
const actuatorsByDeviceIndex = new Map<number, ActuatorInfo[]>(
|
|
|
|
|
connectedDevices.map((d) => [d.index, d.actuators]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Reset every actuator on every device that will take part in this replay before
|
|
|
|
|
// the first scheduled event fires, so playback always starts from a known-zero
|
|
|
|
|
// state rather than whatever intensity was left over from manual control.
|
|
|
|
|
const deviceIndexes = new Set(sessionDeviceIdToDeviceIndex.values());
|
|
|
|
|
await Promise.all(
|
|
|
|
|
[...deviceIndexes].map(async (deviceIndex) => {
|
|
|
|
|
const liveDevice = await getDevice(deviceIndex);
|
|
|
|
|
await liveDevice?.stop().catch(() => {});
|
|
|
|
|
actuatorsByDeviceIndex.get(deviceIndex)?.forEach((a) => setActuatorValue(deviceIndex, a.featureIndex, 0));
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { runtime } = await getButtplugClientHandle();
|
2026-08-27 21:38:51 +02:00
|
|
|
const instance = new SessionPlayer({
|
2026-08-25 21:26:18 +02:00
|
|
|
events: data.events,
|
2026-08-27 21:38:51 +02:00
|
|
|
durationMs: data.session.durationMs,
|
2026-08-25 21:26:18 +02:00
|
|
|
sessionDeviceIdToDeviceIndex,
|
|
|
|
|
actuatorsByDeviceIndex,
|
|
|
|
|
runtime,
|
|
|
|
|
onProgress: (elapsed) => setElapsedMs(elapsed),
|
|
|
|
|
onError: (message) => toast.error(`Replay command failed: ${message}`),
|
2026-08-27 21:38:51 +02:00
|
|
|
onComplete: () => {
|
2026-08-25 21:26:18 +02:00
|
|
|
setPlaying(false);
|
|
|
|
|
toast.success("Replay finished");
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
setReplayTargets(targets);
|
|
|
|
|
setReplayDeviceIndexes([...deviceIndexes]);
|
|
|
|
|
setPlayer(instance);
|
|
|
|
|
instance.play();
|
|
|
|
|
setPlaying(true);
|
|
|
|
|
} catch {
|
|
|
|
|
toast.error("Could not start replay");
|
|
|
|
|
} finally {
|
|
|
|
|
setStarting(false);
|
|
|
|
|
}
|
2026-08-25 07:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
2026-08-27 21:38:51 +02:00
|
|
|
return <p className="text-sm text-muted-foreground">Loading session...</p>;
|
2026-08-25 07:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col gap-6">
|
2026-08-31 17:31:57 +02:00
|
|
|
{!player ? (
|
|
|
|
|
<>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
|
|
|
<DeviceScanPanel scanning={scanning} onScan={() => void startScanning()} onStopScan={() => void stopScanning()} />
|
|
|
|
|
<Button
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
try {
|
|
|
|
|
await getButtplugClientHandle();
|
|
|
|
|
setShowRemap(true);
|
|
|
|
|
} catch {
|
|
|
|
|
toast.error("Could not connect to Buttplug client");
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
disabled={connectedDevices.length === 0 || starting}
|
|
|
|
|
>
|
|
|
|
|
{starting ? (
|
|
|
|
|
"Starting..."
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<Play className="size-3.5" /> Match & replay
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{connectedDevices.length === 0 && (
|
2026-09-01 19:02:32 +02:00
|
|
|
<Alert className="bp-glass">
|
|
|
|
|
<Info />
|
2026-09-01 19:06:28 +02:00
|
|
|
<AlertTitle>No devices connected yet</AlertTitle>
|
2026-09-01 19:02:32 +02:00
|
|
|
<AlertDescription>
|
2026-09-01 19:06:28 +02:00
|
|
|
Scan to discover nearby toys, then match them to this session's original device slots.
|
2026-09-01 19:02:32 +02:00
|
|
|
</AlertDescription>
|
|
|
|
|
</Alert>
|
2026-08-31 17:31:57 +02:00
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<Card className="bp-glass">
|
2026-08-31 19:25:51 +02:00
|
|
|
<CardContent className="flex flex-col gap-3">
|
2026-08-31 17:31:57 +02:00
|
|
|
{replayTargets.length > 0 && (
|
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
{replayTargets.map((t) => (
|
|
|
|
|
<span
|
|
|
|
|
key={t.slotLabel}
|
|
|
|
|
className="bp-readout rounded-md border border-border bg-card/60 px-2 py-1 text-xs text-muted-foreground"
|
|
|
|
|
>
|
|
|
|
|
{t.slotLabel} → {t.deviceName}
|
|
|
|
|
</span>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<Slider
|
|
|
|
|
value={[elapsedMs]}
|
|
|
|
|
max={data.session.durationMs}
|
|
|
|
|
onValueChange={([v]) => player.seek(v)}
|
|
|
|
|
/>
|
|
|
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
|
|
|
<span className="bp-readout text-xs text-muted-foreground">
|
|
|
|
|
{formatTime(elapsedMs)} / {formatTime(data.session.durationMs)}
|
|
|
|
|
</span>
|
|
|
|
|
<div className="flex items-center gap-2">
|
2026-08-25 07:51:38 +02:00
|
|
|
<Button
|
2026-08-31 17:31:57 +02:00
|
|
|
size="icon"
|
|
|
|
|
variant="outline"
|
2026-08-25 07:51:38 +02:00
|
|
|
onClick={async () => {
|
2026-08-31 17:31:57 +02:00
|
|
|
if (playing) {
|
|
|
|
|
player.pause();
|
|
|
|
|
setPlaying(false);
|
|
|
|
|
} else {
|
|
|
|
|
player.play();
|
|
|
|
|
setPlaying(true);
|
2026-08-25 21:26:18 +02:00
|
|
|
}
|
2026-08-31 17:31:57 +02:00
|
|
|
// Toggling either way leaves the toy holding whatever intensity was
|
|
|
|
|
// last sent - zero it out so pause always actually stops the device,
|
|
|
|
|
// and resume always starts from a clean, known state.
|
|
|
|
|
await resetReplayDevices();
|
2026-08-25 07:51:38 +02:00
|
|
|
}}
|
2026-08-31 17:31:57 +02:00
|
|
|
aria-label={playing ? "Pause" : "Play"}
|
2026-08-25 07:51:38 +02:00
|
|
|
>
|
2026-08-31 17:31:57 +02:00
|
|
|
{playing ? <Pause className="size-4" /> : <Play className="size-4" />}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
player.stop();
|
|
|
|
|
setPlaying(false);
|
|
|
|
|
setElapsedMs(0);
|
|
|
|
|
// Fully disconnect every device that took part in this replay,
|
|
|
|
|
// not just stop the player - see handleDisconnectDevice in
|
|
|
|
|
// ButtplugConsole for why "stop + remove from store" is the
|
|
|
|
|
// closest equivalent Buttplug's protocol allows per-device.
|
|
|
|
|
await Promise.all(
|
|
|
|
|
replayDeviceIndexes.map(async (deviceIndex) => {
|
|
|
|
|
const liveDevice = await getDevice(deviceIndex);
|
|
|
|
|
await liveDevice?.stop().catch(() => {});
|
|
|
|
|
removeDevice(deviceIndex);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
setPlayer(null);
|
|
|
|
|
setReplayTargets([]);
|
|
|
|
|
setReplayDeviceIndexes([]);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Stop
|
2026-08-25 07:51:38 +02:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-08-31 17:31:57 +02:00
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
)}
|
2026-08-25 07:51:38 +02:00
|
|
|
|
|
|
|
|
<DeviceRemapDialog
|
|
|
|
|
open={showRemap}
|
2026-08-27 21:38:51 +02:00
|
|
|
deviceSlots={data.deviceSlots}
|
2026-08-25 07:51:38 +02:00
|
|
|
connectedDevices={connectedDevices}
|
|
|
|
|
onCancel={() => setShowRemap(false)}
|
|
|
|
|
onConfirm={(mapping) => void handleConfirmRemap(mapping)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|