Skip to content

Commit

Permalink
chore: fix dapp
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Jul 8, 2024
1 parent c002f3d commit b02f12a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
30 changes: 8 additions & 22 deletions packages/examples/dnode-dapp/src/pages/pushscan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,13 @@ import SearchBar from '../../components/SearchBar';
import { useRouter } from 'next/router';
import Link from 'next/link';
import { getRecentTransactionAccounts } from '../../utils/push';
import { getCheckSumAddress } from '../../utils';
import { formatDate, getCheckSumAddress } from '../../utils';

export default function Explorer() {
const [page, setPage] = useState(1);
const [latestNotifications, setLatestNotifications] = useState<
{ address: string }[]
>([
// generate 10 dummy data
{ address: '0x5ac9E6205eACA2bBbA6eF716FD9AabD76326EEee' },
{ address: '0x1234567890123456789012345678901234567891' },
{ address: '0x1234567890123456789012345678901234567892' },
{ address: '0x1234567890123456789012345678901234567893' },
{ address: '0x1234567890123456789012345678901234567894' },
{ address: '0x1234567890123456789012345678901234567895' },
{ address: '0x1234567890123456789012345678901234567896' },
{ address: '0x1234567890123456789012345678901234567897' },
{ address: '0x1234567890123456789012345678901234567898' },
{ address: '0x1234567890123456789012345678901234567899' },
]);
{ nsId: string; last_usage: string }[]
>([]);
const [total, setTotal] = useState(20);
const size = 10; // Hardcoded in api

Expand All @@ -41,8 +29,7 @@ export default function Explorer() {
// Fetch initial stats and latest blocks
const fetchData = async () => {
const recipients = await getRecentTransactionAccounts();
console.log(recipients);
// setLatestNotifications(recipients);
setLatestNotifications(recipients);
};

// Polling function
Expand All @@ -68,24 +55,23 @@ export default function Explorer() {

<ul role="list" className="divide-y divide-gray-100">
{latestNotifications.map((notification, index) => (
<Link href={`/pushscan/${notification.address}`} key={index}>
<Link href={`/pushscan/${notification.nsId}`} key={index}>
<li className="flex justify-between gap-x-6 py-5 px-4 border border-gray-200 rounded-lg hover:scale-105 transition-transform duration-150 hover:cursor-pointer">
<div className="flex min-w-0 gap-x-4">
<img
className="h-12 w-12 flex-none rounded-md bg-gray-50"
src="/user.png"
alt={`${notification.address} img`}
alt={`${notification.nsId} img`}
/>
<div className="min-w-0 flex-auto">
<p className="text-sm font-semibold leading-6 text-gray-900 break-words overflow-x-auto">
{notification.address}
{notification.nsId}
</p>
</div>
</div>
<div className="hidden shrink-0 sm:flex sm:flex-col sm:items-end">
<p className="text-sm leading-6 text-gray-900">
{/* {protocol.category} */}
Maybe A TimeStamp
{formatDate(notification.last_usage)}
</p>
</div>
</li>
Expand Down
24 changes: 24 additions & 0 deletions packages/examples/dnode-dapp/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// TODO: Implement All the Fns

import { getAddress } from 'viem';
import { Block, NetworkStats, Transaction } from '../types';

export const fetchNetworkStats = async (): Promise<NetworkStats> => {
Expand Down Expand Up @@ -44,3 +45,26 @@ export const fetchTransaction = async (
value: '1000',
};
};

export const getCheckSumAddress = (address: string): string => {
const addr = address.toLowerCase();
try {
return getAddress(addr);
} catch (e) {
return addr;
}
};

export const formatDate = (dateString: string): string => {
const date = new Date(dateString);
if (isNaN(date.getTime())) {
return 'Invalid date';
}
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
};
19 changes: 17 additions & 2 deletions packages/examples/dnode-dapp/src/utils/push.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PushAPI } from '@pushprotocol/dnode';
import { ENV } from '@pushprotocol/dnode/src/lib/constants';
import axios from 'axios';
import { getCheckSumAddress } from '.';

// TODO: Change to dev or stage
const env = ENV.DEV;

export const sendNotification = async (
Expand All @@ -11,7 +12,8 @@ export const sendNotification = async (
signer: any
) => {
const userAlice = await PushAPI.initialize(signer, { env });
return await userAlice.channel.send(recipient, {
const parsedRecipients = recipient.map((r) => getCheckSumAddress(r));
return await userAlice.channel.send(parsedRecipients, {
notification: {
title,
body,
Expand All @@ -28,3 +30,16 @@ export const getAddressTrx = async (address: string) => {
const userAlice = await PushAPI.initialize(null, { env, account: address });
return await userAlice.notification.list('INBOX');
};

export const getRecentTransactionAccounts = async () => {
// Example with no additional data or headers
return await axios
.post('https://s1.dev.push.org/api/v1/kv/ns/all')
.then((response) => {
return response.data;
})
.catch((error) => {
console.error('Error:', error);
return [];
});
};

0 comments on commit b02f12a

Please sign in to comment.