"use client";
import { useEffect, useMemo, useRef, useState } from "react";
import { ClientTableConfig, ServiceTableConfig } from "@/config/client_1";
import CustomTable from "@/components/table";
import {
Alert,
Button,
Card,
CardContent,
CardHeader,
Snackbar,
Typography,
CircularProgress,
IconButton,
} from "@mui/material";
import CopyToClipboard from "@/components/copy_to_clipboard";
import {
attachEntity,
detachEntity,
isAttached,
} from "@/api/entities/entities";
import { mutate } from "swr";
import { Skeleton } from "@mui/material";
import { Entity, Service } from "@/api/model";
import useGetEntityByNameOrDid from "@/components/hooks/useGetEntityByNameOrDid";
import { useGetAllServices } from "@/api/services/services";
import axios from "axios";
import CloseIcon from "@mui/icons-material/Close";
interface SnackMessage {
message: string;
severity: "success" | "error";
}
type AttachButtonProps = {
entity?: Entity;
setSnackbarMessage: (message: SnackMessage) => void;
setSnackbarOpen: (open: boolean) => void;
};
const AttachButton = ({
entity,
setSnackbarMessage,
setSnackbarOpen,
}: AttachButtonProps) => {
const [loading, setLoading] = useState(false);
const handleClick = async () => {
setLoading(true);
// Call the attach or detach function depending on the isAttached value
// and await for the result
try {
let response = await (entity?.attached
? detachEntity({ entity_did: entity?.did })
: attachEntity({ entity_did: entity?.did }));
if (!entity?.attached) {
console.log("calling isAttached");
response = await isAttached({ entity_did: entity?.did });
console.log("response: ", response);
}
const msg = {
message: response.data.message,
severity: "success",
} as SnackMessage;
setSnackbarMessage(msg);
setSnackbarOpen(true);
} catch (error) {
if (axios.isAxiosError(error)) {
// Extract the error message from the error object
const errorMessage = error.response?.data.detail[0].msg;
const msg = {
message: `${errorMessage}`,
severity: "error",
} as SnackMessage;
setSnackbarMessage(msg);
setSnackbarOpen(true);
} else {
console.error("error: ", error);
}
} finally {
setLoading(false);
}
};
return (
<>
>
);
};
export default function Client({ params }: { params: { name: string } }) {
const { name } = params;
const { entity: entity } = useGetEntityByNameOrDid(name);
const {
data: services,
isLoading: services_loading,
swrKey: entityKeyFunc,
} = useGetAllServices();
const clients: Service[] = useMemo(() => {
if (services?.data) {
return services.data.filter((service) => {
if (service.entity_did !== entity?.did) return true;
});
}
return [];
}, [services, entity?.did]);
const onRefresh = () => {
const entityKey =
typeof entityKeyFunc === "function" ? entityKeyFunc() : entityKeyFunc;
if (entityKey) mutate(entityKey);
};
useEffect(() => {
const interval = setInterval(() => {
onRefresh();
}, 5000);
return () => clearInterval(interval);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const cardContentRef = useRef(null);
const [snackbarOpen, setSnackbarOpen] = useState(false);
const [snackbarMessage, setSnackbarMessage] = useState<
SnackMessage | undefined
>(undefined);
const closeSnackBar = () => {
setSnackbarMessage(undefined);
setSnackbarOpen(false);
};
console.log("entity", entity);
if (services_loading) return
{entity?.did}
{entity?.ip}
{entity?.network}