import { ChangeEvent, useEffect, useState } from "react"; import toast from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { useChangeChannelTypeMutation, useGetChannelQuery, useUpdateChannelMutation, useUpdateIconMutation } from "@/app/services/channel"; import { useAppSelector } from "@/app/store"; import { Channel } from "@/types/channel"; import AvatarUploader from "@/components/AvatarUploader"; import SaveTip from "@/components/SaveTip"; import Input from "@/components/styled/Input"; import Label from "@/components/styled/Label"; import Radio from "@/components/styled/Radio"; import Textarea from "@/components/styled/Textarea"; import IconChannel from "@/assets/icons/channel.svg"; import { shallowEqual } from "react-redux"; export default function Overview({ id = 0 }) { const { t } = useTranslation("setting", { keyPrefix: "channel" }); const { t: ct } = useTranslation(); const loginUser = useAppSelector((store) => store.authData.user, shallowEqual); const channel = useAppSelector((store) => store.channels.byId[id], shallowEqual); const { data, refetch } = useGetChannelQuery(id); const [changed, setChanged] = useState(false); const [values, setValues] = useState(); const [updateChannelIcon] = useUpdateIconMutation(); const [updateChannel, { isSuccess: updated }] = useUpdateChannelMutation(); const [changeChannelType, { isSuccess: changeTypeSuccess }] = useChangeChannelTypeMutation(); const handleUpdate = () => { if (!values) return; const { name, description } = values; updateChannel({ id, name, description }); }; const handleChange = (evt: ChangeEvent) => { const newValue = evt.target.value; const { type = "" } = evt.target.dataset; setValues((prev) => { if (!prev) return prev; return { ...prev, [type]: newValue }; }); }; const updateIcon = (image: File) => { updateChannelIcon({ gid: id, image }); }; const handleReset = () => { setValues(data); }; useEffect(() => { if (data) { setValues(data); } }, [data]); useEffect(() => { if (data && values) { const { name, description } = values; const { name: oName, description: oDescription } = data; if (oName !== name || oDescription !== description) { setChanged(true); } else { setChanged(false); } } }, [data, values]); useEffect(() => { if (updated) { toast.success(ct("tip.update")); refetch(); } }, [updated]); useEffect(() => { if (changeTypeSuccess) { toast.success(ct("tip.update")); } }, [changeTypeSuccess]); if (!values || !id || !channel) return null; const { name, description } = values; const readOnly = !loginUser?.is_admin && channel?.owner != loginUser?.uid; const inputClass = `w-full flex flex-col items-start gap-2 relative`; return (
#{id}