fix: persist auto-play setting

This commit is contained in:
talksik
2026-03-25 16:03:14 -07:00
parent 60daec38ff
commit f3ddceecfc
3 changed files with 72 additions and 12 deletions
+21 -1
View File
@@ -3,6 +3,24 @@ import type { Particle } from "@/api/types";
type MediaParticle = Extract<Particle, { type: "media" }>;
const STORAGE_KEY = "llink:autoplay-muted";
function loadMuted(): boolean {
try {
return localStorage.getItem(STORAGE_KEY) === "true";
} catch {
return false;
}
}
function saveMuted(muted: boolean) {
try {
localStorage.setItem(STORAGE_KEY, String(muted));
} catch {
// Storage unavailable
}
}
interface AutoplayState {
/** The media particle currently being autoplayed, null when idle */
activeParticle: MediaParticle | null;
@@ -21,7 +39,7 @@ interface AutoplayState {
export const useAutoplayStore = create<AutoplayState>((set, get) => ({
activeParticle: null,
streamId: null,
muted: false,
muted: loadMuted(),
play: (particle, streamId) => {
if (get().muted) return;
set({ activeParticle: particle, streamId });
@@ -32,8 +50,10 @@ export const useAutoplayStore = create<AutoplayState>((set, get) => ({
// If muting, also stop any current playback
if (!wasMuted) {
set({ muted: true, activeParticle: null, streamId: null });
saveMuted(true);
} else {
set({ muted: false });
saveMuted(false);
}
},
}));