Skip to content

Commit

Permalink
Merge pull request #1849 from Chia-Network/rifeljm/vc-is-valid-lazy-g…
Browse files Browse the repository at this point in the history
…et-proofs
  • Loading branch information
paninaro authored Jun 15, 2023
2 parents 2960b7f + adf2c01 commit 1d5f7f2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/api-react/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export { default as useNFTCoinAdded } from './useNFTCoinAdded';
export { default as useNFTCoinRemoved } from './useNFTCoinRemoved';
export { default as useNFTCoinUpdated } from './useNFTCoinUpdated';
export { default as useNFTCoinDIDSet } from './useNFTCoinDIDSet';
export { useVCCoinAdded, useVCCoinRemoved } from './useVCEvents';
11 changes: 11 additions & 0 deletions packages/api-react/src/hooks/useVCEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { VC } from '@chia-network/api';

import useSubscribeToEvent from './useSubscribeToEvent';

export function useVCCoinAdded(callback: (coin: any) => void) {
return useSubscribeToEvent('onVCCoinAdded', VC, callback);
}

export function useVCCoinRemoved(callback: (coin: any) => void) {
return useSubscribeToEvent('onVCCoinRemoved', VC, callback);
}
1 change: 1 addition & 0 deletions packages/api-react/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export const {
useSpendVCMutation,
useAddVCProofsMutation,
useGetProofsForRootQuery,
useLazyGetProofsForRootQuery,
useRevokeVCMutation,
// clawback
useSetAutoClaimMutation,
Expand Down
1 change: 1 addition & 0 deletions packages/api-react/src/services/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,7 @@ export const {
useSpendVCMutation,
useAddVCProofsMutation,
useGetProofsForRootQuery,
useLazyGetProofsForRootQuery,
useRevokeVCMutation,
// clawback
useSetAutoClaimMutation,
Expand Down
2 changes: 1 addition & 1 deletion packages/gui/src/components/vcs/VCCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof
<Trans>Expired</Trans>
) : vcRecord.revoked ? (
<Trans>Revoked</Trans>
) : proofs && Object.keys(proofs).length ? (
) : vcRecord.isValid || !!(proofs && Object.keys(proofs).length > 0) ? (
<Trans>Valid</Trans>
) : (
<Trans>Invalid</Trans>
Expand Down
53 changes: 49 additions & 4 deletions packages/gui/src/components/vcs/VCList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useGetVCListQuery, useLocalStorage, useGetLoggedInFingerprintQuery } from '@chia-network/api-react';
import {
useGetVCListQuery,
useLocalStorage,
useGetLoggedInFingerprintQuery,
useLazyGetProofsForRootQuery,
useVCCoinAdded,
} from '@chia-network/api-react';
import { Flex, More, MenuItem, AlertDialog, useOpenDialog, useDarkMode } from '@chia-network/core';
import {
VCZeroStateBackground as VCZeroStateBackgroundIcon,
Expand Down Expand Up @@ -74,6 +80,37 @@ export default function VCList() {

const { isDarkMode } = useDarkMode();

const [getProofsForRoot] = useLazyGetProofsForRootQuery();

const [proofs, setProofs] = React.useState<any>({});

/* We only need to subscribe to event and the list will be updated automatically on added VC */
useVCCoinAdded(() => {});

React.useEffect(() => {
async function loadProofsData() {
if (isLoading || !blockchainVCs) {
return;
}

const mapping: Record<string, any> = {};

const proofDataPromises = blockchainVCs.vcRecords.map(async (vcRecord: any) => {
const proofHash = (vcRecord as any)?.vc?.proofHash;
if (proofHash) {
const { data } = await getProofsForRoot({ root: proofHash });
const vcProofs = data?.proofs;
mapping[proofHash] = vcProofs;
}
});
await Promise.all(proofDataPromises);

setProofs(mapping);
}

loadProofsData();
}, [isLoading, blockchainVCs, getProofsForRoot]);

const COMPONENTS = {
Item: ItemContainer,
List: ListContainer,
Expand All @@ -93,16 +130,24 @@ export default function VCList() {
}

function renderVCCard(index: number, vcRecord: any) {
return <VCCard vcRecord={vcRecord} />;
const proofHash = vcRecord?.vc?.proofHash;
const vcProofs = proofHash ? proofs[proofHash] : undefined;
return <VCCard vcRecord={vcRecord} proofs={vcProofs} />;
}

const allVCs = React.useMemo(() => {
if (fingerprint) {
// filter out undefined values
return (blockchainVCs?.vcRecords || []).concat(VCsLocalStorage[fingerprint]).filter(Boolean);
return blockchainVCs?.vcRecords
.map((record) => ({
...record,
isValid: !!(proofs[record.vc.proofHash] && Object.keys(proofs[record.vc.proofHash]).length > 0),
}))
.concat(VCsLocalStorage[fingerprint])
.filter(Boolean);
}
return [];
}, [VCsLocalStorage, blockchainVCs?.vcRecords, fingerprint]);
}, [VCsLocalStorage, blockchainVCs?.vcRecords, fingerprint, proofs]);

if (isLoading) return null;

Expand Down

0 comments on commit 1d5f7f2

Please sign in to comment.