fix: crossbell.js connect on load

This commit is contained in:
DIYgod 2022-08-07 17:00:03 +01:00
parent b12f22d67d
commit 51f4c0e627
No known key found for this signature in database
GPG Key ID: D159328F47A80DCA
2 changed files with 18 additions and 11 deletions

View File

@ -1,12 +1,15 @@
import { Contract, Indexer } from "crossbell.js"
const indexer = new Indexer()
const contract =
typeof window !== "undefined"
? new Contract((<any>window).ethereum)
: undefined
if (contract) {
contract.connect()
let contract: Contract | undefined
const getContract = () => {
if (!contract && typeof window !== "undefined") {
contract = new Contract((<any>window).ethereum)
contract.connect()
}
return contract
}
export { indexer, contract }
export { indexer, getContract }

View File

@ -13,7 +13,7 @@ import { PageVisibilityEnum, Notes } from "~/lib/types"
import { isUUID } from "~/lib/uuid"
import { stripHTML } from "~/lib/utils"
import unidata from "~/lib/unidata"
import { indexer, contract } from "~/lib/crossbell"
import { indexer, getContract } from "~/lib/crossbell"
const checkPageSlug = async ({
slug,
@ -305,7 +305,7 @@ export async function likePage({
if (!characterId) {
throw notFound(`character not found`)
} else {
return contract?.linkNote(
return getContract()?.linkNote(
characterId,
pageId.split("-")[0],
pageId.split("-")[1],
@ -325,7 +325,7 @@ export async function unlikePage({
if (!characterId) {
throw notFound(`character not found`)
} else {
return contract?.unlinkNote(
return getContract()?.unlinkNote(
characterId,
pageId.split("-")[0],
pageId.split("-")[1],
@ -371,7 +371,11 @@ export async function mintPage({
address: string
pageId: string
}) {
return contract?.mintNote(pageId.split("-")[0], pageId.split("-")[1], address)
return getContract()?.mintNote(
pageId.split("-")[0],
pageId.split("-")[1],
address,
)
}
export async function getMints({ pageId }: { pageId: string }) {