Skip to content

Commit

Permalink
replace polling call for item counts and group pinboards, to instead …
Browse files Browse the repository at this point in the history
…be triggered whenever we hear about a new/mutated item on any pinboard
  • Loading branch information
twrichards committed Nov 25, 2024
1 parent 23bb039 commit 6f274b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
8 changes: 6 additions & 2 deletions client/src/fronts/frontsIntegration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export const FrontsIntegration = ({
[frontsPinboardElements]
);

const { setError } = useGlobalStateContext();
const { setError, totalItemsReceivedViaSubscription } =
useGlobalStateContext();

const apolloClient = useApolloClient();

Expand Down Expand Up @@ -84,9 +85,12 @@ export const FrontsIntegration = ({
variables: {
pinboardIds,
},
pollInterval: 10_000, //TODO consider subscribing to all messages and fetching counts based on that, might be more efficient
});

useEffect(() => {
itemCountsQuery.refetch();
}, [totalItemsReceivedViaSubscription]);

useEffect(() => {
itemCountsQuery.data?.getItemCounts &&
setMaybeItemCountsLookup(
Expand Down
20 changes: 12 additions & 8 deletions client/src/globalState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ interface GlobalStateContextShape {
allSubscriptionItems: Item[];
allSubscriptionClaimedItems: Item[]; // both the updated 'claimed' item and the new 'claim' item
allSubscriptionOnSeenItems: LastItemSeenByUser[];
totalItemsReceivedViaSubscription: number;

payloadToBeSent: PayloadAndType | null;
setPayloadToBeSent: (newPayloadToBeSent: PayloadAndType | null) => void;
Expand Down Expand Up @@ -288,9 +289,15 @@ export const GlobalStateProvider = ({
? [demoPinboardData]
: pinboardDataQuery.data?.getPinboardsByIds || [];

const [
totalItemsReceivedViaSubscription,
setTotalItemsReceivedViaSubscription,
] = useState(0);

const [allSubscriptionItems, setAllSubscriptionItems] = useState<Item[]>([]);
const itemSubscription = useSubscription(gqlOnMutateItem, {
onSubscriptionData: ({ subscriptionData }) => {
setTotalItemsReceivedViaSubscription((prev) => prev + 1);
const itemFromSubscription: Item = subscriptionData.data.onMutateItem;
const pinboardId = itemFromSubscription.pinboardId;
// TODO trigger any item count re-fetches (probably need to store a count of items to trigger useEffect elsewhere)
Expand All @@ -317,6 +324,7 @@ export const GlobalStateProvider = ({
useState<Item[]>([]);
const claimSubscription = useSubscription(gqlOnClaimItem, {
onSubscriptionData: ({ subscriptionData }) => {
setTotalItemsReceivedViaSubscription((prev) => prev + 1);
const { updatedItem, newItem }: Claimed =
subscriptionData.data.onClaimItem;
const pinboardId = updatedItem.pinboardId;
Expand All @@ -330,11 +338,6 @@ export const GlobalStateProvider = ({
updatedItem,
newItem,
]);
if (!isExpanded && pinboardId !== selectedPinboardId) {
// TODO check this condition doesn't need to be ORed
showNotification(newItem);
setUnreadFlag(pinboardId)(true);
}
}
},
});
Expand Down Expand Up @@ -681,9 +684,10 @@ export const GlobalStateProvider = ({
activePinboards,
activePinboardIds,

allSubscriptionItems: allSubscriptionItems,
allSubscriptionClaimedItems: allSubscriptionClaimedItems,
allSubscriptionOnSeenItems: allSubscriptionOnSeenItems,
allSubscriptionItems,
allSubscriptionClaimedItems,
allSubscriptionOnSeenItems,
totalItemsReceivedViaSubscription,

payloadToBeSent,
setPayloadToBeSent,
Expand Down
12 changes: 6 additions & 6 deletions client/src/inline/inlineMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import React, { useEffect, useMemo, useState } from "react";
import { InlineModePinboardTogglePortal } from "./inlineModePinboardToggle";
import { useLazyQuery } from "@apollo/client";
import { gqlGetItemCounts } from "../../gql";
import { PinboardIdWithItemCounts } from "../../../shared/graphql/graphql";
import { PinboardIdWithItemCounts } from "shared/graphql/graphql";
import { InlineModePanel } from "./inlineModePanel";
import ReactDOM from "react-dom";
import { InlineModeWorkflowColumnHeading } from "./inlineModeWorkflowColumnHeading";
import { useGlobalStateContext } from "../globalState";

export const WORKFLOW_PINBOARD_ELEMENTS_QUERY_SELECTOR =
".content-list-item__field--pinboard";
Expand Down Expand Up @@ -37,6 +38,8 @@ export const InlineMode = ({
maybeInlineSelectedPinboardId,
setMaybeInlineSelectedPinboardId,
}: InlineModeProps) => {
const { totalItemsReceivedViaSubscription } = useGlobalStateContext();

const pinboardArea = useMemo(
() => document.getElementById("pinboard-area"),
[]
Expand All @@ -55,11 +58,9 @@ export const InlineMode = ({
Record<string, PinboardIdWithItemCounts>
>({});

const [fetchItemCounts, { stopPolling, startPolling }] =
useLazyQuery(gqlGetItemCounts);
const [fetchItemCounts] = useLazyQuery(gqlGetItemCounts);

useEffect(() => {
stopPolling();
const pinboardIds = Object.keys(workflowTitleElementLookup);
if (pinboardIds.length > 0) {
fetchItemCounts({
Expand All @@ -80,9 +81,8 @@ export const InlineMode = ({
)
),
});
startPolling(15000);
}
}, [workflowTitleElementLookup]);
}, [workflowTitleElementLookup, totalItemsReceivedViaSubscription]);

const maybeSelectedNode =
maybeInlineSelectedPinboardId &&
Expand Down
9 changes: 6 additions & 3 deletions client/src/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const Panel = ({
setActiveTab,
boundedPositionTranslation,
setUnreadFlag,
totalItemsReceivedViaSubscription,
} = useGlobalStateContext();

const tourProgress = useTourProgress();
Expand Down Expand Up @@ -94,9 +95,11 @@ export const Panel = ({
const isTopHalf =
Math.abs(boundedPositionTranslation.y) > window.innerHeight / 2;

const groupPinboardIdsQuery = useQuery(gqlGetGroupPinboardIds, {
pollInterval: 15000, // always poll this one, to ensure we get unread flags even when pinboard is not expanded
});
const groupPinboardIdsQuery = useQuery(gqlGetGroupPinboardIds);

useEffect(() => {
groupPinboardIdsQuery.refetch();
}, [totalItemsReceivedViaSubscription]);

const groupPinboardIdsWithClaimCounts: PinboardIdWithClaimCounts[] = useMemo(
() =>
Expand Down

0 comments on commit 6f274b8

Please sign in to comment.