import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
Input,
Button,
useIsMobile,
} from "@tensamin/ui";
import z from "zod";
import { useTTP } from "@tensamin/ttp";
import { useState } from "react";
import { Loader2 } from "lucide-react";
import { isTauri } from "@tauri-apps/api/core";
import { useStorage } from "@tensamin/storage/context";
import { useSession } from "@tensamin/storage/session";
// The page
export default function Page() {
const { clear } = useStorage();
const isMobile = useIsMobile();
return (
);
}
// Add Conversation Button Component
function AddConversationButton() {
const { send } = useTTP();
const { contacts, insertContact } = useSession();
const [loading, setLoading] = useState(false);
const [open, setOpen] = useState(false);
const [error, setError] = useState(null);
async function submit(username: string | null) {
if (loading) return;
setError(null);
// username check
const schema = z
.string()
.min(1, "Username is too short")
.max(15, "Username is too long");
const result = schema.safeParse(username?.toLowerCase().trim());
if (!result.success) {
setError(result.error.issues[0].message);
return;
}
// user existence check
const user = await send("get_user_data", {
username: result.data,
})
.then((data) => {
if (data.data.user_id === 0) {
throw new Error();
}
return data;
})
.catch(() => {
setError("User not found");
return;
});
if (!user) return;
// alrady added check
if (contacts.some((contact) => contact.user_id === user.data.user_id)) {
setError("Conversation already exists");
return;
}
// add the conv
const timeout = setTimeout(() => setLoading(true), 500);
send("add_conversation", {
chat_partner_name: result.data,
})
.then(() => {
insertContact(user.data.user_id);
setOpen(false);
})
.catch((error) => {
if (String(error).includes("error_not_found")) {
setError("User not found");
return;
}
setError(String(error));
})
.finally(() => {
clearTimeout(timeout);
setLoading(false);
});
}
return (
);
}