Skip to content

Commit

Permalink
Merge pull request #2371 from daostack/bugfix/CW-2356-same-items-on-s…
Browse files Browse the repository at this point in the history
…witch

Optimize breadcrumb loading #2356
  • Loading branch information
pvm-code authored Nov 30, 2023
2 parents e53dd3c + 8707e44 commit 53e77b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
selectCommonLayoutCommonId,
} from "@/store/states";
import { useGoToCreateCommon } from "../../../../../../hooks";
import { LoadingBreadcrumbsItem } from "../LoadingBreadcrumbsItem";
import { Separator } from "../Separator";
import { ActiveFeedBreadcrumbsItem, FeedBreadcrumbsItem } from "./components";
import styles from "./FeedItemBreadcrumbs.module.scss";
Expand Down Expand Up @@ -60,7 +59,6 @@ const FeedItemBreadcrumbs: FC<FeedItemBreadcrumbsProps> = (props) => {

return (
<ul className={styles.container}>
{breadcrumbs.areItemsLoading && <LoadingBreadcrumbsItem />}
{!breadcrumbs.areItemsLoading &&
breadcrumbs.items.map((item, index) => (
<React.Fragment key={item.commonId}>
Expand All @@ -75,7 +73,7 @@ const FeedItemBreadcrumbs: FC<FeedItemBreadcrumbsProps> = (props) => {
))}
{breadcrumbs.activeItem && (
<>
{(breadcrumbs.areItemsLoading || breadcrumbs.items.length > 0) && (
{!breadcrumbs.areItemsLoading && breadcrumbs.items.length > 0 && (
<Separator />
)}
<ActiveFeedBreadcrumbsItem
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { put, select } from "redux-saga/effects";
import { InboxItemType } from "@/shared/constants";
import {
MultipleSpacesLayoutFeedItemBreadcrumbs,
selectCommonLayoutCommonsState,
selectCommonLayoutProjectsState,
} from "@/store/states";
Expand Down Expand Up @@ -39,6 +40,42 @@ const getItemsByExistingData = (
return items;
};

const getNextBreadcrumbsData = (
items: ProjectsStateItem[] | null,
currentBreadcrumbs: MultipleSpacesLayoutFeedItemBreadcrumbs | null,
activeCommonId: string,
): Pick<
MultipleSpacesLayoutFeedItemBreadcrumbs,
"items" | "areItemsLoading" | "areItemsFetched"
> => {
if (items) {
return {
items,
areItemsLoading: false,
areItemsFetched: true,
};
}
if (!currentBreadcrumbs) {
return {
items: [],
areItemsLoading: true,
areItemsFetched: false,
};
}

const activeItemIndex = currentBreadcrumbs.items.findIndex(
(item) => item.commonId === activeCommonId,
);

return {
...currentBreadcrumbs,
items:
activeItemIndex > -1
? currentBreadcrumbs.items.slice(0, activeItemIndex + 1)
: currentBreadcrumbs.items,
};
};

export function* configureBreadcrumbsData(
action: ReturnType<typeof actions.configureBreadcrumbsData>,
) {
Expand Down Expand Up @@ -88,17 +125,11 @@ export function* configureBreadcrumbsData(

yield put(
actions.setBreadcrumbsData({
...(items
? {
items,
areItemsLoading: false,
areItemsFetched: true,
}
: currentBreadcrumbs || {
items: [],
areItemsLoading: true,
areItemsFetched: false,
}),
...getNextBreadcrumbsData(
items,
currentBreadcrumbs,
payload.activeCommonId,
),
type: InboxItemType.FeedItemFollow,
activeItem: payload.activeItem ? { ...payload.activeItem } : null,
activeCommonId: payload.activeCommonId,
Expand Down

0 comments on commit 53e77b9

Please sign in to comment.