Skip to content

Commit

Permalink
Merge pull request #243 from us3r-network/F-lensV2-bufan
Browse files Browse the repository at this point in the history
upgrade notification and recommend follow
  • Loading branch information
friendlysxw authored Nov 10, 2023
2 parents ca0c79f + 9086cca commit d7b749e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 28 deletions.
13 changes: 9 additions & 4 deletions apps/u3/src/components/social/SocialWhoToFollow.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Profile,
useFollow,
useProfilesToFollow,
useRecommendedProfiles,
} from '@lens-protocol/react-web';
import styled from 'styled-components';
import useFarcasterCurrFid from 'src/hooks/social/farcaster/useFarcasterCurrFid';
Expand All @@ -26,12 +26,17 @@ const SUGGEST_NUM = 3;
export default function SocialWhoToFollow() {
const navigate = useNavigate();
const { isLogin: isLoginU3 } = useLogin();
const { isLogin: isLoginLens } = useLensCtx();
const { data: lensProfiles } = useProfilesToFollow();
const { isLogin: isLoginLens, sessionProfile: lensProfile } = useLensCtx();
const { data: lensProfiles } = useRecommendedProfiles({
for: lensProfile.id,
});
const lensRecommendedProfiles: Profile[] = useMemo(
() =>
lensProfiles
?.filter((profile) => profile.name && profile.name !== '')
?.filter(
(profile) =>
profile.metadata.displayName && profile.metadata.displayName !== ''
)
.slice(0, SUGGEST_NUM),
[lensProfiles, isLoginLens]
);
Expand Down
13 changes: 9 additions & 4 deletions apps/u3/src/container/social/SocialSuggestFollow.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Profile,
useFollow,
useProfilesToFollow,
useRecommendedProfiles,
} from '@lens-protocol/react-web';
import styled from 'styled-components';
import useFarcasterCurrFid from 'src/hooks/social/farcaster/useFarcasterCurrFid';
Expand Down Expand Up @@ -33,12 +33,17 @@ export default function SocialSuggestFollow() {
socialPlatform: SocialPlatform | '';
}>();
const { isLogin: isLoginU3 } = useLogin();
const { isLogin: isLoginLens } = useLensCtx();
const { data: lensProfiles, loading: loadingLens } = useProfilesToFollow();
const { isLogin: isLoginLens, sessionProfile: lensProfile } = useLensCtx();
const { data: lensProfiles, loading: loadingLens } = useRecommendedProfiles({
for: lensProfile.id,
});
const lensRecommendedProfiles: Profile[] = useMemo(
() =>
lensProfiles
?.filter((profile) => profile.name && profile.name !== '')
?.filter(
(profile) =>
profile.metadata.displayName && profile.metadata.displayName !== ''
)
.slice(0, SUGGEST_NUM),
[lensProfiles, isLoginLens]
);
Expand Down
98 changes: 78 additions & 20 deletions apps/u3/src/contexts/notification/NotificationStoreCtx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {

import {
useNotifications as useLensNotifications,
useUnreadNotificationCount as useUnreadLensNotificationCount,
Notification as LensNotification,
ProfileId,
LimitType,
} from '@lens-protocol/react-web';

import { debounce } from 'lodash';
Expand Down Expand Up @@ -44,6 +44,69 @@ const defaultContextValue: NotificationStoreCtxValue = {
const DEFAULT_PAGE_SIZE = 10;
export const NotificationStoreCtx = createContext(defaultContextValue);

// const mergeNotifications = (
// lensNotifications: LensNotification[] | undefined,
// farcasterNotifications: FarcasterNotification[] | undefined
// ) => {
// if (!lensNotifications && !farcasterNotifications) {
// return [] as (LensNotification | FarcasterNotification)[];
// }
// if (
// (!lensNotifications || lensNotifications.length === 0) &&
// farcasterNotifications
// ) {
// return farcasterNotifications as (
// | LensNotification
// | FarcasterNotification
// )[];
// }
// if (
// lensNotifications &&
// (!farcasterNotifications || farcasterNotifications.length === 0)
// ) {
// return lensNotifications as (LensNotification | FarcasterNotification)[];
// }
// if (
// lensNotifications &&
// farcasterNotifications &&
// lensNotifications.length > 0 &&
// farcasterNotifications.length > 0
// ) {
// const notifications = [];
// let lensIndex = 0;
// let farcasterIndex = 0;
// while (
// lensIndex < lensNotifications.length &&
// farcasterIndex < farcasterNotifications.length
// ) {
// if (lensNotifications.length === 0 && farcasterNotifications.length > 0) {
// notifications.push(farcasterNotifications[farcasterIndex]);
// farcasterIndex++;
// } else if (
// farcasterNotifications.length === 0 &&
// lensNotifications.length > 0
// ) {
// notifications.push(lensNotifications[lensIndex]);
// lensIndex++;
// } else if (
// lensNotifications[lensIndex].createdAt >
// farcasterNotifications[farcasterIndex].message_timestamp
// ) {
// notifications.push(lensNotifications[lensIndex]);
// lensIndex++;
// } else {
// notifications.push(farcasterNotifications[farcasterIndex]);
// farcasterIndex++;
// }
// }
// return notifications;
// }
// return [] as (LensNotification | FarcasterNotification)[];
// };

// this is a temp merge function
// because notification type in lens sdk, which is NOT completed yet, has NO timestamp to compare
// so we need to merge lens notification and farcaster notification one by one
const mergeNotifications = (
lensNotifications: LensNotification[] | undefined,
farcasterNotifications: FarcasterNotification[] | undefined
Expand Down Expand Up @@ -88,10 +151,7 @@ const mergeNotifications = (
) {
notifications.push(lensNotifications[lensIndex]);
lensIndex++;
} else if (
lensNotifications[lensIndex].createdAt >
farcasterNotifications[farcasterIndex].message_timestamp
) {
} else if (lensIndex < farcasterIndex) {
notifications.push(lensNotifications[lensIndex]);
lensIndex++;
} else {
Expand Down Expand Up @@ -126,21 +186,22 @@ export function NotificationStoreProvider({
defaultContextValue.unreadCount
);

// get unread notification count
const { unreadNotificationCount: unreadLensCount, clear: clearLensUnread } =
useUnreadLensNotificationCount({
profileId: config.lensProfileId,
});
let lensPageSize: LimitType;
if (config.pageSize > 50) {
lensPageSize = LimitType.Fifty;
} else if (config.pageSize > 25) {
lensPageSize = LimitType.TwentyFive;
} else {
lensPageSize = LimitType.Ten;
}

const {
data: lensNotifications,
loading: lensNotificationsLoading,
hasMore: lensNotificationsHasMore,
next: loadMoreLensNotifications,
} = useLensNotifications({
profileId: config.lensProfileId,
limit: config.pageSize || DEFAULT_PAGE_SIZE,
highSignalFilter: false,
limit: lensPageSize,
});

// farcaster notifications
Expand Down Expand Up @@ -193,20 +254,17 @@ export function NotificationStoreProvider({
]);

const clearUnread = useCallback(async () => {
if (clearLensUnread) {
await clearLensUnread();
await clearFarcasterUnread();
}
await clearFarcasterUnread();
setUnreadCount(0);
}, [clearLensUnread, clearFarcasterUnread]);
}, [clearFarcasterUnread]);

useEffect(() => {
debounce(updateNotifications, 500)();
}, [updateNotifications, config]);

useEffect(() => {
setUnreadCount(unreadLensCount + unreadFarcasterCount);
}, [unreadLensCount, unreadFarcasterCount, config]);
setUnreadCount(unreadFarcasterCount);
}, [unreadFarcasterCount, config]);

return (
<NotificationStoreCtx.Provider
Expand Down

0 comments on commit d7b749e

Please sign in to comment.