mirror of
https://github.com/luxfhe/example-redact.git
synced 2026-10-30 18:13:20 +00:00
- @luxfhe/wasm: ^0.2.0 - @luxfhe/kms: ^0.2.0 - @luxfhe/relayer-sdk: ^0.6.0 - @luxfhe/mock-contracts: ^0.6.0 - Remove deprecated Zama dependencies
86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
import { useMemo } from "react";
|
|
import { DisplayValue } from "./DisplayValue";
|
|
import { FheTypes, UnsealedItem } from "@luxfhe/sdk/web";
|
|
import { EyeOff, LoaderCircle } from "lucide-react";
|
|
import { formatTokenAmount } from "~~/lib/common";
|
|
import { cn } from "~~/lib/utils";
|
|
import { useDecryptValue } from "~~/services/store/decrypted";
|
|
|
|
export function EncryptedValue<T extends FheTypes>({
|
|
fheType,
|
|
ctHash,
|
|
className,
|
|
transform,
|
|
showIcon = true,
|
|
}: {
|
|
fheType: T;
|
|
ctHash: bigint | null | undefined;
|
|
className?: string;
|
|
transform: (value: UnsealedItem<T>) => string;
|
|
showIcon?: boolean;
|
|
}) {
|
|
const decryptedValue = useDecryptValue(fheType, ctHash);
|
|
const { value, state } = decryptedValue;
|
|
|
|
const display = useMemo(() => {
|
|
if (ctHash == null) return "...";
|
|
if (value == null) return "XXXXXX";
|
|
return transform(value);
|
|
}, [ctHash, transform, value]);
|
|
|
|
return (
|
|
<DisplayValue
|
|
value={display}
|
|
icon={
|
|
showIcon ? (
|
|
state === "pending" ? (
|
|
<LoaderCircle className="w-5 h-5 animate-spin" />
|
|
) : (
|
|
<EyeOff className="w-5 h-5" />
|
|
)
|
|
) : undefined
|
|
}
|
|
className={cn(
|
|
"text-primary min-w-32",
|
|
(state === "pending" || state === "error") && "text-primary-foreground",
|
|
className,
|
|
)}
|
|
onClick={() => {
|
|
console.log("Encrypted Value:", decryptedValue);
|
|
}}
|
|
>
|
|
<div
|
|
className={cn(
|
|
"absolute right-0 h-full bg-primary transition-all",
|
|
state === "pending" || state === "error" ? "w-full" : "w-0",
|
|
state === "error" && "bg-destructive",
|
|
)}
|
|
/>
|
|
</DisplayValue>
|
|
);
|
|
}
|
|
|
|
export function EncryptedBalance({
|
|
ctHash,
|
|
decimals = 18,
|
|
precision,
|
|
className,
|
|
showIcon = true,
|
|
}: {
|
|
ctHash: bigint | null | undefined;
|
|
decimals?: number;
|
|
precision?: number;
|
|
className?: string;
|
|
showIcon?: boolean;
|
|
}) {
|
|
return (
|
|
<EncryptedValue
|
|
fheType={FheTypes.Uint128}
|
|
ctHash={ctHash}
|
|
transform={value => formatTokenAmount(value, decimals, precision)}
|
|
className={className}
|
|
showIcon={showIcon}
|
|
/>
|
|
);
|
|
}
|