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
This commit is contained in:
74
k-tv-frontend/hooks/use-channel-passwords.ts
Normal file
74
k-tv-frontend/hooks/use-channel-passwords.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
"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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user