Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 66 additions & 56 deletions src/WebhookDisplay/WebhookList.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { forwardWebhookToLocalhost } from "../forward-to-localhost";
import posthog from "posthog-js";
import { WebhookStoreUrlContext } from "../WebhookStoreUrl/WebhookStoreUrl.context";
import { UpdateQueryFn } from "@apollo/client/core/watchQueryOptions";

const largePayloadCellStyle: React.CSSProperties = {
minWidth: 200,
Expand Down Expand Up @@ -45,62 +46,8 @@ type QueryWebhook = {
webhooks: Array<Webhook> | null;
};

const COMMENTS_SUBSCRIPTION = gql`
subscription WebhookAdded {
webhookAdded {
id
path
body
headers
receivedAt
}
}
`;

type SubscriptionWebhook = {
webhookAdded: Webhook;
};

const WebhookList: React.FC = () => {
const path = useMemo(
() =>
window.location.pathname === "/" ? undefined : window.location.pathname,
[window.location.pathname]
);
const { data, subscribeToMore } = useQuery<QueryWebhook>(QUERY_WEBHOOKS, {
variables: { first: 100, path },
});
const baseUrl = "http://localhost:8010/proxy";
const { value: webhookStoreUrl } = useContext(WebhookStoreUrlContext);

useEffect(() => {
const unsuscribe = subscribeToMore<SubscriptionWebhook>({
document: COMMENTS_SUBSCRIPTION,
updateQuery: (prev, { subscriptionData }): QueryWebhook => {
if (!subscriptionData.data) {
return prev;
}
if (path && subscriptionData.data.webhookAdded.path != path) {
return prev;
}
const newWebhook = subscriptionData.data.webhookAdded;
try {
void forwardWebhookToLocalhost(baseUrl, newWebhook);
} catch (err) {
console.error(err);
}
posthog.capture("New webhook received", { webhookId: newWebhook.id });
return Object.assign({}, prev, {
webhooks: [newWebhook, ...(prev.webhooks || [])],
});
},
});
return () => {
unsuscribe();
};
}, [subscribeToMore]);

const columns = React.useMemo<Column<Webhook>[]>(
const useBuildColumns = (data: QueryWebhook | undefined) =>
React.useMemo<Column<Webhook>[]>(
() => [
{
Header: "Id",
Expand Down Expand Up @@ -131,6 +78,9 @@ const WebhookList: React.FC = () => {
[data]
);

const useBuildTable = (data: QueryWebhook | undefined) => {
const columns = useBuildColumns(data);

const orderedWebhooks = useMemo(() => {
return (
data?.webhooks?.slice().sort((a, b) => {
Expand All @@ -154,6 +104,66 @@ const WebhookList: React.FC = () => {
usePagination
);

return { table, orderedWebhooks };
};

const subscribeToMoreUpdateQuery =
(path: string | undefined): UpdateQueryFn =>
(prev, { subscriptionData }): QueryWebhook => {
const baseUrl = "http://localhost:8010/proxy";
if (!subscriptionData.data) {
return prev;
}
if (path && subscriptionData.data.webhookAdded.path != path) {
return prev;
}
const newWebhook = subscriptionData.data.webhookAdded;
try {
void forwardWebhookToLocalhost(baseUrl, newWebhook);
} catch (err) {
console.error(err);
}
posthog.capture("New webhook received", { webhookId: newWebhook.id });
return Object.assign({}, prev, {
webhooks: [newWebhook, ...(prev.webhooks || [])],
});
};
const COMMENTS_SUBSCRIPTION = gql`
subscription WebhookAdded {
webhookAdded {
id
path
body
headers
receivedAt
}
}
`;

type SubscriptionWebhook = {
webhookAdded: Webhook;
};

const WebhookList: React.FC = () => {
const path = useMemo(
() =>
window.location.pathname === "/" ? undefined : window.location.pathname,
[window.location.pathname]
);
const { data, subscribeToMore } = useQuery<QueryWebhook>(QUERY_WEBHOOKS, {
variables: { first: 100, path },
});
const { value: webhookStoreUrl } = useContext(WebhookStoreUrlContext);
useEffect(() => {
const unsuscribe = subscribeToMore<SubscriptionWebhook>({
document: COMMENTS_SUBSCRIPTION,
updateQuery: subscribeToMoreUpdateQuery(path),
});
return () => {
unsuscribe();
};
}, [subscribeToMore]);
const { table, orderedWebhooks } = useBuildTable(data);
return orderedWebhooks && orderedWebhooks.length > 0 ? (
<WebhookPage webhooks={orderedWebhooks} table={table} />
) : (
Expand Down