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

Fix TS checks in the common package #75

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 3 additions & 8 deletions .github/workflows/release/reviewers.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{
"packages/react-native/src/**/*": {
"users": [
"techwritermat",
"are"
]
"users": ["techwritermat", "are"]
},
"packages/react/src/**/*.ts": {
"users": [
"parfeon"
]
"users": ["parfeon"]
}
}
}
4 changes: 2 additions & 2 deletions packages/common/src/channel-list/channel-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export const useChannelListCore = (props: CommonChannelListProps) => {

const channelSorter = (a: ChannelEntity, b: ChannelEntity) => {
if (props.sort) return props.sort(a, b);
return a?.name?.localeCompare(b.name as string, "en", { sensitivity: "base" });
return a?.name?.localeCompare(b.name as string, "en", { sensitivity: "base" }) as number;
};

const channelFromString = (channel: ChannelEntity | string) => {
if (typeof channel === "string") {
return {
id: channel,
name: channel,
};
} as ChannelEntity;
}
return channel;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const getNameInitials = (name: string): string => {
return initials.toUpperCase();
};

export const getPredefinedColor = (uuid: string): string => {
export const getPredefinedColor = (uuid: string): string | undefined => {
if (!uuid || !uuid.length) return;
const colors = ["#80deea", "#9fa7df", "#aed581", "#ce93d8", "#ef9a9a", "#ffab91", "#ffe082"];
const sum = uuid
Expand Down
12 changes: 6 additions & 6 deletions packages/common/src/hooks/use-channel-members.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect, useMemo } from "react";
import { GetChannelMembersParameters } from "pubnub";
import { GetChannelMembersParameters, ObjectsEvent } from "pubnub";
import { usePubNub } from "pubnub-react";
import { merge, cloneDeep } from "lodash";
import { UserEntity } from "../types";
Expand Down Expand Up @@ -55,11 +55,11 @@ export const useChannelMembers = (options: GetChannelMembersParameters): HookRet
...members,
...(response.data.map((m) => m.uuid) as UserEntity[]),
]);
setTotalCount(response.totalCount);
setPage(response.next);
setTotalCount(response.totalCount || 0);
setPage(response.next || "");
} catch (e) {
setDoFetch(false);
setError(e);
setError(e as Error);
}
}

Expand All @@ -70,7 +70,7 @@ export const useChannelMembers = (options: GetChannelMembersParameters): HookRet

useEffect(() => {
const listener = {
objects: (event) => {
objects: (event: ObjectsEvent) => {
const message = event.message;
if (message.type !== "membership") return;

Expand Down Expand Up @@ -99,5 +99,5 @@ export const useChannelMembers = (options: GetChannelMembersParameters): HookRet
};
}, [pubnub, paginatedOptions.channel]);

return [members, fetchMoreMembers, resetHook, totalCount, error];
return [members, fetchMoreMembers, resetHook, totalCount, error as Error];
};
12 changes: 6 additions & 6 deletions packages/common/src/hooks/use-channels.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect } from "react";
import { GetAllMetadataParameters } from "pubnub";
import { GetAllMetadataParameters, ObjectsEvent } from "pubnub";
import { usePubNub } from "pubnub-react";
import { merge, cloneDeep } from "lodash";
import { ChannelEntity } from "../types";
Expand All @@ -10,7 +10,7 @@ export const useChannels = (options: GetAllMetadataParameters = {}): HookReturnV
const pubnub = usePubNub();

const [channels, setChannels] = useState<ChannelEntity[]>([]);
const [page, setPage] = useState("");
const [page, setPage] = useState<string | undefined>("");
const [totalCount, setTotalCount] = useState(0);
const [error, setError] = useState<Error>();
const [doFetch, setDoFetch] = useState(true);
Expand All @@ -35,11 +35,11 @@ export const useChannels = (options: GetAllMetadataParameters = {}): HookReturnV
if (ignoreRequest) return;
setDoFetch(false);
setChannels((channels) => [...channels, ...response.data]);
setTotalCount(response.totalCount);
setTotalCount(response.totalCount || 0);
setPage(response.next);
} catch (e) {
setDoFetch(false);
setError(e);
setError(e as Error);
}
}

