getProjectsByTeams.tsx 983 B

123456789101112131415161718192021222324252627282930313233
  1. import {Project, Team} from 'sentry/types';
  2. export default function getProjectsByTeams(
  3. teams: Team[],
  4. projects: Project[],
  5. isSuperuser: boolean = false
  6. ): {projectsByTeam: {[teamSlug: string]: Project[]}; teamlessProjects: Project[]} {
  7. const projectsByTeam = {};
  8. const teamlessProjects: Project[] = [];
  9. let usersTeams = new Set(teams.filter(team => team.isMember).map(team => team.slug));
  10. if (usersTeams.size === 0 && isSuperuser) {
  11. usersTeams = new Set(teams.map(team => team.slug));
  12. }
  13. projects.forEach(project => {
  14. if (!project.teams.length && project.isMember) {
  15. teamlessProjects.push(project);
  16. } else {
  17. project.teams.forEach(team => {
  18. if (!usersTeams.has(team.slug)) {
  19. return;
  20. }
  21. if (!projectsByTeam.hasOwnProperty(team.slug)) {
  22. projectsByTeam[team.slug] = [];
  23. }
  24. projectsByTeam[team.slug].push(project);
  25. });
  26. }
  27. });
  28. return {projectsByTeam, teamlessProjects};
  29. }