Skip to content

Commit

Permalink
fix(shared): Remove unnecessary complexity in types
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Oct 29, 2023
1 parent dd62747 commit 1683fbc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 45 deletions.
31 changes: 8 additions & 23 deletions packages/shared/src/react/hooks/useOrganization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,19 @@ type UseOrganization = <T extends UseOrganizationParams>(
membership: OrganizationMembershipResource | null | undefined;
domains: PaginatedResources<
OrganizationDomainResource,
T['membershipRequests'] extends { infinite: true } ? true : false,
T['membershipRequests'] extends { infinite: true }
? ClerkPaginatedResponse<OrganizationDomainResource>
: OrganizationDomainResource[]
T['membershipRequests'] extends { infinite: true } ? true : false
> | null;
membershipRequests: PaginatedResources<
OrganizationMembershipRequestResource,
T['membershipRequests'] extends { infinite: true } ? true : false,
T['membershipRequests'] extends { infinite: true }
? ClerkPaginatedResponse<OrganizationMembershipRequestResource>
: OrganizationMembershipRequestResource[]
T['membershipRequests'] extends { infinite: true } ? true : false
> | null;
memberships: PaginatedResources<
OrganizationMembershipResource,
T['memberships'] extends { infinite: true } ? true : false,
T['memberships'] extends { infinite: true }
? ClerkPaginatedResponse<OrganizationMembershipResource>
: OrganizationMembershipResource[]
T['memberships'] extends { infinite: true } ? true : false
> | null;
invitations: PaginatedResources<
OrganizationInvitationResource,
T['invitations'] extends { infinite: true } ? true : false,
T['invitations'] extends { infinite: true }
? ClerkPaginatedResponse<OrganizationInvitationResource>
: OrganizationInvitationResource[]
T['invitations'] extends { infinite: true } ? true : false
> | null;
};

Expand Down Expand Up @@ -397,13 +385,10 @@ export const useOrganization: UseOrganization = params => {
void mutateMembershipList();
void mutateInvitationList();
},
// Let the hook return type define this type
domains: domains as any,
// Let the hook return type define this type
membershipRequests: membershipRequests as any,
// Let the hook return type define this type
memberships: memberships as any,
invitations: invitations as any,
domains,
membershipRequests,
memberships,
invitations,
};
};

Expand Down
24 changes: 6 additions & 18 deletions packages/shared/src/react/hooks/useOrganizationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,15 @@ type UseOrganizationList = <T extends UseOrganizationListParams>(
setActive: SetActive;
userMemberships: PaginatedResources<
OrganizationMembershipResource,
T['userMemberships'] extends { infinite: true } ? true : false,
T['userMemberships'] extends { infinite: true }
? ClerkPaginatedResponse<OrganizationMembershipResource>
: OrganizationMembershipResource[]
T['userMemberships'] extends { infinite: true } ? true : false
>;
userInvitations: PaginatedResources<
UserOrganizationInvitationResource,
T['userInvitations'] extends { infinite: true } ? true : false,
T['userInvitations'] extends { infinite: true }
? ClerkPaginatedResponse<UserOrganizationInvitationResource>
: UserOrganizationInvitationResource[]
T['userInvitations'] extends { infinite: true } ? true : false
>;
userSuggestions: PaginatedResources<
OrganizationSuggestionResource,
T['userSuggestions'] extends { infinite: true } ? true : false,
T['userSuggestions'] extends { infinite: true }
? ClerkPaginatedResponse<OrganizationSuggestionResource>
: OrganizationSuggestionResource[]
T['userSuggestions'] extends { infinite: true } ? true : false
>;
};

Expand Down Expand Up @@ -234,12 +225,9 @@ export const useOrganizationList: UseOrganizationList = params => {
organizationList: createOrganizationList(user.organizationMemberships),
setActive: clerk.setActive,
createOrganization: clerk.createOrganization,
// Let the hook return type define this type
userMemberships: memberships as any,
// Let the hook return type define this type
userInvitations: invitations as any,
// Let the hook return type define this type
userSuggestions: suggestions as any,
userMemberships: memberships,
userInvitations: invitations,
userSuggestions: suggestions,
};
deprecatedObjectProperty(result, 'organizationList', 'Use `userMemberships` instead.');

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/react/hooks/usePagesOrInfinite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type UsePagesOrInfinite = <
*/
options: TOptions,
cacheKeys: CacheKeys,
) => PaginatedResources<ExtractData<FetcherReturnData>, TOptions['infinite'], FetcherReturnData>;
) => PaginatedResources<ExtractData<FetcherReturnData>, TOptions['infinite']>;

export const usePagesOrInfinite: UsePagesOrInfinite = (params, fetcher, options, cacheKeys) => {
const [paginatedPage, setPaginatedPage] = useState(params.initialPage ?? 1);
Expand Down
8 changes: 5 additions & 3 deletions packages/shared/src/react/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { ClerkPaginatedResponse } from '@clerk/types';

export type ValueOrSetter<T = unknown> = (size: T | ((_size: T) => T)) => void;

export type CacheSetter<CData = any> = (
data?: CData | ((currentData?: CData) => Promise<undefined | CData> | undefined | CData),
) => Promise<CData | undefined>;

export type PaginatedResources<T = unknown, Infinite = false, ArrayOrPaginated = unknown> = {
export type PaginatedResources<T = unknown, Infinite = false> = {
data: T[];
count: number;
isLoading: boolean;
Expand All @@ -20,9 +22,9 @@ export type PaginatedResources<T = unknown, Infinite = false, ArrayOrPaginated =
revalidate: () => Promise<void>;
setData: Infinite extends true
? // Array of pages of data
CacheSetter<(ArrayOrPaginated | undefined)[]>
CacheSetter<(ClerkPaginatedResponse<T> | undefined)[]>
: // Array of data
CacheSetter<ArrayOrPaginated | undefined>;
CacheSetter<ClerkPaginatedResponse<T> | undefined>;
};

// Utility type to convert PaginatedDataAPI to properties as undefined, except booleans set to false
Expand Down

0 comments on commit 1683fbc

Please sign in to comment.