Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropdown for versions on diff page #715

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/DiffIntro/DiffIntro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const DiffIntro = forwardRef<DiffIntroProps, "h2">(
<Halfs
left={
<SpecBox
id="A"
packageName={aName}
packageVersion={aVersion}
/>
Expand All @@ -92,6 +93,7 @@ const DiffIntro = forwardRef<DiffIntroProps, "h2">(
}
right={
<SpecBox
id="B"
packageName={bName}
packageVersion={bVersion}
/>
Expand Down
125 changes: 108 additions & 17 deletions src/components/DiffIntro/SpecBox.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,116 @@
import { Box, BoxProps, Code, forwardRef, Text } from "@chakra-ui/react";
import {
Box,
BoxProps,
Code,
forwardRef,
Spinner,
Text,
} from "@chakra-ui/react";
import { useState } from "react";
import {
useNpmCombobox,
UseNpmComboboxProps,
} from "^/lib/npm-combobox/useNpmCombobox";
import {
ComboboxBox,
ComboboxInput,
ComboboxSuggestion,
ComboboxSuggestionList,
ComboboxWrapper,
} from "../Landing/MainForm/SpecInput/Combobox";
import Suggestion from "../Landing/MainForm/SpecInput/Suggestion";
import ServiceLinks from "./ServiceLinks";

interface SpecBoxProps extends BoxProps {
packageName: string;
packageVersion: string;
}
const SuggestionListText = ({
children,
error = false,
}: {
children: string;
error?: boolean;
}) => (
<Text padding="2em" align="center" color={error ? "red.500" : "gray.500"}>
{children}
</Text>
);

type SpecBoxProps = BoxProps &
Pick<UseNpmComboboxProps, "id"> & {
packageName: string;
packageVersion: string;
};

const SpecBox = forwardRef<SpecBoxProps, "div">(
({ packageName, packageVersion, ...props }, ref) => (
<Box {...props} ref={ref}>
<Code>
{packageName}@{packageVersion}
</Code>
<Box>
<ServiceLinks
packageName={packageName}
packageVersion={packageVersion}
/>
({ packageName, packageVersion, id, ...props }, ref) => {
const [inputValue, onInputValueChange] = useState(
`${packageName}@${packageVersion}`,
);

const {
getInputProps,
getItemProps,
getMenuProps,
highlightedIndex,
isOpen,
items,
loading,
error,
} = useNpmCombobox({
id,
fallback: [],
inputValue,
onInputValueChange,
});

return (
<Box {...props} ref={ref}>
<ComboboxWrapper>
<ComboboxBox size="xs">
<ComboboxInput isOpen={isOpen} {...getInputProps()} />
</ComboboxBox>
<ComboboxSuggestionList {...getMenuProps()}>
{isOpen ? (
<>
{error ? (
<SuggestionListText error={true}>
Something went wrong.
</SuggestionListText>
) : items.length === 0 ? (
<SuggestionListText>
No suggestions
</SuggestionListText>
) : (
items.map((item, index) => (
<ComboboxSuggestion
key={item.value}
highlighted={
index === highlightedIndex
}
{...getItemProps({ item, index })}
>
<Suggestion item={item} />
</ComboboxSuggestion>
))
)}
{loading ? (
<Spinner
position="absolute"
right={2}
bottom={2}
/>
) : null}
</>
) : null}
</ComboboxSuggestionList>
</ComboboxWrapper>
<Box>
<ServiceLinks
packageName={packageName}
packageVersion={packageVersion}
/>
</Box>
</Box>
</Box>
),
);
},
);

export default SpecBox;