-
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.
* share contacts command (#103) * dev * usernames example * remove username file * links test * update url * add path param * Add Send Haptic Feedback command (#82) * Add Send Haptic Feedback command * add haptics to demo, improve dx * fix: add haptic feedback async handler, simplify payloads * Revert "Add Send Haptic Feedback command (#82)" (#114) This reverts commit 072798a. --------- Co-authored-by: Michał Struck <[email protected]>
- Loading branch information
1 parent
be66b05
commit bae6d76
Showing
8 changed files
with
305 additions
and
2 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/ShareContacts.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, | ||
ShareContactsErrorCodes, | ||
ResponseEvent, | ||
Contact, | ||
ShareContactsPayload, | ||
} from "@worldcoin/minikit-js"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { validateSchema } from "./helpers/validate-schema"; | ||
import * as yup from "yup"; | ||
|
||
const shareContactsSuccessPayloadSchema = yup.object({ | ||
status: yup.string<"success">().equals(["success"]).required(), | ||
version: yup.number().required(), | ||
contacts: yup.array().of(yup.object<Contact>().required()), | ||
}); | ||
|
||
const shareContactsErrorPayloadSchema = yup.object({ | ||
error_code: yup | ||
.string<ShareContactsErrorCodes>() | ||
.oneOf(Object.values(ShareContactsErrorCodes)) | ||
.required(), | ||
status: yup.string<"error">().equals(["error"]).required(), | ||
version: yup.number().required(), | ||
}); | ||
|
||
export const ShareContacts = () => { | ||
const [shareContactsAppPayload, setShareContactsAppPayload] = useState< | ||
string | undefined | ||
>(); | ||
|
||
const [ | ||
shareContactsPayloadValidationMessage, | ||
setShareContactsPayloadValidationMessage, | ||
] = useState<string | null>(); | ||
|
||
const [sentShareContactsPayload, setSentShareContactsPayload] = | ||
useState<Record<string, any> | null>(null); | ||
|
||
const [tempInstallFix, setTempInstallFix] = useState(0); | ||
|
||
useEffect(() => { | ||
if (!MiniKit.isInstalled()) { | ||
return; | ||
} | ||
|
||
MiniKit.subscribe(ResponseEvent.MiniAppShareContacts, async (payload) => { | ||
console.log("MiniAppShareContacts, SUBSCRIBE PAYLOAD", payload); | ||
setShareContactsAppPayload(JSON.stringify(payload, null, 2)); | ||
if (payload.status === "error") { | ||
const errorMessage = await validateSchema( | ||
shareContactsErrorPayloadSchema, | ||
payload | ||
); | ||
|
||
if (!errorMessage) { | ||
setShareContactsPayloadValidationMessage("Payload is valid"); | ||
} else { | ||
setShareContactsPayloadValidationMessage(errorMessage); | ||
} | ||
} else { | ||
const errorMessage = await validateSchema( | ||
shareContactsSuccessPayloadSchema, | ||
payload | ||
); | ||
|
||
// This checks if the response format is correct | ||
if (!errorMessage) { | ||
setShareContactsPayloadValidationMessage("Payload is valid"); | ||
} else { | ||
setShareContactsPayloadValidationMessage(errorMessage); | ||
} | ||
} | ||
}); | ||
|
||
return () => { | ||
MiniKit.unsubscribe(ResponseEvent.MiniAppShareContacts); | ||
}; | ||
}, [tempInstallFix]); | ||
|
||
const onShareContacts = useCallback( | ||
async (isMultiSelectEnabled: boolean = false) => { | ||
const shareContactsPayload: ShareContactsPayload = { | ||
isMultiSelectEnabled, | ||
}; | ||
|
||
const payload = MiniKit.commands.shareContacts(shareContactsPayload); | ||
setSentShareContactsPayload({ | ||
payload, | ||
}); | ||
console.log("payload", payload); | ||
setTempInstallFix((prev) => prev + 1); | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<div> | ||
<div className="grid gap-y-2"> | ||
<h2 className="text-2xl font-bold">Share Contacts</h2> | ||
|
||
<div> | ||
<div className="bg-gray-300 min-h-[100px] p-2"> | ||
<pre className="break-all whitespace-break-spaces"> | ||
{JSON.stringify(sentShareContactsPayload, 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={() => onShareContacts(true)} | ||
> | ||
Share Contacts (Multi enabled) | ||
</button> | ||
<button | ||
className="bg-black text-white rounded-lg p-4 w-full" | ||
onClick={() => onShareContacts(false)} | ||
> | ||
Share Contacts (multi disabled) | ||
</button> | ||
</div> | ||
</div> | ||
|
||
<hr /> | ||
|
||
<div className="w-full grid gap-y-2"> | ||
<p>Message from "{ResponseEvent.MiniAppShareContacts}" </p> | ||
|
||
<div className="bg-gray-300 min-h-[100px] p-2"> | ||
<pre className="break-all whitespace-break-spaces"> | ||
{shareContactsAppPayload ?? JSON.stringify(null)} | ||
</pre> | ||
</div> | ||
|
||
<div className="grid gap-y-2"> | ||
<p>Response Validation:</p> | ||
<p className="bg-gray-300 p-2"> | ||
{shareContactsPayloadValidationMessage ?? "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {}; | ||
const nextConfig = { | ||
images: { | ||
domains: ["static.usernames.app-backend.toolsforhumanity.com"], | ||
}, | ||
}; | ||
|
||
export default nextConfig; |
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
Oops, something went wrong.