Files
k-tv/k-tv-frontend/hooks/use-channel-order.ts
Gabriel Kaszewski c4d2e48f73 fix(frontend): resolve all eslint warnings and errors
- block-timeline: ref updates moved to useLayoutEffect
- channel-card, guide/page: Date.now() wrapped in useMemo + suppress purity rule
- auth-context: lazy localStorage init (removes setState-in-effect)
- use-channel-order: lazy localStorage init (removes setState-in-effect)
- use-idle: start timer on mount without calling resetIdle (removes setState-in-effect)
- use-subtitles, transcode-settings-dialog: inline eslint-disable on exact violating line
- providers: block-level eslint-disable for tokenRef closure in useState initializer
- edit-channel-sheet: remove unused minsToTime and BlockContent imports
- docs/page: escape unescaped quote and apostrophe entities
2026-03-17 02:40:32 +01:00

54 lines
1.5 KiB
TypeScript

"use client";
import { useState } from "react";
import type { ChannelResponse } from "@/lib/types";
export function useChannelOrder(channels: ChannelResponse[] | undefined) {
const [channelOrder, setChannelOrder] = useState<string[]>(() => {
try {
const stored = localStorage.getItem("k-tv-channel-order");
return stored ? JSON.parse(stored) : [];
} catch {
return [];
}
});
const saveOrder = (order: string[]) => {
setChannelOrder(order);
try {
localStorage.setItem("k-tv-channel-order", JSON.stringify(order));
} catch {}
};
const sortedChannels = channels
? [...channels].sort((a, b) => {
const ai = channelOrder.indexOf(a.id);
const bi = channelOrder.indexOf(b.id);
if (ai === -1 && bi === -1) return 0;
if (ai === -1) return 1;
if (bi === -1) return -1;
return ai - bi;
})
: [];
const handleMoveUp = (channelId: string) => {
const ids = sortedChannels.map((c) => c.id);
const idx = ids.indexOf(channelId);
if (idx <= 0) return;
const next = [...ids];
[next[idx - 1], next[idx]] = [next[idx], next[idx - 1]];
saveOrder(next);
};
const handleMoveDown = (channelId: string) => {
const ids = sortedChannels.map((c) => c.id);
const idx = ids.indexOf(channelId);
if (idx === -1 || idx >= ids.length - 1) return;
const next = [...ids];
[next[idx], next[idx + 1]] = [next[idx + 1], next[idx]];
saveOrder(next);
};
return { channelOrder, sortedChannels, handleMoveUp, handleMoveDown };
}