Skip to content

Commit

Permalink
Merge pull request #246 from us3r-network/B-lensV2LoginAndCreatePost-…
Browse files Browse the repository at this point in the history
…shixuewen

fix: lens v2 login & create post
  • Loading branch information
friendlysxw authored Nov 10, 2023
2 parents 84af4a0 + 9b1d27d commit af5929e
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function LensFollowProfileCard({
() => ({
handle: getHandle(profile),
address: getOwnedByAddress(profile),
name: getName(profile),
name: getName(profile) || profile?.id,
avatar: getAvatar(profile),
bio: getBio(profile),
isFollowed:
Expand Down
5 changes: 1 addition & 4 deletions apps/u3/src/components/social/SocialWhoToFollow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ export default function SocialWhoToFollow() {
const lensRecommendedProfiles: Profile[] = useMemo(
() =>
lensProfiles
?.filter(
(profile) =>
profile.metadata.displayName && profile.metadata.displayName !== ''
)
?.filter((profile) => getName(profile) && getName(profile) !== '')
.slice(0, SUGGEST_NUM),
[lensProfiles, isLoginLens]
);
Expand Down
8 changes: 6 additions & 2 deletions apps/u3/src/components/social/lens/LensPostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ export default function LensPostCard({
isPending: isPendingReactionUpvote,
} = useReactionLensUpvote({
publication,
onReactionSuccess: ({ originPublication }) => {
onReactionSuccess: ({ originPublication, hasUpvoted: upvoted }) => {
if (originPublication?.id !== updatedPublication.id) return;
setUpdatedPublication((prev) => {
if (!prev) return prev;
const { stats } = prev;
const { stats, operations } = prev;
return {
...prev,
operations: {
...operations,
hasUpvoted: upvoted,
},
stats: {
...stats,
upvotes: Number(stats.upvotes) + 1,
Expand Down
1 change: 1 addition & 0 deletions apps/u3/src/components/social/lens/v1/LensPostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function LensPostCardV1({
navigate(`/social/post-detail/lens/${data.id}`);
}}
data={cardData}
likeDisabled
replyDisabled={replyDisabled}
repostDisabled={repostDisabled}
shareLink={getSocialDetailShareUrlWithLens(data.id)}
Expand Down
5 changes: 3 additions & 2 deletions apps/u3/src/container/social/Social.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ import {
PostList,
} from './CommonStyles';
import { useLensCtx } from '../../contexts/social/AppLensCtx';
import { getOwnedByAddress } from '../../utils/social/lens/profile';

export default function SocialAll() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [parentId, setParentId] = useState('social-all');
const { isLogin } = useLogin();
const { sessionProfile: lensSessionProfile } = useLensCtx();
const fid = useFarcasterCurrFid();
const { ownedBy: lensProfileOwnedByAddress, id: lensSessionProfileId } =
lensSessionProfile || {};
const { id: lensSessionProfileId } = lensSessionProfile || {};
const lensProfileOwnedByAddress = getOwnedByAddress(lensSessionProfile);

const {
socialPlatform,
Expand Down
4 changes: 2 additions & 2 deletions apps/u3/src/container/social/SocialLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import SearchInput from '../../components/common/input/SearchInput';
import { useFarcasterCtx } from '../../contexts/social/FarcasterCtx';
import TrendChannel from '../../components/social/farcaster/TrendChannel';
import { useLensCtx } from '../../contexts/social/AppLensCtx';
import { getOwnedByAddress } from '../../utils/social/lens/profile';

export default function SocialLayout() {
const location = useLocation();
const { isConnected: isConnectedFarcaster } = useFarcasterCtx();
const { sessionProfile: lensSessionProfile } = useLensCtx();
const { loading: lensSessionLoading } = useSession();
const { ownedBy: lensProfileOwnedBy } = lensSessionProfile || {};
const { address: lensProfileOwnedByAddress } = lensProfileOwnedBy || {};
const lensProfileOwnedByAddress = getOwnedByAddress(lensSessionProfile);

const [postScroll, setPostScroll] = useState({
currentParent: '',
Expand Down
5 changes: 3 additions & 2 deletions apps/u3/src/container/social/SocialLens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
PostList,
} from './CommonStyles';
import { useLensCtx } from '../../contexts/social/AppLensCtx';
import { getOwnedByAddress } from '../../utils/social/lens/profile';

export default function SocialFarcaster() {
const [parentId] = useState('social-lens');
Expand Down Expand Up @@ -52,8 +53,8 @@ export default function SocialFarcaster() {
const { feeds, firstLoading, pageInfo, moreLoading } = useListFeeds(parentId);

const { sessionProfile } = useLensCtx();
const { ownedBy: lensProfileOwnedByAddress, id: lensSessionProfileId } =
sessionProfile || {};
const { id: lensSessionProfileId } = sessionProfile || {};
const lensProfileOwnedByAddress = getOwnedByAddress(sessionProfile);

const loadFirstFeeds = useCallback(async () => {
setFirstLoadingDone(false);
Expand Down
38 changes: 37 additions & 1 deletion apps/u3/src/contexts/social/AppLensCtx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
useUpdateProfileManagers,
Profile,
PrimaryPublication,
useProfiles,
ProfileId,
} from '@lens-protocol/react-web';
import { bindings as wagmiBindings } from '@lens-protocol/wagmi';
import { Connector, useAccount } from 'wagmi';
Expand Down Expand Up @@ -68,6 +70,20 @@ const lensConfig: LensConfig = {
environment: LENS_ENV === 'production' ? production : development,
};

// TODO : 兼容v2登录,为了在登录前获取钱包对应的profileID

let walletProfileId = '';
const getWalletProfileId = (): Promise<ProfileId> => {
return new Promise((resolve) => {
const intervalId = setInterval(() => {
if (walletProfileId) {
clearInterval(intervalId);
resolve(walletProfileId as ProfileId);
}
}, 100);
});
};

export function AppLensProvider({ children }: PropsWithChildren) {
return (
<LensProvider config={lensConfig}>
Expand Down Expand Up @@ -113,6 +129,22 @@ export function LensAuthProvider({ children }: PropsWithChildren) {
});
}, [sessionProfile, updateProfileManagers]);

// TODO : 兼容v2登录,为了在登录前获取钱包对应的profileID
const [walletAddress, setWalletAddress] = useState<string>(null);
const profilesWhere = {};
if (walletAddress) {
Object.assign(profilesWhere, { ownedBy: [walletAddress] });
}
const { data: profiles } = useProfiles({
where: profilesWhere,
});
const firstProfile = profiles?.[0] || { id: '' };
const { id: firstProfileId } = firstProfile;
useEffect(() => {
walletProfileId = firstProfileId;
}, [firstProfileId]);

// const { execute: fetchProfile } = useLazyProfile();
const lensLoginStartRef = useRef(false);
const lensLoginAdpater = async (connector: Connector) => {
const chainId = await connector.getChainId();
Expand All @@ -123,7 +155,11 @@ export function LensAuthProvider({ children }: PropsWithChildren) {
}
const walletClient = await connector.getWalletClient();
const { address } = walletClient.account;
await login({ address });
walletProfileId = '';
setWalletAddress(address);
const profileId = await getWalletProfileId();

await login({ address, profileId });
};

const { connector: activeConnector, isConnected } = useAccount({
Expand Down
4 changes: 3 additions & 1 deletion apps/u3/src/hooks/social/lens/useCreateLensComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export function useCreateLensComment(props?: {
commentOn: {
id: publicationId,
},
metadata,
metadata: {
...(metadata?.lens || {}),
},
});
toast.success('Comment successfully!');
} catch (err) {
Expand Down
9 changes: 6 additions & 3 deletions apps/u3/src/hooks/social/lens/useCreateLensPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ export function useCreateLensPost() {

const createPost = useCallback(
async (content: string, attachments?: MediaImage[]) => {
const metadata = article({
const metadataAttrs = {
content,
attachments,
});
};
if (attachments && attachments.length > 0) {
Object.assign(metadataAttrs, { attachments });
}
const metadata = article(metadataAttrs);
const uri = await lensUploadToArweave(metadata);
const result = await execute({
metadata: uri,
Expand Down
4 changes: 3 additions & 1 deletion apps/u3/src/utils/profile/biolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export const isFarcasterHandle = (handle: string) => {
};
export const lensHandleToBioLinkHandle = (handle: string) => {
return handle
? `${handle.replace(/\.[^.]+$/, '')}.${BIOLINK_LENS_SUFFIX}`
? `${handle
.replace('test/', '')
.replace(/\.[^.]+$/, '')}.${BIOLINK_LENS_SUFFIX}`
: '';
};
export const farcasterHandleToBioLinkHandle = (handle: string) => {
Expand Down
8 changes: 4 additions & 4 deletions apps/u3/src/utils/social/lens/lens-ui-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const lensPublicationToPostCardData = (
return {
platform: SocialPlatform.Lens,
avatar: getAvatar(publication?.by),
name: getName(publication?.by),
handle: getHandle(publication?.by),
name: getName(publication?.by) || publication?.by?.id,
handle: getHandle(publication?.by) || publication?.by?.id,
createdAt: publication?.createdAt || '',
content: getContent(publication?.metadata),
totalLikes: publication?.stats?.upvotes || 0,
Expand All @@ -29,8 +29,8 @@ export const lensPublicationToReplyCardData = (
return {
platform: SocialPlatform.Lens,
avatar: getAvatar(publication?.by),
name: getName(publication?.by),
handle: getHandle(publication?.by),
name: getName(publication?.by) || publication?.by?.id,
handle: getHandle(publication?.by) || publication?.by?.id,
createdAt: publication?.createdAt || '',
content: getContent(publication?.metadata),
totalLikes: publication?.stats?.upvotes || 0,
Expand Down

0 comments on commit af5929e

Please sign in to comment.