Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: profile farcaster #117

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions apps/u3/src/api/farcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,18 @@ export function getFarcasterEmbedMetadata(urls: string[]): AxiosPromise<
},
});
}

export function getFarcasterFollow(fid: string | number): AxiosPromise<
ApiResp<{
followers: number;
following: number;
}>
> {
return axios({
url: `${REACT_APP_API_SOCIAL_URL}/3r/farcaster/follow`,
method: 'get',
params: {
fid,
},
});
}
82 changes: 58 additions & 24 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 All @@ -10,6 +10,9 @@ import {
useProfilesOwnedBy,
} from '@lens-protocol/react-web';
import { useSession } from '@us3r-network/auth-with-rainbowkit';

import { getFarcasterFollow } from 'src/api/farcaster';

import { ButtonPrimaryLineCss } from '../../common/button/ButtonBase';
import { InputBaseCss } from '../../common/input/InputBase';
import { TextareaBaseCss } from '../../common/input/TextareaBase';
Expand All @@ -28,6 +31,8 @@ import {
useXmtpStore,
} from '../../../contexts/xmtp/XmtpStoreCtx';
import useCanMessage from '../../../hooks/xmtp/useCanMessage';
import { useFarcasterCtx } from '../../../contexts/FarcasterCtx';
import useFarcasterUserData from '../../../hooks/farcaster/useFarcasterUserData';

interface ProfileInfoCardProps extends StyledComponentPropsWithRef<'div'> {
address: string;
Expand All @@ -38,7 +43,11 @@ export default function ProfileInfoCard({
}: ProfileInfoCardProps) {
const session = useSession();
const [isOpenEdit, setIsOpenEdit] = useState(false);

const { currFid, farcasterUserData } = useFarcasterCtx();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的fid 应该是通过address参数拿到的fid, 不是当前登录的fid,这个组件不仅是当前用户的ProfileInfoCard, 还用来渲染其它用户的ProfileInfoCard

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

支持 profile/:address 的时候再做一个 adapter 就可以

const [farcasterFollowData, setFarcasterFollowData] = useState({
followers: 0,
following: 0,
});
const did = useMemo(() => getDidPkhWithAddress(address), [address]);

const isLoginUser = useMemo(() => session?.id === did, [session, did]);
Expand All @@ -47,33 +56,58 @@ export default function ProfileInfoCard({
address,
});

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

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

useEffect(() => {
getFarcasterFollowData().catch(console.error);
}, [getFarcasterFollowData]);

const platformAccounts: PlatformAccountsData = useMemo(() => {
const lensAccounts = lensProfiles?.map((lensProfile) => ({
platform: SocailPlatform.Lens,
avatar: getAvatar(lensProfile),
name: lensProfile.name,
handle: lensProfile.handle,
}));
// TODO 加上farcaster平台的account
return lensAccounts || [];
}, [lensProfiles]);
const lensAccounts =
lensProfiles?.map((lensProfile) => ({
platform: SocailPlatform.Lens,
avatar: getAvatar(lensProfile),
name: lensProfile.name,
handle: lensProfile.handle,
})) || [];

if (userData) {
return [
...lensAccounts,
{
platform: SocailPlatform.Farcaster,
avatar: userData.pfp,
name: userData.userName,
handle: userData.display,
},
];
}
return lensAccounts;
}, [lensProfiles, userData]);

const followersCount = useMemo(() => {
const lensFollowersCount = lensProfiles?.reduce(
(acc, cur) => acc + cur.stats.totalFollowers,
0
);
// TODO 加上farcaster平台的followers数量
return lensFollowersCount || 0;
}, [lensProfiles]);
const lensFollowersCount =
lensProfiles?.reduce((acc, cur) => acc + cur.stats.totalFollowers, 0) ||
0;

return lensFollowersCount + farcasterFollowData.followers;
}, [lensProfiles, farcasterFollowData]);

const followingCount = useMemo(() => {
const lensFollowersCount = lensProfiles?.reduce(
(acc, cur) => acc + cur.stats.totalFollowing,
0
);
// TODO 加上farcaster平台的following数量
return lensFollowersCount || 0;
const lensFollowersCount =
lensProfiles?.reduce((acc, cur) => acc + cur.stats.totalFollowing, 0) ||
0;

return lensFollowersCount + farcasterFollowData.following;
}, [lensProfiles]);

// TODO lens 一个address可能有多个profile,需要每个profile都follow吗?
Expand Down