From 6d197ea19fe0761d1b185760708119d8dfe15767 Mon Sep 17 00:00:00 2001 From: Marcus Forsberg Date: Wed, 27 Sep 2023 14:52:35 +0200 Subject: [PATCH] feat: added empty states for when there are no webhooks or webhook attempts (#1) * refactor: adjust some linting rules and fix some lint issues * feat: added empty states for when there are no webhooks or webhook attempts --- .eslintrc | 7 ++++++- src/components/Webhook.tsx | 13 ++++++++++--- src/components/WebhookAttempt.tsx | 10 +++------- src/components/Webhooks.tsx | 25 ++++++++++++++++--------- src/webhooks.tsx | 8 ++++++-- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/.eslintrc b/.eslintrc index b737fae..91dbdf7 100644 --- a/.eslintrc +++ b/.eslintrc @@ -11,5 +11,10 @@ "plugin:react-hooks/recommended", "plugin:prettier/recommended", "plugin:react/jsx-runtime" - ] + ], + "rules": { + "react/prop-types": "off", + "react/require-default-props": "off", + "no-nested-ternary": "off" + } } diff --git a/src/components/Webhook.tsx b/src/components/Webhook.tsx index 0e019ce..fca9f03 100644 --- a/src/components/Webhook.tsx +++ b/src/components/Webhook.tsx @@ -1,4 +1,5 @@ -import { Flex, Spinner, Stack } from '@sanity/ui'; +import { Card, Flex, Spinner, Stack } from '@sanity/ui'; +import { FC } from 'react'; import { useClient, useProjectId } from 'sanity'; import useSWR, { type SWRResponse } from 'swr'; import type { SanityWebhookAttempt } from '../types/SanityWebhookAttempt'; @@ -7,8 +8,8 @@ import { WebhookAttempt } from './WebhookAttempt'; export interface WebhookProps { webhookId: string; - refreshInterval?: number; - webhookBodyComponent?: React.FC; + refreshInterval: number | undefined; + webhookBodyComponent: FC; } export function Webhook({ @@ -59,6 +60,12 @@ export function Webhook({ )} + {data && data.length === 0 && ( + + Found no events. + + )} + {data?.map((row) => ( ; + webhookBodyComponent: FC; } export function WebhookAttempt({ attempt, - webhookBodyComponent + webhookBodyComponent: WebhookBody }: WebhookAttemptProps) { const [showResponse, setShowResponse] = useState(false); const formatDistanceLocale: { [s: string]: string } = { @@ -39,9 +38,6 @@ export function WebhookAttempt({ xHours: '{{count}}h' }; - // Injectable components - const WebhookBody = webhookBodyComponent || DefaultWebhookBody; - return ( diff --git a/src/components/Webhooks.tsx b/src/components/Webhooks.tsx index 753fe82..2b156dc 100644 --- a/src/components/Webhooks.tsx +++ b/src/components/Webhooks.tsx @@ -1,5 +1,5 @@ import { Badge, Card, Flex, Heading, Inline, Stack, Text } from '@sanity/ui'; -import { useEffect, useState } from 'react'; +import { FC, useEffect, useState } from 'react'; import { useClient, useDataset, useProjectId } from 'sanity'; import { Link, useRouter } from 'sanity/router'; import styled from 'styled-components'; @@ -24,8 +24,8 @@ const WebhooksList = styled.div` `; export interface WebhooksProps { - refreshInterval?: number; - webhookBodyComponent?: React.FC; + refreshInterval: number | undefined; + webhookBodyComponent: FC; } export function Webhooks({ @@ -47,12 +47,13 @@ export function Webhooks({ }) ); - const webhooks = webhooksResponse?.data?.filter( - (webhook) => - webhook.dataset === '*' || - (Array.isArray(webhook.dataset) && webhook.dataset.includes(dataset)) || - webhook.dataset === dataset - ); + const webhooks = + webhooksResponse?.data?.filter( + (webhook) => + webhook.dataset === '*' || + (Array.isArray(webhook.dataset) && webhook.dataset.includes(dataset)) || + webhook.dataset === dataset + ) || []; useEffect(() => { if (router.state) { @@ -73,6 +74,12 @@ export function Webhooks({ + {webhooks.length === 0 && ( + + No webhooks are configured for the current dataset. + + )} + {webhooks?.map((webhook) => ( ; + webhookBodyComponent?: FC; } /** @@ -49,7 +51,9 @@ export const webhooks = definePlugin( ? undefined : config?.refreshInterval || 10000 } - webhookBodyComponent={config?.webhookBodyComponent} + webhookBodyComponent={ + config?.webhookBodyComponent || DefaultWebhookBody + } /> ) }