Skip to content

Commit

Permalink
reconstruct SyncingBotSaves component
Browse files Browse the repository at this point in the history
  • Loading branch information
sin-bufan committed Jan 23, 2024
1 parent 7d7564a commit 7615317
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
55 changes: 24 additions & 31 deletions apps/u3/src/components/save/SyncingBotSaves.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useFavorAction } from '@us3r-network/link';
import { useSession } from '@us3r-network/auth-with-rainbowkit';
import { useCallback, useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import { Link } from '@us3r-network/data-model';
import {
getSavedCasts,
Expand All @@ -11,53 +11,46 @@ import { getAddressWithDidPkh } from '../../utils/shared/did';
export default function SyncingBotSaves({
onComplete,
}: {
onComplete?: () => void;
onComplete?: (saves) => void;
}) {
const session = useSession();
const [saves, setSaves] = useState([]);
const [link, setLink] = useState(null);
const [saveIndex, setSaveIndex] = useState(-1);

useEffect(() => {
const walletAddress = getAddressWithDidPkh(session.id);
getSavedCasts(walletAddress).then((res) => {
setSaves(res);
console.log('getSavedCasts', res);
const links = res.map((item) => {
const { castHash, text } = item;
return {
url: `https://u3.xyz/social/post-detail/fcast/${Buffer.from(
castHash.data
).toString('hex')}`,
title: text || 'Saved Farcaster Cast using U3 Bot',
type: 'link',
data: JSON.stringify(item),
};
});
setSaves(links);
setSaveIndex(0);
});
}, [session]);

useEffect(() => {
if (!saves || saves.length === 0) return;
syncNext();
}, [saves.length]);

const syncNext = useCallback(() => {
if (!saves || saves.length === 0) return;
const save = saves[0];
const nextLink = {
url: `https://u3.xyz/social/post-detail/fcast/${Buffer.from(
save.castHash.data
).toString('hex')}`,
title: save.text || 'Saved Farcaster Cast using U3 Bot',
type: 'link',
data: JSON.stringify(save),
};
console.log('cast', nextLink);
setLink(nextLink);
}, [saves]);

return link ? (
return saves && saves.length > 0 && saveIndex >= 0 ? (
<FavoringLink
link={link}
link={saves[saveIndex]}
onSuccessfullyFavor={(isFavored: boolean, linkId: string) => {
console.log('onSuccessfullyFavor', isFavored, linkId);
if (isFavored) {
const linkData = JSON.parse(link.data);
const linkData = JSON.parse(saves[saveIndex].data);
const { id, walletAddress } = linkData;
console.log(id, walletAddress);
setSavedCastsSynced(Buffer.from(walletAddress).toString('hex'), id);
const newSaves = saves.slice(1);
setSaves(newSaves);
if (newSaves.length === 0) onComplete();
if (saveIndex >= saves.length - 1) {
onComplete(saves);
} else {
setSaveIndex(saveIndex + 1);
}
}
}}
onFailedFavor={(errMsg: string) => {
Expand Down
6 changes: 4 additions & 2 deletions apps/u3/src/container/Save.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react';
import { useMemo, useState } from 'react';
import styled from 'styled-components';
import { usePersonalFavors } from '@us3r-network/link';
import { isMobile } from 'react-device-detect';
Expand Down Expand Up @@ -52,6 +52,7 @@ const EmptyDesc = styled.span`

export default function Save() {
const { isFetching, personalFavors } = usePersonalFavors();
const [syncNum, setSyncNum] = useState(0);
// console.log('personalFavors', personalFavors);
const list = useMemo(
() =>
Expand Down Expand Up @@ -99,8 +100,9 @@ export default function Save() {
<Wrapper>
{isMobile ? null : <PageTitle>Saves</PageTitle>}
<SyncingBotSaves
onComplete={() => {
onComplete={(saves) => {
console.log('onComplete SyncingBotSaves');
setSyncNum(saves.length);
}}
/>
<ContentWrapper>
Expand Down

0 comments on commit 7615317

Please sign in to comment.