|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import moment from "moment"; |
| 8 | +import { useLocation } from "react-router"; |
| 9 | +import { Link, Redirect } from "react-router-dom"; |
| 10 | +import { useContext, useState, useEffect } from "react"; |
| 11 | + |
| 12 | +import { adminMenu } from "./admin-menu"; |
| 13 | +import ProjectDetail from "./ProjectDetail"; |
| 14 | +import { UserContext } from "../user-context"; |
| 15 | +import { getGitpodService } from "../service/service"; |
| 16 | +import { PageWithSubMenu } from "../components/PageWithSubMenu"; |
| 17 | +import { AdminGetListResult, Project } from "@gitpod/gitpod-protocol"; |
| 18 | + |
| 19 | +export default function ProjectsSearchPage() { |
| 20 | + return ( |
| 21 | + <PageWithSubMenu subMenu={adminMenu} title="Projects" subtitle="Search and manage all projects."> |
| 22 | + <ProjectsSearch /> |
| 23 | + </PageWithSubMenu> |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +export function ProjectsSearch() { |
| 28 | + const location = useLocation(); |
| 29 | + const { user } = useContext(UserContext); |
| 30 | + const [searchTerm, setSearchTerm] = useState(''); |
| 31 | + const [searching, setSearching] = useState(false); |
| 32 | + const [searchResult, setSearchResult] = useState<AdminGetListResult<Project>>({ total: 0, rows: [] }); |
| 33 | + const [currentProject, setCurrentProject] = useState<Project | undefined>(undefined); |
| 34 | + const [currentProjectOwner, setCurrentProjectOwner] = useState<string | undefined>(""); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + const projectId = location.pathname.split('/')[3]; |
| 38 | + if (projectId && searchResult) { |
| 39 | + let currentProject = searchResult.rows.find(project => project.id === projectId); |
| 40 | + if (currentProject) { |
| 41 | + setCurrentProject(currentProject); |
| 42 | + } else { |
| 43 | + getGitpodService().server.adminGetProjectById(projectId) |
| 44 | + .then(project => setCurrentProject(project)) |
| 45 | + .catch(e => console.error(e)); |
| 46 | + } |
| 47 | + } else { |
| 48 | + setCurrentProject(undefined); |
| 49 | + } |
| 50 | + }, [location]); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + (async () => { |
| 54 | + if (currentProject) { |
| 55 | + if (currentProject.userId) { |
| 56 | + const owner = await getGitpodService().server.adminGetUser(currentProject.userId); |
| 57 | + if (owner) { setCurrentProjectOwner(owner?.name) } |
| 58 | + } |
| 59 | + if (currentProject.teamId) { |
| 60 | + const owner = await getGitpodService().server.adminGetTeamById(currentProject.teamId); |
| 61 | + if (owner) { setCurrentProjectOwner(owner?.name) } |
| 62 | + } |
| 63 | + } |
| 64 | + })(); |
| 65 | + }, [currentProject]) |
| 66 | + |
| 67 | + if (!user || !user?.rolesOrPermissions?.includes('admin')) { |
| 68 | + return <Redirect to="/" /> |
| 69 | + } |
| 70 | + |
| 71 | + if (currentProject) { |
| 72 | + return <ProjectDetail project={currentProject} owner={currentProjectOwner} />; |
| 73 | + } |
| 74 | + |
| 75 | + const search = async () => { |
| 76 | + setSearching(true); |
| 77 | + try { |
| 78 | + const result = await getGitpodService().server.adminGetProjectsBySearchTerm({ |
| 79 | + searchTerm, |
| 80 | + limit: 50, |
| 81 | + orderBy: 'creationTime', |
| 82 | + offset: 0, |
| 83 | + orderDir: "desc" |
| 84 | + }) |
| 85 | + setSearchResult(result); |
| 86 | + } finally { |
| 87 | + setSearching(false); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return <> |
| 92 | + <div className="pt-8 flex"> |
| 93 | + <div className="flex justify-between w-full"> |
| 94 | + <div className="flex"> |
| 95 | + <div className="py-4"> |
| 96 | + <svg className={searching ? 'animate-spin' : ''} width="16" height="16" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 97 | + <path fillRule="evenodd" clipRule="evenodd" d="M6 2a4 4 0 100 8 4 4 0 000-8zM0 6a6 6 0 1110.89 3.477l4.817 4.816a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 010 6z" fill="#A8A29E" /> |
| 98 | + </svg> |
| 99 | + </div> |
| 100 | + <input type="search" placeholder="Search Projects" onKeyDown={(k) => k.key === 'Enter' && search()} onChange={(v) => { setSearchTerm(v.target.value) }} /> |
| 101 | + </div> |
| 102 | + <button disabled={searching} onClick={search}>Search</button> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + <div className="flex flex-col space-y-2"> |
| 106 | + <div className="px-6 py-3 flex justify-between text-sm text-gray-400 border-t border-b border-gray-200 dark:border-gray-800 mb-2"> |
| 107 | + <div className="w-4/12">Name</div> |
| 108 | + <div className="w-6/12">Clone URL</div> |
| 109 | + <div className="w-2/12">Created</div> |
| 110 | + </div> |
| 111 | + {searchResult.rows.map(project => <ProjectResultItem project={project} />)} |
| 112 | + </div> |
| 113 | + </> |
| 114 | + |
| 115 | + function ProjectResultItem(p: { project: Project }) { |
| 116 | + return ( |
| 117 | + <Link key={'pr-' + p.project.name} to={'/admin/projects/' + p.project.id} data-analytics='{"button_type":"sidebar_menu"}'> |
| 118 | + <div className="rounded-xl whitespace-nowrap flex py-6 px-6 w-full justify-between hover:bg-gray-100 dark:hover:bg-gray-800 focus:bg-gitpod-kumquat-light group"> |
| 119 | + <div className="flex flex-col w-4/12 truncate"> |
| 120 | + <div className="font-medium text-gray-800 dark:text-gray-100 truncate">{p.project.name}</div> |
| 121 | + </div> |
| 122 | + <div className="flex flex-col w-6/12 truncate"> |
| 123 | + <div className="font-medium text-gray-800 dark:text-gray-100 truncate">{p.project.cloneUrl}</div> |
| 124 | + </div> |
| 125 | + <div className="flex w-2/12 self-center"> |
| 126 | + <div className="text-sm w-full text-gray-400 truncate">{moment(p.project.creationTime).fromNow()}</div> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + </Link> |
| 130 | + ) |
| 131 | + } |
| 132 | +} |
0 commit comments