|
| 1 | +import Image from "next/image"; |
| 2 | + |
| 3 | +# Post Action Mods |
| 4 | + |
| 5 | +Post Action Mods enable users to use Mods when interacting with posts, such as Mods for tipping, sharing, traslating or other mini-interactions that require the context of the post such as its author and contents. |
| 6 | + |
| 7 | +Here's an example of where post action Mods will typically be used: |
| 8 | + |
| 9 | +<Image |
| 10 | + src="/post-action.png" |
| 11 | + alt="Post Action Mod example" |
| 12 | + width="400" |
| 13 | + height="200" |
| 14 | +/> |
| 15 | + |
| 16 | +You can integrate Post Action Mods with or without our [Mod Metadata Cache](../metadata-cache.mdx). |
| 17 | + |
| 18 | +## Integration Example |
| 19 | + |
| 20 | +```tsx |
| 21 | +import { ActionMod } from "@mod-protocol/react"; |
| 22 | + |
| 23 | +... |
| 24 | + |
| 25 | + <ActionMod |
| 26 | + input={text} |
| 27 | + embeds={embeds} |
| 28 | + api={API_URL} |
| 29 | + variant="action" |
| 30 | + manifest={currentMod} |
| 31 | + renderers={renderers} |
| 32 | + onOpenFileAction={handleOpenFile} |
| 33 | + onExitAction={hideCurrentMod} |
| 34 | + /> |
| 35 | +``` |
| 36 | + |
| 37 | +## Full Example with Mod Search |
| 38 | + |
| 39 | +```tsx |
| 40 | +import { Embed, ModManifest, handleOpenFile } from "@mod-protocol/core"; |
| 41 | +import { actionMods, actionModsExperimental } from "@mod-protocol/mod-registry"; |
| 42 | +import { ActionMod } from "@mod-protocol/react"; |
| 43 | +import { ModsSearch } from "@mod-protocol/react-ui-shadcn/dist/components/creation-mods-search"; |
| 44 | +import { Button } from "@mod-protocol/react-ui-shadcn/dist/components/ui/button"; |
| 45 | +import { |
| 46 | + Popover, |
| 47 | + PopoverContent, |
| 48 | + PopoverTrigger, |
| 49 | +} from "@mod-protocol/react-ui-shadcn/dist/components/ui/popover"; |
| 50 | +import { renderers } from "@mod-protocol/react-ui-shadcn/dist/renderers"; |
| 51 | +import { KebabHorizontalIcon } from "@primer/octicons-react"; |
| 52 | +import React, { useMemo } from "react"; |
| 53 | +import { getAddress } from "viem"; |
| 54 | +import { useAccount } from "wagmi"; |
| 55 | +import { API_URL } from "./constants"; |
| 56 | +import { useExperimentalMods } from "./use-experimental-mods"; |
| 57 | +import { sendEthTransaction } from "./utils"; |
| 58 | + |
| 59 | +export function Actions({ |
| 60 | + author, |
| 61 | + post, |
| 62 | +}: { |
| 63 | + author: { |
| 64 | + farcaster: { |
| 65 | + fid: string; |
| 66 | + }; |
| 67 | + }; |
| 68 | + post: { |
| 69 | + id: string; |
| 70 | + text: string; |
| 71 | + embeds: Embed[]; |
| 72 | + }; |
| 73 | +}) { |
| 74 | + const experimentalMods = useExperimentalMods(); |
| 75 | + const [currentMod, setCurrentMod] = React.useState<ModManifest | null>(null); |
| 76 | + |
| 77 | + const { address: unchecksummedAddress } = useAccount(); |
| 78 | + const checksummedAddress = React.useMemo(() => { |
| 79 | + if (!unchecksummedAddress) return null; |
| 80 | + return getAddress(unchecksummedAddress); |
| 81 | + }, [unchecksummedAddress]); |
| 82 | + const user = React.useMemo(() => { |
| 83 | + return { |
| 84 | + wallet: { |
| 85 | + address: checksummedAddress, |
| 86 | + }, |
| 87 | + }; |
| 88 | + }, [checksummedAddress]); |
| 89 | + |
| 90 | + const onSendEthTransactionAction = useMemo(() => sendEthTransaction, []); |
| 91 | + |
| 92 | + return ( |
| 93 | + <Popover |
| 94 | + open={!!currentMod} |
| 95 | + onOpenChange={(op: boolean) => { |
| 96 | + if (!op) setCurrentMod(null); |
| 97 | + }} |
| 98 | + > |
| 99 | + <PopoverTrigger></PopoverTrigger> |
| 100 | + <ModsSearch |
| 101 | + mods={experimentalMods ? actionModsExperimental : actionMods} |
| 102 | + onSelect={setCurrentMod} |
| 103 | + > |
| 104 | + <Button variant="ghost" role="combobox" type="button"> |
| 105 | + <KebabHorizontalIcon></KebabHorizontalIcon> |
| 106 | + </Button> |
| 107 | + </ModsSearch> |
| 108 | + <PopoverContent className="w-[400px] ml-2" align="start"> |
| 109 | + <div className="space-y-4"> |
| 110 | + <h4 className="font-medium leading-none">{currentMod?.name}</h4> |
| 111 | + <hr /> |
| 112 | + <ActionMod |
| 113 | + api={API_URL} |
| 114 | + user={user} |
| 115 | + variant="action" |
| 116 | + manifest={currentMod} |
| 117 | + renderers={renderers} |
| 118 | + onOpenFileAction={handleOpenFile} |
| 119 | + onExitAction={() => setCurrentMod(null)} |
| 120 | + onSendEthTransactionAction={onSendEthTransactionAction} |
| 121 | + author={author} |
| 122 | + post={{ |
| 123 | + text: post.text, |
| 124 | + embeds: post.embeds, |
| 125 | + }} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + </PopoverContent> |
| 129 | + </Popover> |
| 130 | + ); |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +This component can then be included in your post to allow it to be invoked by the user: |
| 135 | + |
| 136 | +```tsx |
| 137 | +... |
| 138 | +<div className="ml-auto"> |
| 139 | + <Actions |
| 140 | + post={{ |
| 141 | + id: props.cast.hash, |
| 142 | + text: props.cast.text, |
| 143 | + embeds: props.cast.embeds, |
| 144 | + }} |
| 145 | + author={{ |
| 146 | + farcaster: { |
| 147 | + fid: `${props.cast.fid}`, |
| 148 | + }, |
| 149 | + }} |
| 150 | + /> |
| 151 | +</div> |
| 152 | +... |
| 153 | +``` |
0 commit comments