|
| 1 | +/*! |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { Box, Heading, Flex, HStack, VStack, StackSeparator, Skeleton } from "@chakra-ui/react"; |
| 20 | +import { createListCollection } from "@chakra-ui/react/collection"; |
| 21 | + |
| 22 | +import { useAssetServiceGetAssetEvents } from "openapi/queries"; |
| 23 | +import { MetricsBadge } from "src/components/MetricsBadge"; |
| 24 | +import { Select } from "src/components/ui"; |
| 25 | + |
| 26 | +import { AssetEvent } from "./AssetEvent"; |
| 27 | + |
| 28 | +type AssetEventProps = { |
| 29 | + readonly assetSortBy: string; |
| 30 | + readonly endDate: string; |
| 31 | + readonly setAssetSortBy: React.Dispatch<React.SetStateAction<string>>; |
| 32 | + readonly startDate: string; |
| 33 | +}; |
| 34 | + |
| 35 | +export const AssetEvents = ({ assetSortBy, endDate, setAssetSortBy, startDate }: AssetEventProps) => { |
| 36 | + const { data, isLoading } = useAssetServiceGetAssetEvents({ |
| 37 | + limit: 6, |
| 38 | + orderBy: assetSortBy, |
| 39 | + timestampGte: startDate, |
| 40 | + timestampLte: endDate, |
| 41 | + }); |
| 42 | + |
| 43 | + const assetSortOptions = createListCollection({ |
| 44 | + items: [ |
| 45 | + { label: "Newest first", value: "-timestamp" }, |
| 46 | + { label: "Oldest first", value: "timestamp" }, |
| 47 | + ], |
| 48 | + }); |
| 49 | + |
| 50 | + return ( |
| 51 | + <Box borderRadius={5} borderWidth={1} ml={2} pb={2}> |
| 52 | + <Flex justify="space-between" mr={1} mt={0} pl={3} pt={1}> |
| 53 | + <HStack> |
| 54 | + <MetricsBadge backgroundColor="blue.solid" runs={isLoading ? 0 : data?.total_entries} /> |
| 55 | + <Heading marginEnd="auto" size="md"> |
| 56 | + Asset Events |
| 57 | + </Heading> |
| 58 | + </HStack> |
| 59 | + <Select.Root |
| 60 | + borderWidth={0} |
| 61 | + collection={assetSortOptions} |
| 62 | + data-testid="asset-sort-duration" |
| 63 | + defaultValue={["-timestamp"]} |
| 64 | + onValueChange={(option) => setAssetSortBy(option.value[0] as string)} |
| 65 | + width={130} |
| 66 | + > |
| 67 | + <Select.Trigger> |
| 68 | + <Select.ValueText placeholder="Sort by" /> |
| 69 | + </Select.Trigger> |
| 70 | + |
| 71 | + <Select.Content> |
| 72 | + {assetSortOptions.items.map((option) => ( |
| 73 | + <Select.Item item={option} key={option.value[0]}> |
| 74 | + {option.label} |
| 75 | + </Select.Item> |
| 76 | + ))} |
| 77 | + </Select.Content> |
| 78 | + </Select.Root> |
| 79 | + </Flex> |
| 80 | + {isLoading ? ( |
| 81 | + <VStack px={3} separator={<StackSeparator />}> |
| 82 | + {Array.from({ length: 5 }, (_, index) => index).map((index) => ( |
| 83 | + <Skeleton height={100} key={index} width="full" /> |
| 84 | + ))} |
| 85 | + </VStack> |
| 86 | + ) : ( |
| 87 | + <VStack px={3} separator={<StackSeparator />}> |
| 88 | + {data?.asset_events.map((event) => <AssetEvent event={event} key={event.id} />)} |
| 89 | + </VStack> |
| 90 | + )} |
| 91 | + </Box> |
| 92 | + ); |
| 93 | +}; |
0 commit comments