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

react: add useProfileActionHistory and useWhoReactedToPublication #569

Merged
merged 3 commits into from
Oct 6, 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
13 changes: 12 additions & 1 deletion examples/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import {
ProfilesPage,
UseMutualFollowers,
UseProfile,
UseProfileActionHistory,
UseProfileFollowers,
UseProfileFollowing,
UseProfiles,
UseRecommendedProfiles,
UseWhoActedOnPublication,
} from './profiles';
import { PublicationsPage, UsePublication, UsePublications } from './publications';
import {
PublicationsPage,
UsePublication,
UsePublications,
UseWhoReactedToPublication,
} from './publications';
import { localStorage } from './storage';

const { publicClient, webSocketPublicClient } = configureChains(
Expand Down Expand Up @@ -69,6 +75,10 @@ export function App() {
<Route index element={<PublicationsPage />} />
<Route path="usePublication" element={<UsePublication />} />
<Route path="usePublications" element={<UsePublications />} />
<Route
path="useWhoReactedToPublication"
element={<UseWhoReactedToPublication />}
/>
</Route>

<Route path="/profiles">
Expand All @@ -80,6 +90,7 @@ export function App() {
<Route path="useMutualFollowers" element={<UseMutualFollowers />} />
<Route path="useRecommendedProfiles" element={<UseRecommendedProfiles />} />
<Route path="useWhoActedOnPublication" element={<UseWhoActedOnPublication />} />
<Route path="useProfileActionHistory" element={<UseProfileActionHistory />} />
</Route>

<Route path="/discovery">
Expand Down
5 changes: 5 additions & 0 deletions examples/web/src/profiles/ProfilesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ const profileHooks = [
description: `Fetch a list of profiles who acted on a publication.`,
path: '/profiles/useWhoActedOnPublication',
},
{
label: 'useProfileActionHistory',
description: `Fetch profile action history.`,
path: '/profiles/useProfileActionHistory',
},
];

export function ProfilesPage() {
Expand Down
37 changes: 37 additions & 0 deletions examples/web/src/profiles/UseProfileActionHistory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useProfileActionHistory } from '@lens-protocol/react';

import { ErrorMessage } from '../components/error/ErrorMessage';
import { Loading } from '../components/loading/Loading';
import { useInfiniteScroll } from '../hooks/useInfiniteScroll';

// TODO requires authenticated profile
export function UseProfileActionHistory() {
const {
data: history,
error,
loading,
hasMore,
observeRef,
} = useInfiniteScroll(useProfileActionHistory());

if (loading) return <Loading />;

if (error) return <ErrorMessage error={error} />;

return (
<div>
<h1>
<code>useProfileActionHistory</code>
</h1>
<div>
{history.map((item) => (
<div key={item.id}>
{item.actionType} {item.actionedOn}
</div>
))}

{hasMore && <p ref={observeRef}>Loading more...</p>}
</div>
</div>
);
}
1 change: 1 addition & 0 deletions examples/web/src/profiles/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './ProfilesPage';
export * from './UseMutualFollowers';
export * from './UseProfile';
export * from './UseProfileActionHistory';
export * from './UseProfileFollowers';
export * from './UseProfileFollowing';
export * from './UseProfiles';
Expand Down
5 changes: 5 additions & 0 deletions examples/web/src/publications/PublicationsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const publicationHooks = [
description: `Fetch a list of publications.`,
path: '/publications/usePublications',
},
{
label: 'useWhoReactedToPublication',
description: `Fetch a list of profiles who reacted to a publication.`,
path: '/publications/useWhoReactedToPublication',
},
];

export function PublicationsPage() {
Expand Down
48 changes: 48 additions & 0 deletions examples/web/src/publications/UseWhoReactedToPublication.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { publicationId, useWhoReactedToPublication } from '@lens-protocol/react';

import { ErrorMessage } from '../components/error/ErrorMessage';
import { Loading } from '../components/loading/Loading';
import { useInfiniteScroll } from '../hooks/useInfiniteScroll';
import { ProfileCard } from '../profiles/components/ProfileCard';

export function UseWhoReactedToPublication() {
const {
data: reactions,
error,
loading,
hasMore,
observeRef,
} = useInfiniteScroll(
useWhoReactedToPublication({
for: publicationId('0x04-0x0b'),
}),
);

if (loading) return <Loading />;

if (error) return <ErrorMessage error={error} />;

return (
<div>
<h1>
<code>useWhoReactedToPublication</code>
</h1>
<div>
{reactions.map((p) => (
<div key={p.profile.id}>
<ProfileCard profile={p.profile} />
<div>
{p.reactions.map((r) => (
<div key={r.reactionAt}>
{r.reaction} at {r.reactionAt}
</div>
))}
</div>
</div>
))}

{hasMore && <p ref={observeRef}>Loading more...</p>}
</div>
</div>
);
}
1 change: 1 addition & 0 deletions examples/web/src/publications/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './PublicationsPage';
export * from './UsePublication';
export * from './UsePublications';
export * from './UseWhoReactedToPublication';
4 changes: 4 additions & 0 deletions packages/api-bindings/src/apollo/cache/createTypePolicies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {
createFollowersFieldPolicy,
createFollowingFieldPolicy,
createMutualFollowersFieldPolicy,
createProfileActionHistoryFieldPolicy,
createProfileRecommendationsFieldPolicy,
createProfilesFieldPolicy,
createPublicationsFieldPolicy,
createSearchProfilesFieldPolicy,
createSearchPublicationsFieldPolicy,
createWhoActedOnPublicationFieldPolicy,
createWhoReactedPublicationFieldPolicy,
} from './field-policies';
import { notNormalizedType } from './utils/notNormalizedType';

Expand All @@ -40,12 +42,14 @@ export function createTypePolicies(
followers: createFollowersFieldPolicy(),
following: createFollowingFieldPolicy(),
mutualFollowers: createMutualFollowersFieldPolicy(),
profileActionHistory: createProfileActionHistoryFieldPolicy(),
profileRecommendations: createProfileRecommendationsFieldPolicy(),
profiles: createProfilesFieldPolicy(),
publications: createPublicationsFieldPolicy(),
searchProfiles: createSearchProfilesFieldPolicy(),
searchPublications: createSearchPublicationsFieldPolicy(),
whoActedOnPublication: createWhoActedOnPublicationFieldPolicy(),
whoReactedPublication: createWhoReactedPublicationFieldPolicy(),
...createQueryParamsLocalFields(params),
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cursorBasedPagination } from '../utils/cursorBasedPagination';

export function createProfileActionHistoryFieldPolicy() {
return cursorBasedPagination([]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if this is needed if there are no arguments

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cursorBasedPagination } from '../utils/cursorBasedPagination';

export function createWhoReactedPublicationFieldPolicy() {
return cursorBasedPagination([['request', ['for', 'where', ['anyOf']]]]);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export * from './createFollowersFieldPolicy';
export * from './createFollowingFieldPolicy';
export * from './createMutualFollowersFieldPolicy';
export * from './createProfileActionHistoryFieldPolicy';
export * from './createProfileRecommendationsFieldPolicy';
export * from './createProfilesFieldPolicy';
export * from './createPublicationsFieldPolicy';
export * from './createSearchProfilesFieldPolicy';
export * from './createSearchPublicationsFieldPolicy';
export * from './createProfileRecommendationsFieldPolicy';
export * from './createWhoActedOnPublicationFieldPolicy';
export * from './createWhoReactedPublicationFieldPolicy';
42 changes: 42 additions & 0 deletions packages/api-bindings/src/lens/__helpers__/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import {
PaginatedResultInfo,
Post,
Profile,
ProfileActionHistory,
ProfileActionHistoryType,
ProfileReactionResult,
ProfileStats,
ProfileWhoReactedResult,
PublicationOperations,
PublicationReactionType,
PublicationStats,
TextOnlyMetadataV3,
TriStateValue,
Expand Down Expand Up @@ -232,6 +237,43 @@ export function mockFeedItemFragment(overrides?: Partial<FeedItem>): FeedItem {
comments: [],
mirrors: [],
reactions: [],

...overrides,
};
}

export function mockProfileActionHistoryFragment(
overrides: Partial<ProfileActionHistory> = {},
): ProfileActionHistory {
return {
id: faker.datatype.number(),
actionType: ProfileActionHistoryType.LoggedIn,
who: mockEvmAddress(),
txHash: mockTransactionHash(),
actionedOn: faker.date.past().toISOString(),

...overrides,
};
}

export function mockProfileReactionResultFragment(
overrides: Partial<ProfileReactionResult> = {},
): ProfileReactionResult {
return {
reaction: PublicationReactionType.Upvote,
reactionAt: faker.date.past().toISOString(),

...overrides,
};
}

export function mockProfileWhoReactedResultFragment(
overrides: Partial<ProfileWhoReactedResult> = {},
): ProfileWhoReactedResult {
return {
profile: mockProfileFragment(),
reactions: [mockProfileReactionResultFragment()],

...overrides,
};
}
48 changes: 48 additions & 0 deletions packages/api-bindings/src/lens/__helpers__/queries/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import {
MutualFollowersVariables,
PaginatedResultInfo,
Profile,
ProfileActionHistory,
ProfileActionHistoryDocument,
ProfileActionHistoryVariables,
ProfileRecommendationsDocument,
ProfileRecommendationsVariables,
ProfileWhoReactedResult,
ProfilesDocument,
ProfilesVariables,
SearchProfilesDocument,
SearchProfilesVariables,
WhoActedOnPublicationDocument,
WhoActedOnPublicationVariables,
WhoReactedPublicationDocument,
WhoReactedPublicationVariables,
} from '../../graphql/generated';
import { mockPaginatedResultInfo } from '../fragments';
import { mockAnyPaginatedResponse } from './mockAnyPaginatedResponse';
Expand Down Expand Up @@ -137,3 +143,45 @@ export function mockWhoActedOnPublicationResponse({
query: WhoActedOnPublicationDocument,
});
}

export function mockProfileActionHistoryResponse({
variables,
items,
info = mockPaginatedResultInfo(),
}: {
variables: ProfileActionHistoryVariables;
items: ProfileActionHistory[];
info?: PaginatedResultInfo;
}) {
return {
request: {
query: ProfileActionHistoryDocument,
variables,
},
result: {
data: {
result: {
items,
pageInfo: info,
},
},
},
};
}

export function mockWhoReactedToPublicationResponse({
variables,
items,
info = mockPaginatedResultInfo(),
}: {
variables: WhoReactedPublicationVariables;
items: ProfileWhoReactedResult[];
info?: PaginatedResultInfo;
}) {
return mockAnyPaginatedResponse({
variables,
items,
info,
query: WhoReactedPublicationDocument,
});
}
Loading