Expand All @@ -50,7 +50,7 @@ export const useChannels = (options: GetAllMetadataParameters = {}): HookReturnV

useEffect(() => {
const listener = {
objects: (event) => {
objects: (event: ObjectsEvent) => {
const message = event.message;
if (message.type !== "channel") return;

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 as Error];
};
3 changes: 3 additions & 0 deletions packages/common/src/hooks/use-messages.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This file is unused so there is no need to keep it ts-checked
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { useState, useEffect, useRef } from "react";
import { FetchMessagesParameters, MessageActionEvent, MessageEvent } from "pubnub";
import { usePubNub } from "pubnub-react";
Expand Down
8 changes: 4 additions & 4 deletions packages/common/src/hooks/use-presence.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect } from "react";
import { HereNowParameters, HereNowResponse } from "pubnub";
import { HereNowParameters, HereNowResponse, PresenceEvent } from "pubnub";
import { usePubNub } from "pubnub-react";
import { cloneDeep } from "lodash";

Expand Down Expand Up @@ -39,7 +39,7 @@ export const usePresence = (options: HereNowParameters = {}): HookReturnValue =>
setPresence(response.channels);
} catch (e) {
setDoFetch(false);
setError(e);
setError(e as Error);
}
}

Expand All @@ -50,7 +50,7 @@ export const usePresence = (options: HereNowParameters = {}): HookReturnValue =>

