Skip to content

Commit

Permalink
Merge pull request #157 from us3r-network/F-fcastProfileUrl-shixuewen
Browse files Browse the repository at this point in the history
F fcast profile url shixuewen
  • Loading branch information
sin-bufan authored Oct 13, 2023
2 parents 1406651 + a290854 commit 2772411
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 72 deletions.
40 changes: 11 additions & 29 deletions apps/u3/src/components/profile/ProfileSocial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useActiveProfile } from '@lens-protocol/react-web';
import InfiniteScroll from 'react-infinite-scroll-component';
import { FeedsType } from './ProfilePageNav';
import { useLoadProfileFeeds } from '../../hooks/useLoadProfileFeeds';
import useFarcasterCurrFid from '../../hooks/farcaster/useFarcasterCurrFid';
import Loading from '../common/loading/Loading';
import LensPostCard from '../social/lens/LensPostCard';
import FCast from '../social/farcaster/FCast';
Expand All @@ -15,21 +14,18 @@ import Rss3Content from '../fren/Rss3Content';
import { NoActivity } from '../../container/Activity';

export default function ProfileSocial({
wallet,
lensProfileId,
fid,
feedsType,
}: {
wallet: string;
lensProfileId: string;
fid: string;
feedsType: FeedsType;
}) {
const {
openFarcasterQR,
farcasterUserData,
isConnected: isConnectedFarcaster,
} = useFarcasterCtx();
const { openFarcasterQR, farcasterUserData } = useFarcasterCtx();
const { data: activeLensProfile, loading: activeLensProfileLoading } =
useActiveProfile();

const fid = useFarcasterCurrFid();
const {
firstLoading,
moreLoading,
Expand All @@ -43,35 +39,21 @@ export default function ProfileSocial({
if (feedsType === FeedsType.ACTIVITIES) return;
loadFirstFeeds({
activeLensProfileId: activeLensProfile?.id,
lensProfileId: activeLensProfile?.id,
fid: isConnectedFarcaster ? fid : undefined,
lensProfileId,
fid,
group: feedsType as unknown as ProfileFeedsGroups,
});
}, [
loadFirstFeeds,
activeLensProfile?.id,
fid,
feedsType,
wallet,
isConnectedFarcaster,
]);
}, [loadFirstFeeds, activeLensProfile?.id, fid, feedsType, lensProfileId]);

const loadMoreSocialFeeds = useCallback(() => {
if (feedsType === FeedsType.ACTIVITIES) return;
loadMoreFeeds({
activeLensProfileId: activeLensProfile?.id,
lensProfileId: activeLensProfile?.id,
fid: isConnectedFarcaster ? fid : undefined,
lensProfileId,
fid,
group: feedsType as unknown as ProfileFeedsGroups,
});
}, [
loadMoreFeeds,
activeLensProfile?.id,
fid,
feedsType,
wallet,
isConnectedFarcaster,
]);
}, [loadMoreFeeds, activeLensProfile?.id, fid, feedsType, lensProfileId]);

