import {
Table,
TableBody,
TableCell,
TableHeader,
TableRow,
} from "@/components/ui/table.tsx";
import { Badge } from "@/components/ui/badge.tsx";
import { Check, Plus, RotateCw, Settings2, Trash, X } from "lucide-react";
import { Button } from "@/components/ui/button.tsx";
import OperationAction from "@/components/OperationAction.tsx";
import { useEffect, useMemo, useState } from "react";
import { Channel, getChannelType } from "@/admin/channel.ts";
import { toastState } from "@/admin/utils.ts";
import { useTranslation } from "react-i18next";
import { useEffectAsync } from "@/utils/hook.ts";
import {
activateChannel,
deactivateChannel,
deleteChannel,
listChannel,
} from "@/admin/api/channel.ts";
import { useToast } from "@/components/ui/use-toast.ts";
import { cn } from "@/components/ui/lib/utils.ts";
type ChannelTableProps = {
display: boolean;
setId: (id: number) => void;
setEnabled: (enabled: boolean) => void;
};
type TypeBadgeProps = {
type: string;
};
function TypeBadge({ type }: TypeBadgeProps) {
const content = useMemo(() => getChannelType(type), [type]);
return (
{content || type}
);
}
function ChannelTable({ display, setId, setEnabled }: ChannelTableProps) {
const { t } = useTranslation();
const { toast } = useToast();
const [data, setData] = useState([]);
const [loading, setLoading] = useState(false);
const refresh = async () => {
setLoading(true);
const resp = await listChannel();
setLoading(false);
if (!resp.status) toastState(toast, t, resp);
else setData(resp.data);
};
useEffectAsync(refresh, []);
useEffectAsync(refresh, [display]);
useEffect(() => {
if (display) setId(-1);
}, [display]);
return (
display && (
{t("admin.channels.id")}
{t("admin.channels.name")}
{t("admin.channels.type")}
{t("admin.channels.priority")}
{t("admin.channels.weight")}
{t("admin.channels.state")}
{t("admin.channels.action")}
{(data || []).map((chan, idx) => (
{chan.id}
{chan.name}
{chan.priority}
{chan.weight}
{chan.state ? (
) : (
)}
{
setEnabled(true);
setId(chan.id);
}}
>
{chan.state ? (
{
const resp = await deactivateChannel(chan.id);
toastState(toast, t, resp, true);
await refresh();
}}
>
) : (
{
const resp = await activateChannel(chan.id);
toastState(toast, t, resp, true);
await refresh();
}}
>
)}
{
const resp = await deleteChannel(chan.id);
toastState(toast, t, resp, true);
await refresh();
}}
>
))}
)
);
}
export default ChannelTable;