1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00
unleash.unleash/frontend/src/component/application/ApplicationList/ApplicationList.tsx

202 lines
7.2 KiB
TypeScript
Raw Normal View History

2023-08-18 13:55:23 +02:00
import { useMemo } from 'react';
import { Avatar, CircularProgress, Icon, Link } from '@mui/material';
import { Warning } from '@mui/icons-material';
2023-08-18 13:55:23 +02:00
import { styles as themeStyles } from 'component/common';
import { PageContent } from 'component/common/PageContent/PageContent';
import { PageHeader } from 'component/common/PageHeader/PageHeader';
2022-03-28 10:49:59 +02:00
import useApplications from 'hooks/api/getters/useApplications/useApplications';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { Search } from 'component/common/Search/Search';
2023-08-18 13:55:23 +02:00
import { SearchHighlightProvider } from 'component/common/Table/SearchHighlightContext/SearchHighlightContext';
import {
SortableTableHeader,
Table,
TableBody,
TableCell,
TableRow,
} from 'component/common/Table';
import { useGlobalFilter, useSortBy, useTable } from 'react-table';
import { sortTypes } from 'utils/sortTypes';
import { IconCell } from 'component/common/Table/cells/IconCell/IconCell';
import { LinkCell } from 'component/common/Table/cells/LinkCell/LinkCell';
import { ApplicationUsageCell } from './ApplicationUsageCell/ApplicationUsageCell';
import { ApplicationSchema } from 'openapi';
export const ApplicationList = () => {
2023-08-18 13:55:23 +02:00
const { applications: data, loading } = useApplications();
const renderNoApplications = () => (
2022-02-09 16:15:07 +01:00
<>
<section style={{ textAlign: 'center' }}>
<Warning titleAccess='Warning' /> <br />
<br />
Oh snap, it does not seem like you have connected any
applications. To connect your application to Unleash you will
require a Client SDK.
<br />
<br />
You can read more about how to use Unleash in your application
in the{' '}
<Link href='https://docs.getunleash.io/docs/sdks/'>
documentation.
</Link>
</section>
2022-02-09 16:15:07 +01:00
</>
);
2023-08-18 13:55:23 +02:00
const initialState = useMemo(
() => ({
sortBy: [{ id: 'name', desc: false }],
hiddenColumns: ['description', 'sortOrder'],
}),
[],
2023-08-18 13:55:23 +02:00
);
const columns = useMemo(
() => [
{
id: 'Icon',
Cell: ({
row: {
original: { icon },
},
}: any) => (
2023-08-18 13:55:23 +02:00
<IconCell
icon={
<Avatar>
<Icon>{icon || 'apps'}</Icon>
2023-08-18 13:55:23 +02:00
</Avatar>
}
/>
),
disableGlobalFilter: true,
},
{
Header: 'Name',
accessor: 'appName',
width: '50%',
Cell: ({
row: {
original: { appName, description },
},
2023-08-18 13:55:23 +02:00
}: any) => (
<LinkCell
title={appName}
to={`/applications/${appName}`}
subtitle={description}
/>
),
sortType: 'alphanumeric',
},
{
Header: 'Project(environment)',
accessor: 'usage',
width: '50%',
Cell: ({
row: { original },
}: {
row: { original: ApplicationSchema };
}) => <ApplicationUsageCell usage={original.usage} />,
2023-08-18 13:55:23 +02:00
sortType: 'alphanumeric',
},
{
accessor: 'description',
disableSortBy: true,
},
{
accessor: 'sortOrder',
disableGlobalFilter: true,
sortType: 'number',
},
],
[],
2023-08-18 13:55:23 +02:00
);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
state: { globalFilter },
setGlobalFilter,
} = useTable(
{
columns: columns as any[], // TODO: fix after `react-table` v8 update
data,
initialState,
sortTypes,
autoResetGlobalFilter: false,
autoResetSortBy: false,
disableSortRemove: true,
},
useGlobalFilter,
useSortBy,
2023-08-18 13:55:23 +02:00
);
if (!data) {
return <CircularProgress variant='indeterminate' />;
}
return (
<>
<PageContent
header={
<PageHeader
2023-08-18 13:55:23 +02:00
title={`Applications (${rows.length})`}
actions={
<Search
2023-08-18 13:55:23 +02:00
initialValue={globalFilter}
onChange={setGlobalFilter}
/>
}
/>
}
>
<div className={themeStyles.fullwidth}>
2022-02-10 10:05:50 +01:00
<ConditionallyRender
2023-08-18 13:55:23 +02:00
condition={data.length > 0}
show={
<SearchHighlightProvider value={globalFilter}>
<Table {...getTableProps()}>
<SortableTableHeader
headerGroups={headerGroups}
/>
<TableBody {...getTableBodyProps()}>
{rows.map((row) => {
2023-08-18 13:55:23 +02:00
prepareRow(row);
return (
<TableRow
hover
{...row.getRowProps()}
>
{row.cells.map((cell) => (
2023-08-18 13:55:23 +02:00
<TableCell
{...cell.getCellProps()}
>
{cell.render(
'Cell',
2023-08-18 13:55:23 +02:00
)}
</TableCell>
))}
</TableRow>
);
})}
</TableBody>
</Table>
</SearchHighlightProvider>
}
2022-02-10 10:05:50 +01:00
elseShow={
<ConditionallyRender
condition={loading}
show={<div>...loading</div>}
elseShow={renderNoApplications()}
2022-02-10 10:05:50 +01:00
/>
}
/>
</div>
</PageContent>
</>
);
};