-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Notifications, contacts, bug fixes (#117)
* add invite message * add invite message * small improvement to username * simulation fails * test chaining * test chaining * add double transact * lower verification level * chaining test * permissions! * clean up * fix error * add release update
- Loading branch information
1 parent
43be099
commit 5cc1849
Showing
15 changed files
with
378 additions
and
82 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
145 changes: 145 additions & 0 deletions
145
demo/with-next/components/ClientContent/RequestPermissions.tsx
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,145 @@ | ||
import { | ||
MiniKit, | ||
RequestPermissionErrorCodes, | ||
ResponseEvent, | ||
Contact, | ||
RequestPermissionPayload, | ||
Permission, | ||
} from "@worldcoin/minikit-js"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { validateSchema } from "./helpers/validate-schema"; | ||
import * as yup from "yup"; | ||
|
||
const requestPermissionSuccessPayloadSchema = yup.object({ | ||
status: yup.string<"success">().equals(["success"]).required(), | ||
version: yup.number().required(), | ||
permission: yup.string<Permission>().oneOf(Object.values(Permission)), | ||
timestamp: yup.string().required(), | ||
}); | ||
|
||
const requestPermissionErrorPayloadSchema = yup.object({ | ||
error_code: yup | ||
.string<RequestPermissionErrorCodes>() | ||
.oneOf(Object.values(RequestPermissionErrorCodes)) | ||
.required(), | ||
description: yup.string().required(), | ||
status: yup.string<"error">().equals(["error"]).required(), | ||
version: yup.number().required(), | ||
}); | ||
|
||
export const RequestPermission = () => { | ||
const [requestPermissionAppPayload, setRequestPermissionAppPayload] = | ||
useState<string | undefined>(); | ||
|
||
const [ | ||
requestPermissionPayloadValidationMessage, | ||
setRequestPermissionPayloadValidationMessage, | ||
] = useState<string | null>(); | ||
|
||
const [sentRequestPermissionPayload, setSentRequestPermissionPayload] = | ||
useState<Record<string, any> | null>(null); | ||
|
||
const [tempInstallFix, setTempInstallFix] = useState(0); | ||
|
||
useEffect(() => { | ||
if (!MiniKit.isInstalled()) { | ||
return; | ||
} | ||
|
||
MiniKit.subscribe( | ||
ResponseEvent.MiniAppRequestPermission, | ||
async (payload) => { | ||
console.log("MiniAppRequestPermission, SUBSCRIBE PAYLOAD", payload); | ||
setRequestPermissionAppPayload(JSON.stringify(payload, null, 2)); | ||
if (payload.status === "error") { | ||
const errorMessage = await validateSchema( | ||
requestPermissionErrorPayloadSchema, | ||
payload | ||
); | ||
|
||
if (!errorMessage) { | ||
setRequestPermissionPayloadValidationMessage("Payload is valid"); | ||
} else { | ||
setRequestPermissionPayloadValidationMessage(errorMessage); | ||
} | ||
} else { | ||
const errorMessage = await validateSchema( | ||
requestPermissionSuccessPayloadSchema, | ||
payload | ||
); | ||
|
||
// This checks if the response format is correct | ||
if (!errorMessage) { | ||
setRequestPermissionPayloadValidationMessage("Payload is valid"); | ||
} else { | ||
setRequestPermissionPayloadValidationMessage(errorMessage); | ||
} | ||
} | ||
} | ||
); | ||
|
||
return () => { | ||
MiniKit.unsubscribe(ResponseEvent.MiniAppRequestPermission); | ||
}; | ||
}, [tempInstallFix]); | ||
|
||
const onRequestPermission = useCallback(async (permission: Permission) => { | ||
const requestPermissionPayload: RequestPermissionPayload = { | ||
permission, | ||
}; | ||
|
||
const payload = MiniKit.commands.requestPermission( | ||
requestPermissionPayload | ||
); | ||
setSentRequestPermissionPayload({ | ||
payload, | ||
}); | ||
console.log("payload", payload); | ||
setTempInstallFix((prev) => prev + 1); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<div className="grid gap-y-2"> | ||
<h2 className="text-2xl font-bold">Request Permission</h2> | ||
|
||
<div> | ||
<div className="bg-gray-300 min-h-[100px] p-2"> | ||
<pre className="break-all whitespace-break-spaces"> | ||
{JSON.stringify(sentRequestPermissionPayload, null, 2)} | ||
</pre> | ||
</div> | ||
</div> | ||
<div className="grid gap-4 grid-cols-2"> | ||
<button | ||
className="bg-black text-white rounded-lg p-4 w-full" | ||
onClick={() => onRequestPermission(Permission.Notifications)} | ||
> | ||
Request Notifications | ||
</button> | ||
</div> | ||
</div> | ||
|
||
<hr /> | ||
|
||
<div className="w-full grid gap-y-2"> | ||
<p> | ||
Message from "{ResponseEvent.MiniAppRequestPermission}"{" "} | ||
</p> | ||
|
||
<div className="bg-gray-300 min-h-[100px] p-2"> | ||
<pre className="break-all whitespace-break-spaces"> | ||
{requestPermissionAppPayload ?? JSON.stringify(null)} | ||
</pre> | ||
</div> | ||
|
||
<div className="grid gap-y-2"> | ||
<p>Response Validation:</p> | ||
<p className="bg-gray-300 p-2"> | ||
{requestPermissionPayloadValidationMessage ?? "No validation"} | ||
</p> | ||
</div> | ||
</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
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
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
export const getUserProfile = async (address: string) => { | ||
const res = await fetch('https://usernames.worldcoin.org/api/v1/query', { | ||
method: 'POST', | ||
const res = await fetch("https://usernames.worldcoin.org/api/v1/query", { | ||
method: "POST", | ||
headers: { | ||
'Content-Type': 'application/json' | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
addresses: [address] | ||
}) | ||
}) | ||
addresses: [address], | ||
}), | ||
}); | ||
|
||
const usernames = await res.json(); | ||
return usernames[0] ?? { username: null, profilePictureUrl: null }; | ||
return usernames?.[0] ?? { username: null, profilePictureUrl: null }; | ||
}; |
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
Oops, something went wrong.