Skip to content

Commit

Permalink
feat: support display image for notify activity
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoosocool committed Nov 15, 2023
1 parent ee3233d commit 29c546b
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 307 deletions.
252 changes: 47 additions & 205 deletions packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
// showAllActivities,
// showAllMonitoredAddresses,
// showLastUpdated,
testImage,
} from '../utils';
import {
ConnectButton,
Expand All @@ -31,52 +32,15 @@ import {
CardHeader,
CardTitle,
} from '@/components/ui/card';
// import { Button } from '@/components/ui/button';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
// import { Input } from '@/components/ui/input';

// const supportedNSList = [
// '.eth',
// '.lens',
// '.csb',
// '.bnb',
// '.bit',
// '.crypto',
// '.zil',
// '.nft',
// '.x',
// '.wallet',
// '.bitcoin',
// '.dao',
// '.888',
// '.blockchain',
// '.avax',
// '.arb',
// '.cyber',
// ];
// const isValidNS = (handle: string | null) => {
// if (!handle) {
// return false;
// }
// let valid = false;
// supportedNSList.forEach((ns) => {
// if (handle.endsWith(ns)) {
// valid = true;
// }
// });
// return valid;
// };

const Index = () => {
const [state, dispatch] = useContext(MetaMaskContext);

const isMetaMaskReady = isLocalSnap(defaultSnapOrigin)
? state.isFlask
: state.snapsDetected;

// const [walletAddress, setWalletAddress] = useState('');

const handleConnectClick = async () => {
try {
await connectSnap();
Expand All @@ -92,55 +56,6 @@ const Index = () => {
}
};

// const handleSendSetStateClick = async () => {
// if (
// isValidNS(walletAddress) ||
// (walletAddress?.startsWith('0x') && walletAddress.length === 42)
// ) {
// try {
// const originalState = await sendGetState();

// // check address is already added
// const isAlreadyAdded = originalState.socialActivities.find(
// (account) =>
// account.address.toLocaleLowerCase() ===
// walletAddress.toLocaleLowerCase(),
// );

// if (isAlreadyAdded) {
// await showAlert(
// 'Already Monitored',
// `The wallet address: ${walletAddress} is already monitored.`,
// );
// return;
// }

// // add address
// await sendSetState([
// ...originalState.socialActivities,
// {
// address: walletAddress,
// activities: [],
// total: 0,
// },
// ]);

// await showAlert(
// 'Succeeded',
// `The wallet address: ${walletAddress} is being monitored.`,
// );
// } catch (e) {
// console.error(e);
// dispatch({ type: MetamaskActions.SetError, payload: e });
// }
// } else {
// await showAlert(
// 'invalid wallet address',
// `please check your input. supported ns: ${supportedNSList.join(', ')}`,
// );
// }
// };

const handleSendClearStateClick = async () => {
try {
const resp = await sendClearState();
Expand All @@ -156,32 +71,14 @@ const Index = () => {
}
};

// const handleShowLastUpdatedClick = async () => {
// try {
// await showLastUpdated();
// } catch (e) {
// console.error(e);
// dispatch({ type: MetamaskActions.SetError, payload: e });
// }
// };

// const handleShowAllActivitiesClick = async () => {
// try {
// await showAllActivities();
// } catch (e) {
// console.error(e);
// dispatch({ type: MetamaskActions.SetError, payload: e });
// }
// };

// const handleShowAllAddressesClick = async () => {
// try {
// await showAllMonitoredAddresses();
// } catch (e) {
// console.error(e);
// dispatch({ type: MetamaskActions.SetError, payload: e });
// }
// };
const handleGetTestImageClick = async () => {
try {
await testImage();
} catch (e) {
console.error(e);
dispatch({ type: MetamaskActions.SetError, payload: e });
}
};

return (
<div className="container my-12">
Expand Down Expand Up @@ -317,100 +214,45 @@ const Index = () => {
</Card>

{!isProduction && (
<Card>
<CardHeader>
<CardTitle>Stalk Someone</CardTitle>
<CardDescription className="h-[120px]">
You can get notified if someone else's frens publish something
(actually, this is for debugging purpose).
</CardDescription>
</CardHeader>
<CardFooter>
<Button
disabled={!state.installedSnap}
onClick={() => {
navigate('/monitor/create');
}}
>
Begin Stalking
</Button>
</CardFooter>
</Card>
)}
<>
<Card>
<CardHeader>
<CardTitle>Stalk Someone</CardTitle>
<CardDescription className="h-[120px]">
You can get notified if someone else's frens publish something
(actually, this is for debugging purpose).
</CardDescription>
</CardHeader>
<CardFooter>
<Button
disabled={!state.installedSnap}
onClick={() => {
navigate('/monitor/create');
}}
>
Begin Stalking
</Button>
</CardFooter>
</Card>

{/* <Card>
<CardHeader>
<CardTitle>Show Last Updated</CardTitle>
<CardDescription>
Show the activities included in the last updated
</CardDescription>
</CardHeader>
<CardFooter>
<SendHelloButton
onClick={handleShowLastUpdatedClick}
disabled={!state.installedSnap}
/>
</CardFooter>
</Card>
<Card>
<CardHeader>
<CardTitle>Show All Activities</CardTitle>
<CardDescription>
View all activities from all the addresses monitored.
</CardDescription>
</CardHeader>
<CardFooter>
<SendHelloButton
onClick={handleShowAllActivitiesClick}
disabled={!state.installedSnap}
/>
</CardFooter>
</Card>
<Card>
<CardHeader>
<CardTitle>Show All Addresses</CardTitle>
<CardDescription>
View all addresses from all the addresses monitored.
</CardDescription>
</CardHeader>
<CardFooter>
<SendHelloButton
onClick={handleShowAllAddressesClick}
disabled={!state.installedSnap}
/>
</CardFooter>
</Card>
<Card>
<CardHeader>
<CardTitle>Monitor Any Address</CardTitle>
<CardDescription>
<p>Start monitoring a new address.</p>
<p>Supported Web3 Name Service: {supportedNSList.join(', ')}.</p>
<p>
And of course, your favorite and easy-to-memorize 0x address.
</p>
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex flex-row items-center gap-4">
<Input
type="text"
placeholder="someone.eth, or 0x..."
onChange={(e) => setWalletAddress(e.target.value)}
/>
<Button
onClick={handleSendSetStateClick}
disabled={!state.installedSnap}
>
Add
</Button>
</div>
</CardContent>
</Card> */}
<Card>
<CardHeader>
<CardTitle>Test Snap Dialog </CardTitle>
<CardDescription className="h-[120px]">
For debugging.
</CardDescription>
</CardHeader>
<CardFooter>
<Button
disabled={!state.installedSnap}
onClick={handleGetTestImageClick}
>
show
</Button>
</CardFooter>
</Card>
</>
)}
</div>
</div>
);
Expand Down
12 changes: 12 additions & 0 deletions packages/site/src/utils/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,16 @@ export const getProfilesToMonitorFollowing = async () => {
},
})) as SocialMonitor[];
};

