-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathProjectSelector.tsx
More file actions
56 lines (48 loc) · 1.61 KB
/
ProjectSelector.tsx
File metadata and controls
56 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { EuiSelect, useGeneratedHtmlId } from "@elastic/eui";
import React from "react";
import { useNavigate, useParams, useLocation } from "react-router-dom";
import { useLoadProjectsList } from "../contexts/ProjectListContext";
const ProjectSelector = () => {
const { projectName } = useParams();
const navigate = useNavigate();
const location = useLocation();
const { isLoading, data } = useLoadProjectsList();
const currentProject = data?.projects.find((project) => {
return project.id === projectName;
});
const options = data?.projects.map((p) => {
return {
value: p.id,
text: p.name,
};
});
const basicSelectId = useGeneratedHtmlId({ prefix: "basicSelect" });
const onChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const newProjectId = e.target.value;
// If we're on a project page, maintain the current path context
if (projectName && location.pathname.startsWith(`/p/${projectName}`)) {
// Replace the old project name with the new one in the current path
const newPath = location.pathname.replace(
`/p/${projectName}`,
`/p/${newProjectId}`,
);
navigate(newPath);
} else {
// Otherwise, just navigate to the project home
navigate(`/p/${newProjectId}`);
}
};
return (
<EuiSelect
isLoading={isLoading}
hasNoInitialSelection={currentProject === undefined}
fullWidth={true}
id={basicSelectId}
options={options}
value={currentProject?.id || ""}
onChange={(e) => onChange(e)}
aria-label="Select a Feast Project"
/>
);
};
export default ProjectSelector;