Add a way for frontend to know what URL to use when switching organizations
Update
Closed in favor of #437541 (closed) and #437095 (closed)
Background
In Allow switching of organizations (#423116 - closed) we are adding an organization switcher to the sidebar navigation. This switcher will list the available organizations by using the GraphQL query that was added in !136024 (merged). We need a way to know what URL to go to when switching organizations. For example if you are in the default organization on the main dashboard the URL would be https://gitlab.com. If you were to switch to the foo-bar organization the URL would be https://gitlab.com/-/organizations/foo-bar/dashboard/projects, but the frontend doesn't currently have a way to calculate this URL.
Proposal
Current organization
- Backend will calculate the current organization and expose it via the
current_organizationmethod in lib/organization/current_organization.rb#L11 - We will expose the current organization to the frontend in lib/gitlab/gon_helper.rb similar to how we expose
current_user - The frontend will use
gon.current_organizationto display the current organization in the organization selector.
Switching organizations
- Create a Rails helper (let's call it
current_route_scoped_to_organizationfor now) that calculates the current route in the context of an organization.- If it is a route that can be scoped to organizations such as your work or your profile it returns a route with a placeholder for the organization path. e.g.
https://gitlab.com/-/organizations/:organization_path/dashboard/projects - If it is a route that cannot be scoped to an organization (such as
https://gitlab.com/gitlab-org/gitlaborhttps://gitlab.com/gitlab-org/returnnil
- If it is a route that can be scoped to organizations such as your work or your profile it returns a route with a placeholder for the organization path. e.g.
- Pass the return value of
current_route_scoped_to_organizationto the frontend via a data attribute. - In the frontend, if
current_route_scoped_to_organizationisnulllink to the organization front page. Ifcurrent_route_scoped_to_organizationis a string replace:organization_pathwith the organization path and link to that URL.
Implementation guide
Need to confirm proposal makes sense then refine.
Edited by Peter Hegman