-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from DCFoundation/install-bundle-follower
feat(installBundle): add :bundles follower to confirm installation
- Loading branch information
Showing
5 changed files
with
552 additions
and
19 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
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,49 @@ | ||
import { toast } from "react-toastify"; | ||
import { ClipboardDocumentIcon } from "@heroicons/react/20/solid"; | ||
import { XMarkIcon } from "@heroicons/react/20/solid"; | ||
|
||
export const BundleFollowerToastMessage = ({ | ||
endoZipBase64Sha512, | ||
closeToast = () => {}, | ||
clipboard, | ||
}: { | ||
endoZipBase64Sha512: string; | ||
closeToast: () => void; | ||
clipboard?: Navigator["clipboard"]; | ||
}) => ( | ||
<div className="flex items-start pointer-events-auto"> | ||
<div className="ml-3 w-0 flex-1 pt-0.5"> | ||
<p className="text-sm font-medium text-gray-900"> | ||
Bundle Successfully Installed! | ||
</p> | ||
{clipboard && ( | ||
<span className="mt-1 text-sm text-gray-500"> | ||
<span | ||
className="text-sm text-blue-500 hover:text-blue-700 underline cursor-pointer" | ||
onClick={async () => { | ||
await clipboard.writeText(endoZipBase64Sha512); | ||
toast.info("Copied to clipboard!", { | ||
position: "bottom-center", | ||
autoClose: 3000, | ||
hideProgressBar: true, | ||
}); | ||
}} | ||
> | ||
EndoZipBase64Sha512{" "} | ||
<ClipboardDocumentIcon className="inline-block w-4 h-4" /> | ||
</span> | ||
</span> | ||
)} | ||
</div> | ||
<div className="ml-4 flex flex-shrink-0"> | ||
<button | ||
type="button" | ||
className="inline-flex rounded-md bg-white text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-teal-500 focus:ring-offset-2" | ||
onClick={closeToast} | ||
> | ||
<span className="sr-only">Close</span> | ||
<XMarkIcon className="h-5 w-5" aria-hidden="true" /> | ||
</button> | ||
</div> | ||
</div> | ||
); |
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
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,63 @@ | ||
import { | ||
iterateEach, | ||
makeCastingSpec, | ||
makeFollower, | ||
makeLeader, | ||
} from "@agoric/casting"; | ||
import { toast } from "react-toastify"; | ||
import { BundleFollowerToastMessage } from "../components/BundleFollowerToastMessage"; | ||
|
||
type IteratorEnvelope = { | ||
value?: { | ||
endoZipBase64Sha512: string; | ||
installed: boolean; | ||
error: unknown; | ||
}; | ||
error?: { message: string; stack: string }; | ||
}; | ||
|
||
export const useWatchBundle = ( | ||
rpcUrl: string | undefined, | ||
{ clipboard }: { clipboard?: Navigator["clipboard"] }, | ||
) => { | ||
const leader = rpcUrl ? makeLeader(rpcUrl) : undefined; | ||
|
||
const watchBundle = async ( | ||
expectedEndoZipBase64Sha512: string, | ||
{ height }: { height: number }, | ||
) => { | ||
if (!leader) throw Error("Unexpected error: leader not found."); | ||
const castingSpec = makeCastingSpec(":bundles"); | ||
const follower = makeFollower(castingSpec, leader); | ||
for await (const envelope of iterateEach(follower, { height })) { | ||
const { value, error } = envelope as IteratorEnvelope; | ||
if (!value && error) { | ||
toast.error(`Bundle installation failed.\nSee console for details.`); | ||
console.log(envelope); | ||
throw error; | ||
} | ||
if (value) { | ||
const { endoZipBase64Sha512, installed, error } = value; | ||
if (endoZipBase64Sha512 === expectedEndoZipBase64Sha512) { | ||
if (!installed) { | ||
toast.error( | ||
`Bundle installation failed.\nSee console for details.`, | ||
); | ||
throw error; | ||
} else { | ||
toast.success(({ closeToast }) => ( | ||
<BundleFollowerToastMessage | ||
endoZipBase64Sha512={endoZipBase64Sha512} | ||
closeToast={closeToast as () => void} | ||
clipboard={clipboard} | ||
/> | ||
)); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
return watchBundle; | ||
}; |
Oops, something went wrong.