Files
k-tv/k-tv-frontend/hooks/use-channel-passwords.ts
Gabriel Kaszewski 8ed8da2d90 refactor(frontend): extract logic to hooks, split inline components
Area 1 (tv/page.tsx 964→423 lines):
- hooks: use-fullscreen, use-idle, use-volume, use-quality, use-subtitles,
  use-channel-input, use-channel-passwords, use-tv-keyboard
- components: SubtitlePicker, VolumeControl, QualityPicker, TopControlBar,
  LogoWatermark, AutoplayPrompt, ChannelNumberOverlay, TvBaseLayer

Area 2 (edit-channel-sheet.tsx 1244→678 lines):
- hooks: use-channel-form (all form state + reset logic)
- lib/schemas.ts: extracted Zod schemas + extractErrors
- components: AlgorithmicFilterEditor, RecyclePolicyEditor, WebhookEditor,
  AccessSettingsEditor, LogoEditor

Area 3 (dashboard/page.tsx 406→261 lines):
- hooks: use-channel-order, use-import-channel, use-regenerate-all
- lib/channel-export.ts: pure export utility
- components: DashboardHeader
2026-03-17 02:25:02 +01:00

75 lines
2.4 KiB
TypeScript

"use client";
import { useState, useCallback } from "react";
import { useQueryClient } from "@tanstack/react-query";
export function useChannelPasswords(channelId?: string) {
const queryClient = useQueryClient();
const [channelPasswords, setChannelPasswords] = useState<Record<string, string>>(() => {
try {
return JSON.parse(localStorage.getItem("channel_passwords") ?? "{}");
} catch {
return {};
}
});
const [blockPasswords, setBlockPasswords] = useState<Record<string, string>>(() => {
try {
return JSON.parse(localStorage.getItem("block_passwords") ?? "{}");
} catch {
return {};
}
});
const [showChannelPasswordModal, setShowChannelPasswordModal] = useState(false);
const [showBlockPasswordModal, setShowBlockPasswordModal] = useState(false);
// channelPassword only depends on channelId — available immediately from localStorage
const channelPassword = channelId ? channelPasswords[channelId] : undefined;
// blockPassword is derived lazily by the caller (needs broadcast.slot.id)
const getBlockPassword = useCallback(
(slotId?: string) => (slotId ? blockPasswords[slotId] : undefined),
[blockPasswords],
);
const submitChannelPassword = useCallback(
(password: string) => {
if (!channelId) return;
const next = { ...channelPasswords, [channelId]: password };
setChannelPasswords(next);
try {
localStorage.setItem("channel_passwords", JSON.stringify(next));
} catch {}
setShowChannelPasswordModal(false);
queryClient.invalidateQueries({ queryKey: ["broadcast", channelId] });
},
[channelId, channelPasswords, queryClient],
);
const submitBlockPassword = useCallback(
(slotId: string, channelIdForQuery: string | undefined, password: string) => {
const next = { ...blockPasswords, [slotId]: password };
setBlockPasswords(next);
try {
localStorage.setItem("block_passwords", JSON.stringify(next));
} catch {}
setShowBlockPasswordModal(false);
queryClient.invalidateQueries({
queryKey: ["stream-url", channelIdForQuery, slotId],
});
},
[blockPasswords, queryClient],
);
return {
channelPassword,
getBlockPassword,
showChannelPasswordModal,
setShowChannelPasswordModal,
showBlockPasswordModal,
setShowBlockPasswordModal,
submitChannelPassword,
submitBlockPassword,
};
}