Skip to content
Draft
Show file tree
Hide file tree
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
51 changes: 28 additions & 23 deletions client/src/fronts/frontsIntegration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useMemo, useState } from "react";
import { PinboardData } from "shared/graphql/extraTypes";
import { PinboardIdWithItemCounts } from "shared/graphql/graphql";
import ReactDOM from "react-dom";
import { useApolloClient, useQuery } from "@apollo/client";
import { useApolloClient, useLazyQuery } from "@apollo/client";
import { gqlGetItemCounts, gqlGetPinboardsByPaths } from "../../gql";
import { FrontsPinboardArticleButton } from "./frontsPinboardArticleButton";
import { useGlobalStateContext } from "../globalState";
Expand Down Expand Up @@ -36,13 +36,15 @@ export const FrontsIntegration = ({
{} as { [path: string]: PinboardData }
);
useEffect(() => {
const paths = Object.keys(pathToElementsMap);
paths.length > 0 &&
const pathsYetToBeLookedUp = Object.keys(pathToElementsMap).filter(
(path) => !pathToPinboardDataMap[path]
);
pathsYetToBeLookedUp.length > 0 &&
apolloClient
.query({
query: gqlGetPinboardsByPaths,
variables: {
paths,
paths: pathsYetToBeLookedUp,
},
})
.then(({ data }) => {
Expand All @@ -68,32 +70,35 @@ export const FrontsIntegration = ({
[pathToPinboardDataMap]
);

const itemCountsQuery = useQuery(gqlGetItemCounts, {
variables: {
pinboardIds,
},
});
const [fetchItemCounts] = useLazyQuery(gqlGetItemCounts);

useEffect(() => {
itemCountsQuery.refetch();
if (pinboardIds.length > 0) {
fetchItemCounts({
variables: { pinboardIds },
onCompleted: (data) => {
data?.getItemCounts &&
setMaybeItemCountsLookup(
data.getItemCounts.reduce(
(
acc: ItemCountsLookup,
itemCounts: PinboardIdWithItemCounts
) => ({
...acc,
[itemCounts.pinboardId]: itemCounts,
}),
{}
)
);
},
});
}
}, [
pinboardIds,
totalItemsReceivedViaSubscription,
totalOfMyOwnOnSeenItemsReceivedViaSubscription,
]);

useEffect(() => {
itemCountsQuery.data?.getItemCounts &&
setMaybeItemCountsLookup(
itemCountsQuery.data.getItemCounts.reduce(
(acc: ItemCountsLookup, itemCounts: PinboardIdWithItemCounts) => ({
...acc,
[itemCounts.pinboardId]: itemCounts,
}),
{}
)
);
}, [itemCountsQuery.data]);

return (
<>
{Object.entries(pathToElementsMap).map(
Expand Down
4 changes: 2 additions & 2 deletions client/src/inline/inlineMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const InlineMode = ({
() => getWorkflowTitleElementLookup(workflowPinboardElements),
[workflowPinboardElements]
);
const pinboardIds = Object.keys(workflowTitleElementLookup);

const [itemCountsLookup, setItemCountsLookup] = useState<
Record<string, PinboardIdWithItemCounts>
Expand All @@ -64,7 +65,6 @@ export const InlineMode = ({
const [fetchItemCounts] = useLazyQuery(gqlGetItemCounts);

useEffect(() => {
const pinboardIds = Object.keys(workflowTitleElementLookup);
if (pinboardIds.length > 0) {
fetchItemCounts({
variables: { pinboardIds },
Expand All @@ -86,7 +86,7 @@ export const InlineMode = ({
});
}
}, [
workflowTitleElementLookup,
pinboardIds.toString(),
totalItemsReceivedViaSubscription,
totalOfMyOwnOnSeenItemsReceivedViaSubscription,
]);
Expand Down