import { useState, useRef } from "react"; import { format, formatRelative } from "date-fns"; import CopyIcon from "@src/assets/icons/Copy"; import { Stack, ButtonBase, List, ListItem, ListItemText, Avatar, Button, IconButton, Menu, MenuItem, Switch, Tooltip, Typography, } from "@mui/material"; import AddIcon from "@mui/icons-material/Add"; import WebhookIcon from "@src/assets/icons/Webhook"; import LogsIcon from "@src/assets/icons/CloudLogs"; import EditIcon from "@mui/icons-material/EditOutlined"; import DeleteIcon from "@mui/icons-material/DeleteOutlined"; import EmptyState from "@src/components/EmptyState"; import { webhookTypes, webhookNames, IWebhook, WebhookType } from "./utils"; import { DATE_TIME_FORMAT } from "@src/constants/dates"; import { useProjectContext } from "@src/contexts/ProjectContext"; export interface IWebhookListProps { webhooks: IWebhook[]; handleAddWebhook: (type: WebhookType) => void; handleUpdateActive: (index: number, active: boolean) => void; handleOpenLogs: (index: number) => void; handleEdit: (index: number) => void; handleDelete: (index: number) => void; } export default function WebhookList({ webhooks, handleAddWebhook, handleUpdateActive, handleOpenLogs, handleEdit, handleDelete, }: IWebhookListProps) { const { settings, tableState } = useProjectContext(); const [anchorEl, setAnchorEl] = useState(null); const addButtonRef = useRef(null); const activeWebhookCount = webhooks.filter( (webhook) => webhook.active ).length; const handleAddButton = () => { setAnchorEl(addButtonRef.current); }; const handleChooseAddType = (type: WebhookType) => { handleClose(); handleAddWebhook(type); }; const handleClose = () => { setAnchorEl(null); }; const baseUrl = `${settings?.rowyRunUrl}/whs/${tableState?.tablePath}/`; return ( <> Webhooks ({activeWebhookCount} / {webhooks.length}) {webhookTypes.map((type) => ( handleChooseAddType(type)}> {webhookNames[type]} ))} {webhooks.length === 0 ? ( ) : ( {webhooks.map((webhook, index) => ( {webhook.name} {webhookNames[webhook.type]} } secondary={
{baseUrl} {webhook.endpoint}
navigator.clipboard.writeText( `${baseUrl}${webhook.endpoint}` ) } >
} //secondary={webhookNames[webhook.type]} primaryTypographyProps={{ style: { minHeight: 40, display: "flex", alignItems: "center", }, }} /> } secondaryAction={ handleUpdateActive(index, !webhook.active) } inputProps={{ "aria-label": "Activate" }} sx={{ mr: 1 }} /> handleOpenLogs(index)} > handleEdit(index)} > handleDelete(index)} sx={{ "&&": { mr: -1.5 } }} > Last updated
by {webhook.lastEditor.displayName}
at{" "} {format( webhook.lastEditor.lastUpdate, DATE_TIME_FORMAT )} } > {formatRelative( webhook.lastEditor.lastUpdate, new Date() )}
} /> ))}
)} ); }