Skip to content

Commit

Permalink
fix: lens v2 login & create post
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlysxw committed Nov 10, 2023
1 parent 84af4a0 commit 9f76662
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
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
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
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

0 comments on commit 9f76662

Please sign in to comment.