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

feat: add FilterList to TreeDetails page #4

Merged
merged 6 commits into from
Jul 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion dashboard/src/api/TreeDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fetchTreeDetailData = async (

Object.keys(filter).forEach(key => {
const filterList = filter[key as keyof TTreeDetailsFilter];
filterList?.map(value =>
filterList?.forEach(value =>
filterParam.append(`filter_${key}`, value.toString()),
);
});
Expand Down
15 changes: 3 additions & 12 deletions dashboard/src/components/Filter/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { Separator } from '../ui/separator';

interface IDrawerLink {
treeURL: string;
onRefresh?: () => void;
}

interface IFilterDrawer extends IDrawerLink {
Expand All @@ -41,10 +40,10 @@ const DrawerHeader = (): JSX.Element => {
);
};

const DrawerLink = ({ treeURL, onRefresh }: IDrawerLink): JSX.Element => {
const DrawerLink = ({ treeURL }: IDrawerLink): JSX.Element => {
return (
<div className="flex justify-between items-center mb-8">
<div className="w-[766px] h-[52px] px-4 py-2 bg-white border border-darkGray flex flex-col">
<div className="w-full h-[52px] px-4 py-2 bg-white border border-darkGray flex flex-col">
<span className="text-xs text-darkGray2">
<FormattedMessage id="filter.treeURL" />
</span>
Expand All @@ -56,21 +55,13 @@ const DrawerLink = ({ treeURL, onRefresh }: IDrawerLink): JSX.Element => {
{treeURL}
</a>
</div>
<Button
onClick={onRefresh}
variant="outline"
className="w-[200px] rounded-full border-2 border-black"
>
<FormattedMessage id="filter.refresh" />
</Button>
</div>
);
};

const Drawer = ({
treeURL,
children,
onRefresh,
onCancel,
onFilter,
}: IFilterDrawer): JSX.Element => {
Expand All @@ -85,7 +76,7 @@ const Drawer = ({
<DrawerContent className="flex items-center px-4 bg-lightGray max-h-screen">
<DrawerHeader />
<section className="overflow-y-auto max-h-full">
<DrawerLink treeURL={treeURL} onRefresh={onRefresh} />
<DrawerLink treeURL={treeURL} />
<div className="w-[1000px] rounded-lg bg-white">
{React.Children.map(children, (child, idx) => (
<>
Expand Down
4 changes: 2 additions & 2 deletions dashboard/src/components/FilterList/FilterList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
itens: ['linux-5.15.y', 'Status:failed', 'Status: Warnings'],
items: ['linux-5.15.y', 'Status:failed', 'Status: Warnings'],
},
decorators: [
(story): JSX.Element => (
Expand All @@ -47,7 +47,7 @@ export const Default: Story = {

export const MultipleLines: Story = {
args: {
itens: [
items: [
'linux-5.15.y',
'Status:failed',
'Status: Warnings',
Expand Down
21 changes: 11 additions & 10 deletions dashboard/src/components/FilterList/FilterList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { useIntl } from 'react-intl';

import { Button } from '../ui/button';

interface IFilterList {
itens: string[];
onClickItem: (itemIdx: number) => void;
export interface IFilterList {
items: string[];
onClickItem: (item: string, itemIdx: number) => void;
onClickCleanAll: () => void;
removeOnEmpty?: boolean;
}

interface IFilterItem extends IFilterButton {
idx: number;
onClickItem: (idx: number) => void;
onClickItem: (item: string, itemIdx: number) => void;
}

export interface IFilterButton
Expand Down Expand Up @@ -57,8 +57,8 @@ const FilterItem = ({
...props
}: IFilterItem): JSX.Element => {
const onClickHandler = useCallback(
() => onClickItem(idx),
[onClickItem, idx],
() => onClickItem(text, idx),
[text, onClickItem, idx],
);

return (
Expand All @@ -72,7 +72,7 @@ const FilterItem = ({
};

const FilterList = ({
itens,
items,
onClickItem,
onClickCleanAll,
removeOnEmpty = false,
Expand All @@ -81,25 +81,26 @@ const FilterList = ({

const buttonList = useMemo(
() =>
itens.map((item, idx) => (
items.map((item, idx) => (
<FilterItem
key={idx + item}
text={item}
onClickItem={onClickItem}
idx={idx}
/>
)),
[itens, onClickItem],
[items, onClickItem],
);

if (removeOnEmpty && !itens) {
if (removeOnEmpty && !items.length) {
return <></>;
}

return (
<div className="flex flex-wrap gap-4">
{buttonList}
<FilterButton
className="hover:bg-darkGray2"
text={intl.formatMessage({ id: 'global.cleanAll' })}
variant="primary"
onClick={onClickCleanAll}
Expand Down
9 changes: 8 additions & 1 deletion dashboard/src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ export interface ITabItem {
export interface ITabsComponent {
tabs: ITabItem[];
defaultTab: ITabItem;
filterListElement?: JSX.Element;
}

const TabsComponent = ({ defaultTab, tabs }: ITabsComponent): JSX.Element => {
const TabsComponent = ({
defaultTab,
tabs,
filterListElement,
}: ITabsComponent): JSX.Element => {
const tabsTrigger = useMemo(
() =>
tabs.map(tab => (
Expand Down Expand Up @@ -48,6 +53,8 @@ const TabsComponent = ({ defaultTab, tabs }: ITabsComponent): JSX.Element => {
return (
<Tabs defaultValue={defaultTab.name} className="w-full">
<TabsList className="w-full justify-start">{tabsTrigger}</TabsList>
<div className="py-6">{filterListElement}</div>

{tabsContent}
</Tabs>
);
Expand Down
11 changes: 9 additions & 2 deletions dashboard/src/components/Tabs/TreeDetailsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import TreeDetailsBuildTab from './TreeDetails/TreeDetailsBuildTab';

export interface ITreeDetailsBuildTab {
treeDetailsData?: ITreeDetails;
filterListElement?: JSX.Element;
}

const TreeDetailsTab = ({
treeDetailsData,
filterListElement,
}: ITreeDetailsBuildTab): JSX.Element => {
const buildsTab: ITabItem = {
name: 'treeDetails.builds',
Expand All @@ -29,8 +31,13 @@ const TreeDetailsTab = ({
};

const treeDetailsTab = [buildsTab, bootsTab, testsTab];

return <Tabs tabs={treeDetailsTab} defaultTab={treeDetailsTab[0]} />;
return (
<Tabs
tabs={treeDetailsTab}
defaultTab={treeDetailsTab[0]}
filterListElement={filterListElement}
/>
);
};

export default TreeDetailsTab;
57 changes: 40 additions & 17 deletions dashboard/src/routes/TreeDetails/TreeDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { useParams } from 'react-router-dom';

import { useEffect, useState, useRef } from 'react';
import { useEffect, useState, useMemo } from 'react';

import { useTreeDetails } from '@/api/TreeDetails';
import TreeDetailsTab from '@/components/Tabs/TreeDetailsTab';
import { IListingItem } from '@/components/ListingItem/ListingItem';
import { ISummaryItem } from '@/components/Summary/Summary';
import {
AccordionItemBuilds,
Results,
TTreeDetailsFilter,
TreeDetails as TreeDetailsType,
} from '@/types/tree/TreeDetails';
import { AccordionItemBuilds, Results } from '@/types/tree/TreeDetails';

import TreeDetailsFilter from './TreeDetailsFilter';
import TreeDetailsFilter, {
createFilter,
mapFilterToReq,
TFilter,
} from './TreeDetailsFilter';

import TreeDetailsFilterList from './TreeDetailsFilterList';

export interface ITreeDetails {
archs: ISummaryItem[];
Expand All @@ -24,17 +25,35 @@ export interface ITreeDetails {

const TreeDetails = (): JSX.Element => {
const { treeId } = useParams();
const [filter, setFilter] = useState<
TTreeDetailsFilter | Record<string, never>
>({});
const { data } = useTreeDetails(treeId ?? '', filter);
const [filter, setFilter] = useState<TFilter>({});
const reqFilter = mapFilterToReq(filter);

const { data } = useTreeDetails(treeId ?? '', reqFilter);

const [treeDetailsData, setTreeDetailsData] = useState<ITreeDetails>();
const initialDataRef = useRef<TreeDetailsType | undefined>();
if (!initialDataRef.current) {
initialDataRef.current = data;

if (data && Object.keys(filter).length === 0) {
setFilter(createFilter(data));
}

const filterListElement = useMemo(
() => <TreeDetailsFilterList filter={filter} onFilter={setFilter} />,
[filter],
);

//TODO: at some point `treeUrl` should be returned in `data`
const treeUrl = useMemo(() => {
let url = '';
if (!data) return '';
Object.entries(data.builds).some(([, build]) => {
if (build.git_repository_url) {
url = build.git_repository_url;
return true;
}
});
return url;
}, [data]);

useEffect(() => {
if (data) {
const configsData: IListingItem[] = Object.entries(
Expand Down Expand Up @@ -95,11 +114,15 @@ const TreeDetails = (): JSX.Element => {
<div className="flex flex-col pb-2">
<div className="flex justify-end">
<TreeDetailsFilter
data={initialDataRef.current}
filter={filter}
onFilter={setFilter}
treeUrl={treeUrl}
/>
</div>
<TreeDetailsTab treeDetailsData={treeDetailsData} />
<TreeDetailsTab
treeDetailsData={treeDetailsData}
filterListElement={filterListElement}
/>
</div>
</div>
);
Expand Down
Loading
Loading