export const testImage = async () => {
return await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
request: {
method: 'test-image',
},
},
});
};
export const isLocalSnap = (snapId: string) => snapId.startsWith('local:');
3 changes: 2 additions & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
},
"dependencies": {
"@metamask/snaps-types": "^2.0.0",
"@metamask/snaps-ui": "^2.0.0",
"@metamask/snaps-ui": "^3.1.0",
"@metamask/utils": "^8.1.0",
"@rss3/js-sdk": "^0.6.38",
"@urql/core": "^4.1.4",
"axios": "^1.6.1",
"buffer": "^6.0.3",
"cross-fetch": "^4.0.0",
"ethers": "^6.7.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/NaturalSelectionLabs/RSS3-Social-Notifier-Snap.git"
},
"source": {
"shasum": "NGnMg2JkJ9Q8j9XIftSL923A5KfqhlfPyIr/yEAFfjs=",
"shasum": "yJz16C7h+sLDWuj+Ly8nT2vOklyDNBpAD3e/lOaWgGQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
28 changes: 17 additions & 11 deletions packages/snap/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type Activity,
formatAddressAndNS,
format as sdkFormat,
formatContent,
type Theme,
} from '@rss3/js-sdk';

Expand All @@ -22,10 +23,13 @@ export async function getSocialActivities(address: string) {
const resp = await fetch(getSocialActivitiesUrl(address));
const { data } = (await resp.json()) as { data: Activity[] };
const activities = data.map((item: Activity) => {
return {
id: item.id,
text: format(item).join(''),
};
// formatContent only for social and governance tag.
const content = formatContent(item);

const { id } = item;
const text = format(item).join('');
const image = content?.media?.[0]?.address;
return content ? { id, text, image } : { id, text };
});

return {
Expand Down Expand Up @@ -127,13 +131,15 @@ export async function getMultiple(addresses: string[]) {
meta: null | { cursor: string };
};

data.map((item) =>
activities.push({
id: item.id,
text: format(item).join(''),
owner: item.owner,
}),
);
data.map((item) => {
const content = formatContent(item);
const { id, owner } = item;
const text = format(item).join('');
const image = content?.media?.[0]?.address;
return content
? activities.push({ id, text, image, owner })
: activities.push({ id, text, owner });
});

if (meta === null) {
hasNextPage = false;
Expand Down
Loading

0 comments on commit 29c546b

Please sign in to comment.