useEffect(() => {
const listener = {
presence: (event) => {
presence: (event: PresenceEvent) => {
setPresence((presence) => {
const presenceClone = cloneDeep(presence);
if (!presenceClone[event.channel])
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 as Error];
};
8 changes: 6 additions & 2 deletions packages/common/src/hooks/use-subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ export const useSubscribe = (options: SubscribeParameters = {}): (() => void) =>
const currentGroups = pubnub.getSubscribedChannelGroups() || [];

const subscribeChannels = options.channels.filter((c) => !currentSubscriptions.includes(c));
const unsubscribeChannels = currentSubscriptions.filter((c) => !options.channels.includes(c));
const unsubscribeChannels = currentSubscriptions.filter(
(c) => !(options.channels as string[]).includes(c)
);

const subscribeGroups = options.channelGroups.filter((c) => !currentGroups.includes(c));
const unsubscribeGroups = currentGroups.filter((c) => !options.channelGroups.includes(c));
const unsubscribeGroups = currentGroups.filter(
(c) => !(options.channelGroups as string[]).includes(c)
);

if (subscribeChannels.length || subscribeGroups.length) {
pubnub.subscribe({
Expand Down
14 changes: 7 additions & 7 deletions packages/common/src/hooks/use-user-memberships.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useState, useEffect, useMemo } from "react";
import { GetMembershipsParametersv2 } from "pubnub";
import { GetMembershipsParametersv2, ObjectsEvent } from "pubnub";
import { usePubNub } from "pubnub-react";
import { merge, cloneDeep } from "lodash";
import { ChannelEntity } from "../types";

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

export const useUserMemberships = (options: GetMembershipsParametersv2 = {}): HookReturnValue => {
const jsonOptions = JSON.stringify(options);

const pubnub = usePubNub();
const [channels, setChannels] = useState<ChannelEntity[]>([]);
const [totalCount, setTotalCount] = useState(0);
const [page, setPage] = useState("");
const [totalCount, setTotalCount] = useState<number | undefined>(0);
const [page, setPage] = useState<string | undefined>("");
const [error, setError] = useState<Error>();
const [doFetch, setDoFetch] = useState(true);

Expand Down Expand Up @@ -59,7 +59,7 @@ export const useUserMemberships = (options: GetMembershipsParametersv2 = {}): Ho
setPage(response.next);
} catch (e) {
setDoFetch(false);
setError(e);
setError(e as Error);
}
}

Expand All @@ -70,7 +70,7 @@ export const useUserMemberships = (options: GetMembershipsParametersv2 = {}): Ho

useEffect(() => {
const listener = {
objects: (event) => {
objects: (event: ObjectsEvent) => {
const message = event.message;
if (message.type !== "membership") return;

Expand Down Expand Up @@ -100,5 +100,5 @@ export const useUserMemberships = (options: GetMembershipsParametersv2 = {}): Ho
};
}, [pubnub, paginatedOptions.uuid]);

return [channels, fetchMoreMemberships, resetHook, totalCount, error];
return [channels, fetchMoreMemberships, resetHook, totalCount, error as Error];
};
14 changes: 7 additions & 7 deletions packages/common/src/hooks/use-user.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useState, useEffect } from "react";
import { GetUUIDMetadataParameters } from "pubnub";
import { GetUUIDMetadataParameters, ObjectsEvent } from "pubnub";
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 | null, Error] => {
const jsonOptions = JSON.stringify(options);

const pubnub = usePubNub();
const [user, setUser] = useState(null);
const [user, setUser] = useState<UserEntity | null>(null);
const [error, setError] = useState<Error>();
const [doFetch, setDoFetch] = useState(true);

Expand All @@ -34,7 +34,7 @@ export const useUser = (options: GetUUIDMetadataParameters = {}): [UserEntity, E
setUser(response.data);
} catch (e) {
setDoFetch(false);
setError(e);
setError(e as Error);
}
}

Expand All @@ -45,14 +45,14 @@ export const useUser = (options: GetUUIDMetadataParameters = {}): [UserEntity, E

useEffect(() => {
const listener = {
objects: (event) => {
objects: (event: ObjectsEvent) => {
const message = event.message;
if (message.type !== "uuid") return;

setUser((user) => {
const userCopy = cloneDeep(user);

if (message.data.id == user.id) {
if (message.data.id == userCopy?.id) {
Object.assign(userCopy, message.data);
}

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

return [user, error];
return [user, error as Error];
};
12 changes: 6 additions & 6 deletions packages/common/src/hooks/use-users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect } from "react";
import { GetAllMetadataParameters } from "pubnub";
import { GetAllMetadataParameters, ObjectsEvent } from "pubnub";
import { usePubNub } from "pubnub-react";
import { merge, cloneDeep } from "lodash";
import { UserEntity } from "../types";
Expand Down Expand Up @@ -35,11 +35,11 @@ export const useUsers = (options: GetAllMetadataParameters = {}): HookReturnValu
if (ignoreRequest) return;
setDoFetch(false);
setUsers((users) => [...users, ...response.data]);
setTotalCount(response.totalCount);
setPage(response.next);
setTotalCount(response.totalCount || 0);
setPage(response.next || "");
} catch (e) {
setDoFetch(false);
setError(e);
setError(e as Error);
}
}

Expand All @@ -50,7 +50,7 @@ export const useUsers = (options: GetAllMetadataParameters = {}): HookReturnValu

useEffect(() => {
const listener = {
objects: (event) => {
objects: (event: ObjectsEvent) => {
const message = event.message;
if (message.type !== "uuid") return;

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 as Error];
};
4 changes: 2 additions & 2 deletions packages/common/src/member-list/member-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const useMemberListCore = (props: CommonMemberListProps) => {
return props.presentMembers?.includes(uuid);
};

const memberSorter = (a, b) => {
const memberSorter = (a: UserEntity, b: UserEntity) => {
if (props.sort) return props.sort(a, b);

if (isOwnMember(a.id)) return -1;
Expand All @@ -55,7 +55,7 @@ export const useMemberListCore = (props: CommonMemberListProps) => {
if (isPresentMember(a.id) && !isPresentMember(b.id)) return -1;
if (isPresentMember(b.id) && !isPresentMember(a.id)) return 1;

return a.name.localeCompare(b.name, "en", { sensitivity: "base" });
return a.name?.localeCompare(b.name as string, "en", { sensitivity: "base" });
};

const memberFromString = (member: UserEntity | string) => {
Expand Down
Loading