useEffect(() => {
if (activeLensProfileLoading) return;
Expand Down
44 changes: 26 additions & 18 deletions apps/u3/src/components/profile/profile-info/ProfileInfoCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UserInfo, UserInfoEditForm } from '@us3r-network/profile';
import styled, { StyledComponentPropsWithRef } from 'styled-components';
import { Dialog, Heading, Modal } from 'react-aria-components';
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { toast } from 'react-toastify';
import {
Profile,
Expand Down Expand Up @@ -32,6 +32,7 @@ import {
import useCanMessage from '../../../hooks/xmtp/useCanMessage';
import { useFarcasterCtx } from '../../../contexts/FarcasterCtx';
import useFarcasterUserData from '../../../hooks/farcaster/useFarcasterUserData';
import useUpsertFarcasterUserData from '../../../hooks/farcaster/useUpsertFarcasterUserData';
import { useNav } from '../../../contexts/NavCtx';
import useFarcasterFollowNum from '../../../hooks/farcaster/useFarcasterFollowNum';
import useBioLinkListWithDid from '../../../hooks/useBioLinkListWithDid';
Expand All @@ -51,15 +52,14 @@ export default function ProfileInfoCard({
const { bioLinkList } = useBioLinkListWithDid(did);

const [isOpenEdit, setIsOpenEdit] = useState(false);
const { currFid, farcasterUserData } = useFarcasterCtx();
const { farcasterFollowData } = useFarcasterFollowNum();
const { farcasterUserData } = useFarcasterCtx();

const address = getAddressWithDidPkh(did);
const canMesssage = useCanMessage(address);

const lensBioLinkProfiles = bioLinkList
.filter((link) => link.platform === BIOLINK_PLATFORMS.lens)
.map((item) => {
?.filter((link) => link.platform === BIOLINK_PLATFORMS.lens)
?.map((item) => {
try {
return JSON.parse(item.data) as Profile;
} catch (error) {
Expand All @@ -72,20 +72,28 @@ export default function ProfileInfoCard({
});
const lensProfileFirst = lensProfiles?.[0];

// TODO 用bioLinkList 里的data字段中的 fid 获取 userData
// const fcastBioLinkProfiles = bioLinkList
// .filter((link) => link.platform === BIOLINK_PLATFORMS.farcaster)
// .map((item) => {
// try {
// return JSON.parse(item.data) as { fid: string };
// } catch (error) {
// return null;
// }
// })
// .filter((item) => !!item);
// const fid = fcastBioLinkProfiles[0]?.fid;
const fcastBioLinkProfiles = bioLinkList
?.filter((link) => link.platform === BIOLINK_PLATFORMS.farcaster)
?.map((item) => {
try {
return JSON.parse(item.data) as { fid: string };
} catch (error) {
return null;
}
})
.filter((item) => !!item);
const fid = fcastBioLinkProfiles[0]?.fid;
const { upsertFarcasterUserData } = useUpsertFarcasterUserData();
useEffect(() => {
if (fid && !farcasterUserData[fid]) {
upsertFarcasterUserData({ fid });
}
}, [fid, farcasterUserData]);

const { farcasterFollowData } = useFarcasterFollowNum(fid);

const userData = useFarcasterUserData({
fid: `${currFid}`,
fid: `${fid}`,
farcasterUserData,
});

Expand Down
37 changes: 19 additions & 18 deletions apps/u3/src/container/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export default function Profile() {

const { bioLinkList } = useBioLinkListWithDid(did);
const lensBioLinkProfiles = bioLinkList
.filter((link) => link.platform === BIOLINK_PLATFORMS.lens)
.map((item) => {
?.filter((link) => link.platform === BIOLINK_PLATFORMS.lens)
?.map((item) => {
try {
return JSON.parse(item.data) as LensProfile;
} catch (error) {
Expand All @@ -101,19 +101,19 @@ export default function Profile() {
});
const lensProfileFirst = lensProfiles?.[0];

// TODO 用bioLinkList 里的data字段中的 fid 获取 farcasterFollowData
// const fcastBioLinkProfiles = bioLinkList
// .filter((link) => link.platform === BIOLINK_PLATFORMS.farcaster)
// .map((item) => {
// try {
// return JSON.parse(item.data) as { fid: string };
// } catch (error) {
// return null;
// }
// })
// .filter((item) => !!item);
// const fid = fcastBioLinkProfiles[0]?.fid;
const { farcasterFollowData } = useFarcasterFollowNum();
const fcastBioLinkProfiles = bioLinkList
?.filter((link) => link.platform === BIOLINK_PLATFORMS.farcaster)
?.map((item) => {
try {
return JSON.parse(item.data) as { fid: string };
} catch (error) {
return null;
}
})
.filter((item) => !!item);
const fid = fcastBioLinkProfiles[0]?.fid;

const { farcasterFollowData } = useFarcasterFollowNum(fid);

useEffect(() => {
setFollowNavData((prevData) => ({
Expand Down Expand Up @@ -290,12 +290,13 @@ export default function Profile() {
return (
<KeepAlive
cacheKey={`${RouteKey.profile}_social_${
address || '0x'
}_${profilePageFeedsType}`}
lensProfileFirst?.id || '0'
}_${fid || '0'}_${profilePageFeedsType}`}
saveScrollPosition="#profile-wrapper"
>
<ProfileSocial
wallet={address}
lensProfileId={lensProfileFirst?.id}
fid={fid}
feedsType={profilePageFeedsType}
/>
</KeepAlive>
Expand Down
33 changes: 32 additions & 1 deletion apps/u3/src/contexts/FarcasterCtx.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable @typescript-eslint/no-shadow */
import { NobleEd25519Signer } from '@farcaster/hub-web';
import { NobleEd25519Signer, UserDataType } from '@farcaster/hub-web';
import { createPublicClient, http } from 'viem';
import {
ReactNode,
Expand All @@ -16,6 +16,8 @@ import axios from 'axios';
import { goerli } from 'viem/chains';
import { toast } from 'react-toastify';

import { useSession } from '@us3r-network/auth-with-rainbowkit';
import { useProfileState } from '@us3r-network/profile';
import { WARPCAST_API } from '../constants/farcaster';
import {
generateKeyPair,
Expand All @@ -32,6 +34,12 @@ import {
} from '../api/farcaster';
import FarcasterQRModal from '../components/social/farcaster/FarcasterQRModal';
import FarcasterIframeModal from '../components/social/farcaster/FarcasterIframeModal';
import useBioLinkActions from '../hooks/useBioLinkActions';
import {
BIOLINK_FARCASTER_NETWORK,
BIOLINK_PLATFORMS,
farcasterHandleToBioLinkHandle,
} from '../utils/profile/biolink';
import FarcasterChannelData from '../constants/warpcast.json';

export const publicClient = createPublicClient({
Expand Down Expand Up @@ -295,6 +303,29 @@ export default function FarcasterProvider({
}
}, []);

// 每次farcaster登录成功后,更新farcaster biolink
const session = useSession();
const { profile } = useProfileState();
const { upsertBioLink } = useBioLinkActions();
useEffect(() => {
const findUserInfo = currUserInfo?.[currFid]?.find(
(item) => item.type === UserDataType.USERNAME
);
const handle = findUserInfo?.value || '';
if (!!session?.id && !!profile?.id && signer?.isConnected && !!handle) {
upsertBioLink({
did: session.id,
bioLink: {
profileID: profile.id,
platform: BIOLINK_PLATFORMS.farcaster,
network: String(BIOLINK_FARCASTER_NETWORK),
handle: farcasterHandleToBioLinkHandle(handle),
data: JSON.stringify({ fid: currFid, ...findUserInfo }),
},
});
}
}, [session, profile, signer, currFid, currUserInfo]);

return (
<FarcasterContext.Provider
// TODO: fix this
Expand Down
10 changes: 4 additions & 6 deletions apps/u3/src/hooks/farcaster/useFarcasterFollowNum.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { useCallback, useEffect, useState } from 'react';
import { getFarcasterFollow } from 'src/api/farcaster';
import { useFarcasterCtx } from 'src/contexts/FarcasterCtx';

export default function useFarcasterFollowNum() {
const { currFid } = useFarcasterCtx();
export default function useFarcasterFollowNum(fid: string) {
const [farcasterFollowData, setFarcasterFollowData] = useState({
following: 0,
followers: 0,
});

const getFarcasterFollowData = useCallback(async () => {
if (!currFid) return;
const resp = await getFarcasterFollow(currFid);
if (!fid) return;
const resp = await getFarcasterFollow(fid);
const followData = resp.data.data;
setFarcasterFollowData({
...followData,
});
}, [currFid]);
}, [fid]);

useEffect(() => {
getFarcasterFollowData().catch(console.error);
Expand Down
22 changes: 22 additions & 0 deletions apps/u3/src/hooks/farcaster/useUpsertFarcasterUserData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useCallback } from 'react';
import { useFarcasterCtx } from '../../contexts/FarcasterCtx';
import { getFarcasterUserInfo } from '../../api/farcaster';

export default function useUpsertFarcasterUserData() {
const { setFarcasterUserData } = useFarcasterCtx();
const upsertFarcasterUserData = useCallback(
async ({ fid }: { fid: string | number }) => {
const resp = await getFarcasterUserInfo([Number(fid)]);
if (resp.data.code === 0) {
const temp: {
[key: string]: { type: number; value: string }[];
} = {};
temp[fid] = resp.data.data;
setFarcasterUserData((pre) => ({ ...pre, ...temp }));
}
},
[]
);

return { upsertFarcasterUserData };
}

0 comments on commit 2772411

Please sign in to comment.