2022-11-03 16:39:51 +00:00
|
|
|
import { fetchWorkspace } from "@appsmith/actions/workspaceActions";
|
2022-06-15 15:37:41 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { useSelector, useDispatch } from "react-redux";
|
|
|
|
|
import { getCurrentUser } from "selectors/usersSelectors";
|
|
|
|
|
|
|
|
|
|
import { getCurrentAppWorkspace } from "@appsmith/selectors/workspaceSelectors";
|
|
|
|
|
import { ANONYMOUS_USERNAME } from "constants/userConstants";
|
|
|
|
|
|
|
|
|
|
const useWorkspace = (workspaceId: string) => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const workspace = useSelector(getCurrentAppWorkspace);
|
|
|
|
|
const currentUser = useSelector(getCurrentUser);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!currentUser || currentUser.username === ANONYMOUS_USERNAME) return;
|
|
|
|
|
|
|
|
|
|
if ((!workspace || !workspace.userPermissions) && workspaceId) {
|
|
|
|
|
dispatch(fetchWorkspace(workspaceId, true));
|
|
|
|
|
}
|
|
|
|
|
}, [workspaceId, currentUser && currentUser.username]);
|
|
|
|
|
|
|
|
|
|
return workspace;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useWorkspace;
|