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

Add file sidebar #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 41 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@
"webpack-merge": "^5.10.0"
},
"dependencies": {
"@patternfly/chatbot": "^2.1.0-prerelease.26",
"@patternfly/react-core": "^6.0.0",
"@patternfly/react-icons": "^6.0.0",
"@patternfly/react-styles": "^6.0.0",
"@patternfly/chatbot": "^2.1.0-prerelease.26",
"dompurify": "^3.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sirv-cli": "^2.0.2",
"dompurify": "^3.2.0"
"react-dropzone": "^14.3.5",
"sirv-cli": "^2.0.2"
}
}
77 changes: 54 additions & 23 deletions src/app/FlyoutList/FlyoutList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,43 @@
hideFlyout: () => void;
onFooterButtonClick?: () => void;
title: string;
error?: ErrorObject;
isLoading?: boolean;
onRetry?: () => void;
}
export const FlyoutList: React.FunctionComponent<FlyoutListProps> = ({
typeWordPlural,
buttonText,
hideFlyout,
onFooterButtonClick,
title,
error: errorProp,
isLoading: isLoadingProp = true,
onRetry,
}: FlyoutListProps) => {
const [error, setError] = React.useState<ErrorObject>();
const [items, setItems] = React.useState<CannedChatbot[]>([]);
const [filteredItems, setFilteredItems] = React.useState<CannedChatbot[]>([]);
const [isLoading, setIsLoading] = React.useState(true);
const [error, setError] = React.useState<ErrorObject | undefined>(errorProp);
const [originalChatbots, setOriginalChatbots] = React.useState<CannedChatbot[]>([]);
const [filteredChatbots, setFilteredChatbots] = React.useState<CannedChatbot[]>([]);
// you'll need states for files and filtered files
const [isLoading, setIsLoading] = React.useState(isLoadingProp);
const { nextStep, reloadList, setReloadList } = useFlyoutWizard();
const location = useLocation();
const navigate = useNavigate();
const { flyoutMenuSelectedChatbot, updateFlyoutMenuSelectedChatbot, chatbots, setChatbots } = useAppData();

// used for file case only
React.useEffect(() => {
setIsLoading(isLoadingProp);
}, [isLoadingProp]);

// used for file case only
React.useEffect(() => {
setError(errorProp);
}, [errorProp]);

const header = (
<div className="title-with-label">
{title} <Label variant="outline">{items.length}</Label>
{title} <Label variant="outline">{originalChatbots.length}</Label>
</div>
);

Expand Down Expand Up @@ -71,8 +88,8 @@
throw new Error(`${response.status}`);
}
const data = await response.json();
setItems(data);
setFilteredItems(data);
setOriginalChatbots(data);
setFilteredChatbots(data);
setIsLoading(false);
setReloadList(false);
setChatbots(data);
Expand All @@ -89,22 +106,27 @@
return;
}
if (chatbots.length > 0) {
setItems(chatbots);
setFilteredItems(chatbots);
setOriginalChatbots(chatbots);
setFilteredChatbots(chatbots);
setIsLoading(false);
return;
}
await getAssistants();
};

React.useEffect(() => {
loadAssistants();
if (typeWordPlural === 'assistants') {
loadAssistants();
}
if (typeWordPlural === 'files') {
// this is where you'd put your API call for files
}
}, []);

Check warning on line 124 in src/app/FlyoutList/FlyoutList.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook React.useEffect has missing dependencies: 'loadAssistants' and 'typeWordPlural'. Either include them or remove the dependency array

const buildMenu = () => {
return (
<MenuList>
{filteredItems.map((item) => (
{filteredChatbots.map((item) => (
<MenuItem
className="pf-chatbot__menu-item"
itemId={item.name}
Expand All @@ -120,39 +142,48 @@
};

const onSelect = (_event: React.MouseEvent<Element, MouseEvent> | undefined, value) => {
if (filteredItems.length > 0) {
const newChatbot = items.filter((item) => item.name === value)[0];
if (filteredChatbots.length > 0) {
const newChatbot = originalChatbots.filter((item) => item.name === value)[0];
updateFlyoutMenuSelectedChatbot(newChatbot);
if (location.pathname !== '/') {
navigate('/');
}
}
};

const findMatchingItems = (targetValue: string) => {
const matchingElements = items.filter((item) => {
const findMatchingItems = (targetValue: string, originalItems) => {
const matchingElements = originalItems.filter((item) => {
const name = item.displayName ?? item.name;
return name.toLowerCase().includes(targetValue.toLowerCase());
});
return matchingElements;
};

const handleTextInputChange = (value: string) => {
if (value === '') {
setFilteredItems(items);
return;
if (typeWordPlural === 'assistants') {
if (value === '') {
setFilteredChatbots(originalChatbots);
return;
}
const newItems = findMatchingItems(value, originalChatbots);
setFilteredChatbots(newItems);
}
if (typeWordPlural === 'files') {
// you'll need to add a matcher here based on state
}
const newItems = findMatchingItems(value);
setFilteredItems(newItems);
};

const onClick = () => {
const onAssistantClick = () => {
setError(undefined);
loadAssistants();
};

return error ? (
<FlyoutError title={error.title} subtitle={error.body} onClick={onClick} />
<FlyoutError
title={error.title}
subtitle={error.body}
onClick={typeWordPlural === 'assistants' ? onAssistantClick : onRetry}
/>
) : (
<>
<FlyoutHeader title={header} hideFlyout={hideFlyout} />
Expand All @@ -166,7 +197,7 @@
/>
<Menu className="flyout-list-menu" isPlain onSelect={onSelect}>
<MenuContent>
{filteredItems.length > 0 ? (
{filteredChatbots.length > 0 ? (
buildMenu()
) : (
<MenuList>
Expand Down
Loading
Loading