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

Rifeljm/vc is valid lazy get proofs #1849

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
54 changes: 51 additions & 3 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,42 @@ 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(() => {});
paninaro marked this conversation as resolved.
Show resolved Hide resolved

React.useEffect(() => {
if (blockchainVCs?.vcRecords) {
Promise.all(
blockchainVCs.vcRecords
.filter((vcRecord: any) => !!vcRecord.vc?.proofHash)
.map(
(vcRecord) =>
new Promise((resolve, reject) => {
getProofsForRoot(vcRecord.vc?.proofHash).then((result: any) => {
if (proofs) {
resolve([vcRecord.vc?.launcherId, result.data?.proofs]);
} else {
reject();
}
});
})
)
).then((results) => {
const proofsObj: any = {};
results.forEach((result: any) => {
const vcId = result[0];
const p = result[1];
proofsObj[vcId] = p;
});
setProofs(proofsObj);
});
}
}, [blockchainVCs, getProofsForRoot, proofs]);

const COMPONENTS = {
Item: ItemContainer,
List: ListContainer,
Expand All @@ -99,10 +141,16 @@ export default function VCList() {
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.launcherId] && Object.keys(proofs[record.vc.launcherId]).length > 0),
}))
.concat(VCsLocalStorage[fingerprint])
.filter(Boolean);
}
return [];
}, [VCsLocalStorage, blockchainVCs?.vcRecords, fingerprint]);
}, [VCsLocalStorage, blockchainVCs?.vcRecords, fingerprint, proofs]);

if (isLoading) return null;

Expand Down