-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEVPROD-7654: Add image dropdown to SideNav (#257)
- Loading branch information
1 parent
fc62698
commit 0e85267
Showing
13 changed files
with
177 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
echo "{ \"_id\": \"runtime_environments\", \"base_url\": \"${BASE_URL}\", \"api_key\":\"${API_KEY}\" }" >> ../evergreen/testdata/local/admin.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { isProduction } from "utils/environmentVariables"; | ||
|
||
export const showGitHubAccessTokenProject = !isProduction(); | ||
export const showImageVisibilityPage = !isProduction(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
query Images { | ||
images | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useQuery } from "@apollo/client"; | ||
import { ImagesQuery, ImagesQueryVariables } from "gql/generated/types"; | ||
import { IMAGES } from "gql/queries"; | ||
|
||
/** | ||
* `useFirstImage` returns the alphabetically first image from Evergreen's list of images. | ||
* @returns an object containing the image ID (string) and loading state (boolean) | ||
*/ | ||
export const useFirstImage = () => { | ||
const { data, loading } = useQuery<ImagesQuery, ImagesQueryVariables>(IMAGES); | ||
|
||
return { image: data?.images?.[0] ?? "ubuntu2204", loading }; | ||
}; |
44 changes: 44 additions & 0 deletions
44
apps/spruce/src/hooks/useFirstImage/useFirstImage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { MockedProvider, MockedProviderProps } from "@apollo/client/testing"; | ||
import { ImagesQuery, ImagesQueryVariables } from "gql/generated/types"; | ||
import { IMAGES } from "gql/queries"; | ||
import { renderHook, waitFor } from "test_utils"; | ||
import { ApolloMock } from "types/gql"; | ||
import { useFirstImage } from "."; | ||
|
||
interface ProviderProps { | ||
mocks?: MockedProviderProps["mocks"]; | ||
children: React.ReactNode; | ||
} | ||
const ProviderWrapper: React.FC<ProviderProps> = ({ children, mocks = [] }) => ( | ||
<MockedProvider mocks={mocks}>{children}</MockedProvider> | ||
); | ||
|
||
describe("useFirstImage", () => { | ||
it("retrieve first image from list", async () => { | ||
const { result } = renderHook(() => useFirstImage(), { | ||
wrapper: ({ children }) => | ||
ProviderWrapper({ | ||
children, | ||
mocks: [getImages], | ||
}), | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.loading).toBe(false); | ||
}); | ||
|
||
expect(result.current.image).toBe("amazon2"); | ||
}); | ||
}); | ||
|
||
const getImages: ApolloMock<ImagesQuery, ImagesQueryVariables> = { | ||
request: { | ||
query: IMAGES, | ||
variables: {}, | ||
}, | ||
result: { | ||
data: { | ||
images: ["amazon2", "debian12", "suse15", "ubuntu1604"], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useQuery } from "@apollo/client"; | ||
import { Combobox, ComboboxOption } from "@leafygreen-ui/combobox"; | ||
import { Skeleton } from "@leafygreen-ui/skeleton-loader"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { getImageRoute } from "constants/routes"; | ||
import { zIndex } from "constants/tokens"; | ||
import { useToastContext } from "context/toast"; | ||
import { ImagesQuery, ImagesQueryVariables } from "gql/generated/types"; | ||
import { IMAGES } from "gql/queries"; | ||
|
||
interface ImageSelectProps { | ||
selectedImage: string; | ||
} | ||
|
||
export const ImageSelect: React.FC<ImageSelectProps> = ({ selectedImage }) => { | ||
const navigate = useNavigate(); | ||
|
||
const dispatchToast = useToastContext(); | ||
const { data: imagesData, loading } = useQuery< | ||
ImagesQuery, | ||
ImagesQueryVariables | ||
>(IMAGES, { | ||
onError: (err) => { | ||
dispatchToast.warning(`Failed to retrieve images: ${err.message}`); | ||
}, | ||
}); | ||
|
||
const { images } = imagesData || {}; | ||
|
||
if (!loading) { | ||
return ( | ||
<Combobox | ||
clearable={false} | ||
data-cy="images-select" | ||
label="Images" | ||
placeholder="Select an image" | ||
popoverZIndex={zIndex.popover} | ||
portalClassName="images-select-options" | ||
disabled={loading} | ||
// @ts-expect-error: onChange expects type string | null | ||
onChange={(imageId: string) => { | ||
navigate(getImageRoute(imageId)); | ||
}} | ||
value={selectedImage} | ||
> | ||
{images?.map((image) => ( | ||
<ComboboxOption key={image} value={image}> | ||
{image} | ||
</ComboboxOption> | ||
))} | ||
</Combobox> | ||
); | ||
} | ||
return <Skeleton />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters