-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2067 from daostack/bugfix/CW-2052-navbar-update-o…
…n-stream-creation Auto refresh the navbar upon creation of a new space by another user #2052
- Loading branch information
Showing
17 changed files
with
284 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...ed/layouts/CommonSidenavLayout/components/SidenavContent/hooks/useProjectsSubscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useSelector } from "react-redux"; | ||
import { CommonEvent, CommonEventEmitter } from "@/events"; | ||
import { selectUser } from "@/pages/Auth/store/selectors"; | ||
import { CommonService, GovernanceService, Logger } from "@/services"; | ||
import { GovernanceActions } from "@/shared/constants"; | ||
import { Common } from "@/shared/models"; | ||
import { generateCirclesDataForCommonMember } from "@/shared/utils"; | ||
import { ProjectsStateItem } from "@/store/states"; | ||
|
||
interface Data { | ||
activeItemId: string; | ||
areProjectsFetched: boolean; | ||
commons: ProjectsStateItem[]; | ||
projects: ProjectsStateItem[]; | ||
} | ||
|
||
const getProjectItemFromCommon = async ( | ||
common: Common, | ||
userId?: string, | ||
initialItem?: ProjectsStateItem, | ||
): Promise<ProjectsStateItem> => { | ||
const baseItem: Omit< | ||
ProjectsStateItem, | ||
"hasMembership" | "hasPermissionToAddProject" | ||
> = { | ||
commonId: common.id, | ||
image: common.image, | ||
name: common.name, | ||
directParent: common.directParent, | ||
}; | ||
|
||
if (initialItem) { | ||
return { | ||
...baseItem, | ||
hasMembership: initialItem.hasMembership, | ||
hasPermissionToAddProject: initialItem.hasPermissionToAddProject, | ||
}; | ||
} | ||
if (!userId) { | ||
return { | ||
...baseItem, | ||
hasMembership: false, | ||
hasPermissionToAddProject: false, | ||
}; | ||
} | ||
|
||
const [governance, commonMember] = await Promise.all([ | ||
GovernanceService.getGovernanceByCommonId(common.id), | ||
CommonService.getCommonMemberByUserId(common.id, userId), | ||
]); | ||
|
||
return { | ||
...baseItem, | ||
hasMembership: Boolean(commonMember), | ||
hasPermissionToAddProject: Boolean( | ||
governance && | ||
commonMember && | ||
generateCirclesDataForCommonMember( | ||
governance.circles, | ||
commonMember.circleIds, | ||
).allowedActions[GovernanceActions.CREATE_PROJECT], | ||
), | ||
}; | ||
}; | ||
|
||
export const useProjectsSubscription = (data: Data) => { | ||
const { activeItemId, areProjectsFetched, commons, projects } = data; | ||
const [updatedItemsQueue, setUpdatedItemsQueue] = useState<Common[][]>([]); | ||
const user = useSelector(selectUser()); | ||
const userId = user?.uid; | ||
const nextUpdatedItems = updatedItemsQueue[0]; | ||
|
||
useEffect(() => { | ||
if (!areProjectsFetched) { | ||
return; | ||
} | ||
|
||
const unsubscribe = CommonService.subscribeToSubCommons( | ||
activeItemId, | ||
(data) => { | ||
const commons = data.reduce<Common[]>((acc, { common, isRemoved }) => { | ||
if (!isRemoved) { | ||
CommonEventEmitter.emit(CommonEvent.CommonUpdated, common); | ||
return acc.concat(common); | ||
} | ||
|
||
return acc; | ||
}, []); | ||
|
||
if (commons.length !== 0) { | ||
setUpdatedItemsQueue((currentItems) => | ||
currentItems.concat([commons]), | ||
); | ||
} | ||
}, | ||
); | ||
|
||
return unsubscribe; | ||
}, [areProjectsFetched, activeItemId]); | ||
|
||
useEffect(() => { | ||
if (!nextUpdatedItems) { | ||
return; | ||
} | ||
if (nextUpdatedItems.length === 0) { | ||
setUpdatedItemsQueue((currentItems) => currentItems.slice(1)); | ||
return; | ||
} | ||
|
||
(async () => { | ||
try { | ||
const items = await Promise.all( | ||
nextUpdatedItems.map(async (nextUpdatedItem) => { | ||
const existingItem = | ||
commons.find((item) => item.commonId === nextUpdatedItem.id) || | ||
projects.find((item) => item.commonId === nextUpdatedItem.id); | ||
|
||
return await getProjectItemFromCommon( | ||
nextUpdatedItem, | ||
userId, | ||
existingItem, | ||
); | ||
}), | ||
); | ||
|
||
items.forEach((item) => { | ||
CommonEventEmitter.emit(CommonEvent.ProjectCreatedOrUpdated, item); | ||
}); | ||
} catch (error) { | ||
Logger.error(error); | ||
} finally { | ||
setUpdatedItemsQueue((currentItems) => currentItems.slice(1)); | ||
} | ||
})(); | ||
}, [nextUpdatedItems]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.