import { PlusIcon } from "lucide-react"; import { useState, type FormEvent } from "react"; import { cn } from "../../utils/classname"; import StateSection from "./sections/state"; import CommandSection from "./sections/command"; import { useCreateTopic } from "./hooks/mutations"; import { getMerchant } from "../../utils/storage"; import { ENV } from "../../constants/env"; export default function TopicsFeature() { const [tab, setTab] = useState("state"); const [isOpen, setIsOpen] = useState(false); const [form, setForm] = useState({ topic: "", type: "state-reply", }); const [error, setError] = useState(""); const createTopic = useCreateTopic(); const merchant = getMerchant(); const expectedCredential = ENV.credential?.trim(); const hasCredential = Boolean(merchant?.credential?.trim()); const isCredentialMatch = Boolean(expectedCredential) && merchant?.credential?.trim() === expectedCredential; const isMerchantSetup = Boolean(merchant?.merchantName?.trim()) && Boolean(merchant?.tower?.trim()) && Boolean(merchant?.floor?.trim()) && Boolean(merchant?.unit?.trim()); const isReady = isMerchantSetup && hasCredential && isCredentialMatch; const handleOpen = () => { setError(""); setForm({ topic: "", type: tab === "state" ? "state-reply" : "commands", }); setIsOpen(true); }; const handleClose = () => { if (createTopic.isPending) return; setIsOpen(false); }; const handleSubmit = (event: FormEvent) => { event.preventDefault(); const topic = form.topic.trim(); if (!topic) { setError("Topic wajib diisi."); return; } createTopic.mutate( { topic, type: form.type }, { onSuccess: () => { setIsOpen(false); setForm({ topic: "", type: tab === "state" ? "state-reply" : "commands", }); }, onError: () => { setError("Gagal menambah topic."); }, }, ); }; return (
{!isReady && (
{!isMerchantSetup && ( Silakan setup merchant terlebih dahulu di halaman Settings sebelum mengakses Topics. )} {isMerchantSetup && !hasCredential && ( Silakan isi credential terlebih dahulu di halaman Settings sebelum mengakses Topics. )} {isMerchantSetup && hasCredential && !isCredentialMatch && ( Credential tidak sesuai. )}
)}
{tab === "state" && } {tab === "command" && } {isOpen && (

Tambah Topic

{error && (

{error}

)}
)}
); }