Skip to content

Commit

Permalink
optimize breadcrumbs configuration using existing data
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Oct 24, 2023
1 parent a01f32e commit 742d3fc
Showing 1 changed file with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
import { put, select } from "redux-saga/effects";
import { InboxItemType } from "@/shared/constants";
import {
selectCommonLayoutCommonsState,
selectCommonLayoutProjectsState,
} from "@/store/states";
import * as actions from "../actions";
import { selectMultipleSpacesLayoutBreadcrumbs } from "../selectors";
import { MultipleSpacesLayoutState } from "../types";
import { MultipleSpacesLayoutState, ProjectsStateItem } from "../types";

const getItemsByExistingData = (
activeCommonId: string,
existingItems: ProjectsStateItem[],
): ProjectsStateItem[] | null => {
const currentItem = existingItems.find(
(item) => item.commonId === activeCommonId,
);

if (!currentItem) {
return null;
}

const items: ProjectsStateItem[] = [currentItem];
let parentCommonId = currentItem.directParent?.commonId;

while (parentCommonId) {
const parentItem = existingItems.find(
(item) => item.commonId === parentCommonId,
);

if (!parentItem) {
return null;
}

items.unshift(parentItem);
parentCommonId = parentItem.directParent?.commonId;
}

return items;
};

export function* configureBreadcrumbsData(
action: ReturnType<typeof actions.configureBreadcrumbsData>,
Expand Down Expand Up @@ -41,27 +76,44 @@ export function* configureBreadcrumbsData(
return;
}

const { commons, areCommonsFetched } = (yield select(
selectCommonLayoutCommonsState,
)) as { commons: ProjectsStateItem[]; areCommonsFetched: boolean };
const { projects } = (yield select(selectCommonLayoutProjectsState)) as {
projects: ProjectsStateItem[];
};
const items = areCommonsFetched
? getItemsByExistingData(payload.activeCommonId, [...commons, ...projects])
: null;

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

if (
currentBreadcrumbs?.activeCommonId !== payload.activeCommonId ||
!currentBreadcrumbs.items.some(
(item) => item.commonId === payload.activeCommonId,
)
) {
if (!items) {
yield put(
actions.fetchBreadcrumbsItemsByCommonId.request(payload.activeCommonId),
);
} else {
yield put(
actions.fetchBreadcrumbsItemsByCommonId.cancel(
"Stop current breadcrumbs items fetch",
),
);
}
}

0 comments on commit 742d3fc

Please sign in to comment.