-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91c47ac
commit cd73fd3
Showing
5 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { graphql } from "src/graphql"; | ||
import { ItemDetailsQuery, RegistryItemsQuery } from "src/graphql/graphql"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useGraphqlBatcher } from "context/GraphqlBatcher"; | ||
export type { ItemDetailsQuery }; | ||
|
||
export const registryItemsQuery = graphql(` | ||
query RegistryItems($registryId: Bytes!, $first: Int, $skip: Int) { | ||
items(where: { registryAddress: $registryId }, orderBy: latestRequestSubmissionTime, first: $first, skip: $skip) { | ||
id | ||
status | ||
disputed | ||
props { | ||
label | ||
description | ||
value | ||
isIdentifier | ||
type | ||
} | ||
} | ||
} | ||
`); | ||
|
||
export const useRegistryItemsQuery = (id?: string) => { | ||
const { graphqlBatcher } = useGraphqlBatcher(); | ||
|
||
return useQuery<RegistryItemsQuery["items"]>({ | ||
queryKey: ["registryItemsQuery", id], | ||
// only fetch when download button clicked | ||
enabled: false, | ||
queryFn: async () => { | ||
let allData: RegistryItemsQuery["items"] = []; | ||
const first = 1000; | ||
let skip = 0; | ||
let keepFetching = true; | ||
|
||
try { | ||
while (keepFetching) { | ||
const data = await graphqlBatcher.fetch({ | ||
id: crypto.randomUUID(), | ||
document: registryItemsQuery, | ||
variables: { registryId: id, first, skip }, | ||
}); | ||
|
||
allData = allData.concat(data.items); | ||
|
||
// If less than 1000 items are returned, we have reached the end | ||
if (data.items.length < first) { | ||
keepFetching = false; | ||
} | ||
|
||
skip += first; | ||
} | ||
} catch (error) { | ||
console.error("Error fetching data:", error); | ||
} | ||
return allData; | ||
}, | ||
}); | ||
}; |
73 changes: 73 additions & 0 deletions
73
web/src/pages/AllLists/RegistryDetails/ItemsDownloadLabel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { useEffect, useRef, useState } from "react"; | ||
import styled from "styled-components"; | ||
import { useRegistryItemsQuery } from "queries/useRegistryAllItemsQuery"; | ||
import { json2csv } from "json-2-csv"; | ||
import ExportIcon from "svgs/icons/export.svg"; | ||
|
||
const StyledLink = styled.a` | ||
display: flex; | ||
flex-direction: row; | ||
align-items: end; | ||
gap: 8px; | ||
text-decoration: none; | ||
color: ${({ theme }) => theme.primaryBlue}; | ||
margin-top: 48px; | ||
align-self: flex-end; | ||
cursor: pointer; | ||
`; | ||
|
||
const StyledExportIcon = styled(ExportIcon)` | ||
path { | ||
stroke: ${({ theme }) => theme.primaryBlue}; | ||
} | ||
`; | ||
|
||
const ItemsDownloadLabel: React.FC<{ registryAddress?: string }> = ({ registryAddress }) => { | ||
const [isPreparing, setIsPreparing] = useState(false); | ||
const ref = useRef<HTMLAnchorElement>(null); | ||
|
||
const { data: items, refetch, isRefetching } = useRegistryItemsQuery(registryAddress); | ||
|
||
useEffect(() => { | ||
if (!items || !ref.current) return; | ||
setIsPreparing(true); | ||
const flattenedItems = items.flatMap((item) => | ||
item.props.map((prop) => ({ | ||
id: item.id, | ||
status: item.status, | ||
disputed: item.disputed, | ||
propLabel: prop.label, | ||
propDescription: prop.description, | ||
propValue: prop.value, | ||
propIsIdentifier: prop.isIdentifier, | ||
propType: prop.type, | ||
})) | ||
); | ||
const csvData = json2csv(flattenedItems); | ||
const blob = new Blob([csvData], { type: "text/csv;charset=utf-8;" }); | ||
const link = ref.current; | ||
|
||
if (link.download !== undefined) { | ||
const url = URL.createObjectURL(blob); | ||
link.setAttribute("href", url); | ||
link.setAttribute("download", `${registryAddress}.csv`); | ||
link.click(); | ||
} | ||
|
||
setIsPreparing(false); | ||
}, [items]); | ||
|
||
return ( | ||
<StyledLink onClick={() => refetch()} aria-disabled={isRefetching} ref={ref}> | ||
{isRefetching || isPreparing ? ( | ||
<>Exporting list...</> | ||
) : ( | ||
<> | ||
Export as csv <StyledExportIcon /> | ||
</> | ||
)} | ||
</StyledLink> | ||
); | ||
}; | ||
|
||
export default ItemsDownloadLabel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters