Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
feat(lib): react to changes in users atom and expose loading flags (#132
Browse files Browse the repository at this point in the history
)

* feat(lib): react to changes in users atom and expose loading flags

---------

Co-authored-by: PubNub Release Bot <[email protected]>
  • Loading branch information
piotr-suwala and pubnub-release-bot authored Dec 11, 2023
1 parent 6447a8e commit a941257
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 19 deletions.
7 changes: 6 additions & 1 deletion .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
---
name: pubnub-react-chat-components
version: v0.32.0
version: v0.33.0
scm: github.com/pubnub/react-chat-components
schema: 1
files:
- lib/dist/index.js
- lib/dist/index.es.js
changelog:
- date: 2023-12-11
version: v0.33.0
changes:
- type: feature
text: "Expose "isLoading" flags to hooks."
- date: 2023-10-05
version: v0.32.0
changes:
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubnub/common-chat-components",
"version": "0.32.0",
"version": "0.33.0",
"main": "src/index.ts",
"license": "MIT",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/hooks/use-channel-members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { usePubNub } from "pubnub-react";
import { merge, cloneDeep } from "lodash";
import { UserEntityWithMembership } from "../types";

type HookReturnValue = [UserEntityWithMembership[], () => void, () => void, number, Error];
type HookReturnValue = [UserEntityWithMembership[], () => void, () => void, number, Error, boolean];

export const useChannelMembers = (options: GetChannelMembersParameters): HookReturnValue => {
const jsonOptions = JSON.stringify(options);
Expand Down Expand Up @@ -110,5 +110,5 @@ export const useChannelMembers = (options: GetChannelMembersParameters): HookRet
};
}, [pubnub, paginatedOptions.channel]);

return [members, fetchMoreMembers, resetHook, totalCount, error];
return [members, fetchMoreMembers, resetHook, totalCount, error, doFetch];
};
4 changes: 2 additions & 2 deletions packages/common/src/hooks/use-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { usePubNub } from "pubnub-react";
import { merge, cloneDeep } from "lodash";
import { ChannelEntity } from "../types";

type HookReturnValue = [ChannelEntity[], () => void, number, Error];
type HookReturnValue = [ChannelEntity[], () => void, number, Error, boolean];

export const useChannels = (options: GetAllMetadataParameters = {}): HookReturnValue => {
const pubnub = usePubNub();
Expand Down Expand Up @@ -79,5 +79,5 @@ export const useChannels = (options: GetAllMetadataParameters = {}): HookReturnV
};
}, [pubnub]);

return [channels, fetchMoreChannels, totalCount, error];
return [channels, fetchMoreChannels, totalCount, error, doFetch];
};
4 changes: 2 additions & 2 deletions packages/common/src/hooks/use-presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { usePubNub } from "pubnub-react";
import { cloneDeep } from "lodash";

type ChannelsOccupancy = HereNowResponse["channels"];
type HookReturnValue = [ChannelsOccupancy, () => void, number, Error];
type HookReturnValue = [ChannelsOccupancy, () => void, number, Error, boolean];

export const usePresence = (options: HereNowParameters = {}): HookReturnValue => {
const jsonOptions = JSON.stringify(options);
Expand Down Expand Up @@ -97,5 +97,5 @@ export const usePresence = (options: HereNowParameters = {}): HookReturnValue =>
};
}, [pubnub, options.includeUUIDs]);

return [presence, resetHook, total, error];
return [presence, resetHook, total, error, doFetch];
};
11 changes: 9 additions & 2 deletions packages/common/src/hooks/use-user-memberships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { usePubNub } from "pubnub-react";
import { merge, cloneDeep } from "lodash";
import { ChannelEntityWithMembership } from "../types";

type HookReturnValue = [ChannelEntityWithMembership[], () => void, () => void, number, Error];
type HookReturnValue = [
ChannelEntityWithMembership[],
() => void,
() => void,
number,
Error,
boolean
];

export const useUserMemberships = (options: GetMembershipsParametersv2 = {}): HookReturnValue => {
const jsonOptions = JSON.stringify(options);
Expand Down Expand Up @@ -111,5 +118,5 @@ export const useUserMemberships = (options: GetMembershipsParametersv2 = {}): Ho
};
}, [pubnub, paginatedOptions.uuid]);

return [channels, fetchMoreMemberships, resetHook, totalCount, error];
return [channels, fetchMoreMemberships, resetHook, totalCount, error, doFetch];
};
4 changes: 2 additions & 2 deletions packages/common/src/hooks/use-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { usePubNub } from "pubnub-react";
import { cloneDeep } from "lodash";
import { UserEntity } from "../types";

export const useUser = (options: GetUUIDMetadataParameters = {}): [UserEntity, Error] => {
export const useUser = (options: GetUUIDMetadataParameters = {}): [UserEntity, Error, boolean] => {
const jsonOptions = JSON.stringify(options);

const pubnub = usePubNub();
Expand Down Expand Up @@ -68,5 +68,5 @@ export const useUser = (options: GetUUIDMetadataParameters = {}): [UserEntity, E
};
}, [pubnub]);

return [user, error];
return [user, error, doFetch];
};
4 changes: 2 additions & 2 deletions packages/common/src/hooks/use-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { usePubNub } from "pubnub-react";
import { merge, cloneDeep } from "lodash";
import { UserEntity } from "../types";

type HookReturnValue = [UserEntity[], () => void, number, Error];
type HookReturnValue = [UserEntity[], () => void, number, Error, boolean];

export const useUsers = (options: GetAllMetadataParameters = {}): HookReturnValue => {
const pubnub = usePubNub();
Expand Down Expand Up @@ -79,5 +79,5 @@ export const useUsers = (options: GetAllMetadataParameters = {}): HookReturnValu
};
}, [pubnub]);

return [users, fetchMoreUsers, totalCount, error];
return [users, fetchMoreUsers, totalCount, error, doFetch];
};
4 changes: 3 additions & 1 deletion packages/common/test/use-channel-members.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ describe("useChannelMembers", () => {
const { result } = renderHook(() => useChannelMembers({ channel: "test-channel" }), {
wrapper,
});
expect(result.current[5]).toBe(true);

await waitFor(() => {
const [receivedMembers, fetchMore, resetHook, total, error] = result.current;
const [receivedMembers, fetchMore, resetHook, total, error, loading] = result.current;
expect(receivedMembers).toHaveLength(users.length);
expect(typeof fetchMore).toEqual("function");
expect(typeof resetHook).toEqual("function");
expect(total).toEqual(users.length);
expect(error).toEqual(undefined);
expect(loading).toBe(false);
});
});

Expand Down
7 changes: 5 additions & 2 deletions packages/common/test/use-channels.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ const wrapper = ({ children }) => <PubNubProvider client={pubnub}>{children}</Pu
describe("useChannels", () => {
test("fetches and returns the full list of channels", async () => {
const { result } = renderHook(() => useChannels(), { wrapper });
expect(result.current[4]).toBe(true);

await waitFor(() => {
const [receivedChannels, fetchMore, total, error] = result.current;
const [receivedChannels, fetchMore, total, error, loading] = result.current;
expect(receivedChannels).toHaveLength(channels.length);
expect(typeof fetchMore).toEqual("function");
expect(total).toEqual(channels.length);
expect(error).toEqual(undefined);
expect(loading).toEqual(false);
});
});

Expand All @@ -27,9 +29,10 @@ describe("useChannels", () => {
const { result } = renderHook(() => useChannels({ limit }), { wrapper });

await waitFor(() => {
const [receivedChannels, fetchMore, total] = result.current;
const [receivedChannels, fetchMore, total, , loading] = result.current;
expect(receivedChannels).toHaveLength(limit);
expect(total).toEqual(channels.length);
expect(loading).toEqual(false);
fetchMore();
});

Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubnub/react-native-chat-components",
"version": "0.32.0",
"version": "0.33.0",
"description": "PubNub Chat Components is a development kit of React Native components that aims to help you to easily build Chat applications using PubNub infrastructure. It removes the complexicity of picking an adequate Chat engine, learning its APIs and dealing with its low-level internals. As the same time it allows you to create apps of various use cases, with different functionalities and customizable looks.",
"author": "PubNub <[email protected]>",
"main": "dist/commonjs/index",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubnub/react-chat-components",
"version": "0.32.0",
"version": "0.33.0",
"description": "PubNub Chat Components is a development kit of React components that aims to help you to easily build Chat applications using PubNub infrastructure. It removes the complexicity of picking an adequate Chat engine, learning its APIs and dealing with its low-level internals. As the same time it allows you to create apps of various use cases, with different functionalities and customizable looks.",
"author": "PubNub <[email protected]>",
"main": "dist/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/message-list/message-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type ItemProps = Pick<
export const MessageListItem = React.memo(Item, (prev, next) => {
if (prev.envelope.timetoken !== next.envelope.timetoken) return false;
if (!isEqual(prev.envelope.actions, next.envelope.actions)) return false;
if (!isEqual(prev.users, next.users)) return false;
return true;
});

Expand Down

0 comments on commit a941257

Please sign in to comment.