@clerk/shared
Advanced tools
Comparing version 0.22.0-staging.4ea30e8 to 0.22.0-staging.5ea4146
@@ -36,8 +36,82 @@ "use strict"; | ||
var import_contexts = require("./contexts"); | ||
var import_usePagesOrInfinite = require("./usePagesOrInfinite"); | ||
const undefinedPaginatedResource = { | ||
data: void 0, | ||
count: void 0, | ||
isLoading: false, | ||
isFetching: false, | ||
isError: false, | ||
page: void 0, | ||
pageCount: void 0, | ||
fetchPage: void 0, | ||
fetchNext: void 0, | ||
fetchPrevious: void 0, | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}; | ||
const useOrganization = (params) => { | ||
const { invitationList: invitationListParams, membershipList: membershipListParams } = params || {}; | ||
const { | ||
invitationList: invitationListParams, | ||
membershipList: membershipListParams, | ||
domains: domainListParams, | ||
membershipRequests: membershipRequestsListParams | ||
} = params || {}; | ||
const { organization, lastOrganizationMember, lastOrganizationInvitation } = (0, import_contexts.useOrganizationContext)(); | ||
const session = (0, import_contexts.useSessionContext)(); | ||
const domainSafeValues = (0, import_usePagesOrInfinite.useWithSafeValues)(domainListParams, { | ||
initialPage: 1, | ||
pageSize: 10, | ||
keepPreviousData: false, | ||
infinite: false, | ||
enrollmentMode: void 0 | ||
}); | ||
const membershipRequestSafeValues = (0, import_usePagesOrInfinite.useWithSafeValues)(membershipRequestsListParams, { | ||
initialPage: 1, | ||
pageSize: 10, | ||
status: "pending", | ||
keepPreviousData: false, | ||
infinite: false | ||
}); | ||
const clerk = (0, import_contexts.useClerkInstanceContext)(); | ||
const shouldFetch = clerk.loaded && session && organization; | ||
const shouldFetch = !!(clerk.loaded && session && organization); | ||
const domainParams = typeof domainListParams === "undefined" ? void 0 : { | ||
initialPage: domainSafeValues.initialPage, | ||
pageSize: domainSafeValues.pageSize, | ||
enrollmentMode: domainSafeValues.enrollmentMode | ||
}; | ||
const membershipRequestParams = typeof membershipRequestsListParams === "undefined" ? void 0 : { | ||
initialPage: membershipRequestSafeValues.initialPage, | ||
pageSize: membershipRequestSafeValues.pageSize, | ||
status: membershipRequestSafeValues.status | ||
}; | ||
const domains = (0, import_usePagesOrInfinite.usePagesOrInfinite)( | ||
{ | ||
...domainParams | ||
}, | ||
organization?.getDomains, | ||
{ | ||
keepPreviousData: domainSafeValues.keepPreviousData, | ||
infinite: domainSafeValues.infinite, | ||
enabled: !!domainParams | ||
}, | ||
{ | ||
type: "domains", | ||
organizationId: organization?.id | ||
} | ||
); | ||
const membershipRequests = (0, import_usePagesOrInfinite.usePagesOrInfinite)( | ||
{ | ||
...membershipRequestParams | ||
}, | ||
organization?.getMembershipRequests, | ||
{ | ||
keepPreviousData: membershipRequestSafeValues.keepPreviousData, | ||
infinite: membershipRequestSafeValues.infinite, | ||
enabled: !!membershipRequestParams | ||
}, | ||
{ | ||
type: "membershipRequests", | ||
organizationId: organization?.id | ||
} | ||
); | ||
const pendingInvitations = !clerk.loaded ? () => [] : () => clerk.organization?.getPendingInvitations(invitationListParams); | ||
@@ -67,3 +141,5 @@ const currentOrganizationMemberships = !clerk.loaded ? () => [] : () => clerk.organization?.getMemberships(membershipListParams); | ||
membershipList: void 0, | ||
membership: void 0 | ||
membership: void 0, | ||
domains: undefinedPaginatedResource, | ||
membershipRequests: undefinedPaginatedResource | ||
}; | ||
@@ -77,3 +153,5 @@ } | ||
membershipList: null, | ||
membership: null | ||
membership: null, | ||
domains: null, | ||
membershipRequests: null | ||
}; | ||
@@ -87,3 +165,5 @@ } | ||
membershipList: void 0, | ||
membership: void 0 | ||
membership: void 0, | ||
domains: undefinedPaginatedResource, | ||
membershipRequests: undefinedPaginatedResource | ||
}; | ||
@@ -101,3 +181,5 @@ } | ||
void mutateInvitationList(); | ||
} | ||
}, | ||
domains, | ||
membershipRequests | ||
}; | ||
@@ -104,0 +186,0 @@ }; |
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
@@ -20,10 +18,2 @@ var __export = (target, all) => { | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
@@ -35,103 +25,64 @@ var useOrganizationList_exports = {}; | ||
module.exports = __toCommonJS(useOrganizationList_exports); | ||
var import_react = require("react"); | ||
var import_swr = __toESM(require("swr")); | ||
var import_infinite = __toESM(require("swr/infinite")); | ||
var import_contexts = require("./contexts"); | ||
var import_usePagesOrInfinite = require("./usePagesOrInfinite"); | ||
const useOrganizationList = (params) => { | ||
const { userInvitations } = params || {}; | ||
const shouldUseDefaultOptions = userInvitations === true; | ||
const [paginatedPage, setPaginatedPage] = (0, import_react.useState)(shouldUseDefaultOptions ? 1 : userInvitations?.initialPage ?? 1); | ||
const initialPageRef = (0, import_react.useRef)(shouldUseDefaultOptions ? 1 : userInvitations?.initialPage ?? 1); | ||
const pageSizeRef = (0, import_react.useRef)(shouldUseDefaultOptions ? 10 : userInvitations?.pageSize ?? 10); | ||
const triggerInfinite = shouldUseDefaultOptions ? false : !!userInvitations?.infinite; | ||
const internalKeepPreviousData = shouldUseDefaultOptions ? false : !!userInvitations?.keepPreviousData; | ||
const internalStatus = shouldUseDefaultOptions ? "pending" : userInvitations?.status ?? "pending"; | ||
const { userInvitations, userSuggestions } = params || {}; | ||
const userInvitationsSafeValues = (0, import_usePagesOrInfinite.useWithSafeValues)(userInvitations, { | ||
initialPage: 1, | ||
pageSize: 10, | ||
status: "pending", | ||
keepPreviousData: false, | ||
infinite: false | ||
}); | ||
const userSuggestionsSafeValues = (0, import_usePagesOrInfinite.useWithSafeValues)(userSuggestions, { | ||
initialPage: 1, | ||
pageSize: 10, | ||
status: "pending", | ||
keepPreviousData: false, | ||
infinite: false | ||
}); | ||
const clerk = (0, import_contexts.useClerkInstanceContext)(); | ||
const user = (0, import_contexts.useUserContext)(); | ||
const paginatedParams = typeof userInvitations === "undefined" ? void 0 : { | ||
initialPage: paginatedPage, | ||
pageSize: pageSizeRef.current, | ||
status: internalStatus | ||
const userInvitationsParams = typeof userInvitations === "undefined" ? void 0 : { | ||
initialPage: userInvitationsSafeValues.initialPage, | ||
pageSize: userInvitationsSafeValues.pageSize, | ||
status: userInvitationsSafeValues.status | ||
}; | ||
const canFetch = !!(clerk.loaded && user); | ||
const fetchInvitations = () => user.getOrganizationInvitations(paginatedParams); | ||
const { | ||
data: userInvitationsData, | ||
isValidating: userInvitationsValidating, | ||
isLoading: userInvitationsLoading, | ||
error: userInvitationsError, | ||
mutate: userInvitationsMutate | ||
} = (0, import_swr.default)( | ||
!triggerInfinite && canFetch && paginatedParams ? cacheKey("userInvitations", user, paginatedParams) : null, | ||
fetchInvitations, | ||
{ keepPreviousData: internalKeepPreviousData } | ||
); | ||
const getInfiniteKey = (pageIndex, previousPageData) => { | ||
if (!canFetch || !paginatedParams || !triggerInfinite) { | ||
return null; | ||
} | ||
return cacheKey("userInvitations", user, { | ||
initialPage: initialPageRef.current + pageIndex, | ||
pageSize: pageSizeRef.current, | ||
status: internalStatus | ||
}); | ||
const userSuggestionsParams = typeof userSuggestions === "undefined" ? void 0 : { | ||
initialPage: userSuggestionsSafeValues.initialPage, | ||
pageSize: userSuggestionsSafeValues.pageSize, | ||
status: userSuggestionsSafeValues.status | ||
}; | ||
const { | ||
data: userInvitationsDataInfinite, | ||
isLoading: userInvitationsLoadingInfinite, | ||
isValidating: userInvitationsInfiniteValidating, | ||
error: userInvitationsInfiniteError, | ||
size, | ||
setSize, | ||
mutate: userInvitationsInfiniteMutate | ||
} = (0, import_infinite.default)(getInfiniteKey, ({ initialPage, pageSize, status }) => { | ||
return user.getOrganizationInvitations({ | ||
initialPage, | ||
pageSize, | ||
status | ||
}); | ||
}); | ||
const isomorphicPage = (0, import_react.useMemo)(() => { | ||
if (triggerInfinite) { | ||
return size; | ||
const isClerkLoaded = !!(clerk.loaded && user); | ||
const invitations = (0, import_usePagesOrInfinite.usePagesOrInfinite)( | ||
{ | ||
...userInvitationsParams | ||
}, | ||
user?.getOrganizationInvitations, | ||
{ | ||
keepPreviousData: userInvitationsSafeValues.keepPreviousData, | ||
infinite: userInvitationsSafeValues.infinite, | ||
enabled: !!userInvitationsParams | ||
}, | ||
{ | ||
type: "userInvitations", | ||
userId: user?.id | ||
} | ||
return paginatedPage; | ||
}, [triggerInfinite, size, paginatedPage]); | ||
const isomorphicSetPage = (0, import_react.useCallback)( | ||
(numberOrgFn) => { | ||
if (triggerInfinite) { | ||
void setSize(numberOrgFn); | ||
return; | ||
} | ||
return setPaginatedPage(numberOrgFn); | ||
); | ||
const suggestions = (0, import_usePagesOrInfinite.usePagesOrInfinite)( | ||
{ | ||
...userSuggestionsParams | ||
}, | ||
[setSize] | ||
user?.getOrganizationSuggestions, | ||
{ | ||
keepPreviousData: userSuggestionsSafeValues.keepPreviousData, | ||
infinite: userSuggestionsSafeValues.infinite, | ||
enabled: !!userSuggestionsParams | ||
}, | ||
{ | ||
type: "userSuggestions", | ||
userId: user?.id | ||
} | ||
); | ||
const isomorphicData = (0, import_react.useMemo)(() => { | ||
if (triggerInfinite) { | ||
return userInvitationsDataInfinite?.map((a) => a?.data).flat() ?? []; | ||
} | ||
return userInvitationsData?.data ?? []; | ||
}, [triggerInfinite, userInvitationsDataInfinite, userInvitationsData]); | ||
const isomorphicCount = (0, import_react.useMemo)(() => { | ||
if (triggerInfinite) { | ||
return userInvitationsDataInfinite?.[userInvitationsDataInfinite?.length - 1]?.total_count || 0; | ||
} | ||
return userInvitationsData?.total_count ?? 0; | ||
}, [triggerInfinite, userInvitationsDataInfinite, userInvitationsData]); | ||
const isomorphicIsLoading = triggerInfinite ? userInvitationsLoadingInfinite : userInvitationsLoading; | ||
const isomorphicIsFetching = triggerInfinite ? userInvitationsInfiniteValidating : userInvitationsValidating; | ||
const isomorphicIsError = !!(triggerInfinite ? userInvitationsInfiniteError : userInvitationsError); | ||
const fetchNext = (0, import_react.useCallback)(() => { | ||
isomorphicSetPage((n) => Math.max(0, n + 1)); | ||
}, [isomorphicSetPage]); | ||
const fetchPrevious = (0, import_react.useCallback)(() => { | ||
isomorphicSetPage((n) => Math.max(0, n - 1)); | ||
}, [isomorphicSetPage]); | ||
const offsetCount = (initialPageRef.current - 1) * pageSizeRef.current; | ||
const pageCount = Math.ceil((isomorphicCount - offsetCount) / pageSizeRef.current); | ||
const hasNextPage = isomorphicCount - offsetCount * pageSizeRef.current > isomorphicPage * pageSizeRef.current; | ||
const hasPreviousPage = (isomorphicPage - 1) * pageSizeRef.current > offsetCount * pageSizeRef.current; | ||
const unstable__mutate = triggerInfinite ? userInvitationsInfiniteMutate : userInvitationsMutate; | ||
if (!clerk.loaded || !user) { | ||
if (!isClerkLoaded) { | ||
return { | ||
@@ -156,2 +107,17 @@ isLoaded: false, | ||
unstable__mutate: void 0 | ||
}, | ||
userSuggestions: { | ||
data: void 0, | ||
count: void 0, | ||
isLoading: false, | ||
isFetching: false, | ||
isError: false, | ||
page: void 0, | ||
pageCount: void 0, | ||
fetchPage: void 0, | ||
fetchNext: void 0, | ||
fetchPrevious: void 0, | ||
hasNextPage: false, | ||
hasPreviousPage: false, | ||
unstable__mutate: void 0 | ||
} | ||
@@ -161,21 +127,8 @@ }; | ||
return { | ||
isLoaded: canFetch, | ||
isLoaded: isClerkLoaded, | ||
organizationList: createOrganizationList(user.organizationMemberships), | ||
setActive: clerk.setActive, | ||
createOrganization: clerk.createOrganization, | ||
userInvitations: { | ||
data: isomorphicData, | ||
count: isomorphicCount, | ||
isLoading: isomorphicIsLoading, | ||
isFetching: isomorphicIsFetching, | ||
isError: isomorphicIsError, | ||
page: isomorphicPage, | ||
pageCount, | ||
fetchPage: isomorphicSetPage, | ||
fetchNext, | ||
fetchPrevious, | ||
hasNextPage, | ||
hasPreviousPage, | ||
unstable__mutate | ||
} | ||
userInvitations: invitations, | ||
userSuggestions: suggestions | ||
}; | ||
@@ -189,11 +142,2 @@ }; | ||
} | ||
function cacheKey(type, user, pagination) { | ||
return { | ||
type, | ||
userId: user.id, | ||
initialPage: pagination.initialPage, | ||
pageSize: pagination.pageSize, | ||
status: pagination.status | ||
}; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -200,0 +144,0 @@ 0 && (module.exports = { |
import useSWR from "swr"; | ||
import { useClerkInstanceContext, useOrganizationContext, useSessionContext } from "./contexts"; | ||
import { usePagesOrInfinite, useWithSafeValues } from "./usePagesOrInfinite"; | ||
const undefinedPaginatedResource = { | ||
data: void 0, | ||
count: void 0, | ||
isLoading: false, | ||
isFetching: false, | ||
isError: false, | ||
page: void 0, | ||
pageCount: void 0, | ||
fetchPage: void 0, | ||
fetchNext: void 0, | ||
fetchPrevious: void 0, | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}; | ||
const useOrganization = (params) => { | ||
const { invitationList: invitationListParams, membershipList: membershipListParams } = params || {}; | ||
const { | ||
invitationList: invitationListParams, | ||
membershipList: membershipListParams, | ||
domains: domainListParams, | ||
membershipRequests: membershipRequestsListParams | ||
} = params || {}; | ||
const { organization, lastOrganizationMember, lastOrganizationInvitation } = useOrganizationContext(); | ||
const session = useSessionContext(); | ||
const domainSafeValues = useWithSafeValues(domainListParams, { | ||
initialPage: 1, | ||
pageSize: 10, | ||
keepPreviousData: false, | ||
infinite: false, | ||
enrollmentMode: void 0 | ||
}); | ||
const membershipRequestSafeValues = useWithSafeValues(membershipRequestsListParams, { | ||
initialPage: 1, | ||
pageSize: 10, | ||
status: "pending", | ||
keepPreviousData: false, | ||
infinite: false | ||
}); | ||
const clerk = useClerkInstanceContext(); | ||
const shouldFetch = clerk.loaded && session && organization; | ||
const shouldFetch = !!(clerk.loaded && session && organization); | ||
const domainParams = typeof domainListParams === "undefined" ? void 0 : { | ||
initialPage: domainSafeValues.initialPage, | ||
pageSize: domainSafeValues.pageSize, | ||
enrollmentMode: domainSafeValues.enrollmentMode | ||
}; | ||
const membershipRequestParams = typeof membershipRequestsListParams === "undefined" ? void 0 : { | ||
initialPage: membershipRequestSafeValues.initialPage, | ||
pageSize: membershipRequestSafeValues.pageSize, | ||
status: membershipRequestSafeValues.status | ||
}; | ||
const domains = usePagesOrInfinite( | ||
{ | ||
...domainParams | ||
}, | ||
organization?.getDomains, | ||
{ | ||
keepPreviousData: domainSafeValues.keepPreviousData, | ||
infinite: domainSafeValues.infinite, | ||
enabled: !!domainParams | ||
}, | ||
{ | ||
type: "domains", | ||
organizationId: organization?.id | ||
} | ||
); | ||
const membershipRequests = usePagesOrInfinite( | ||
{ | ||
...membershipRequestParams | ||
}, | ||
organization?.getMembershipRequests, | ||
{ | ||
keepPreviousData: membershipRequestSafeValues.keepPreviousData, | ||
infinite: membershipRequestSafeValues.infinite, | ||
enabled: !!membershipRequestParams | ||
}, | ||
{ | ||
type: "membershipRequests", | ||
organizationId: organization?.id | ||
} | ||
); | ||
const pendingInvitations = !clerk.loaded ? () => [] : () => clerk.organization?.getPendingInvitations(invitationListParams); | ||
@@ -33,3 +107,5 @@ const currentOrganizationMemberships = !clerk.loaded ? () => [] : () => clerk.organization?.getMemberships(membershipListParams); | ||
membershipList: void 0, | ||
membership: void 0 | ||
membership: void 0, | ||
domains: undefinedPaginatedResource, | ||
membershipRequests: undefinedPaginatedResource | ||
}; | ||
@@ -43,3 +119,5 @@ } | ||
membershipList: null, | ||
membership: null | ||
membership: null, | ||
domains: null, | ||
membershipRequests: null | ||
}; | ||
@@ -53,3 +131,5 @@ } | ||
membershipList: void 0, | ||
membership: void 0 | ||
membership: void 0, | ||
domains: undefinedPaginatedResource, | ||
membershipRequests: undefinedPaginatedResource | ||
}; | ||
@@ -67,3 +147,5 @@ } | ||
void mutateInvitationList(); | ||
} | ||
}, | ||
domains, | ||
membershipRequests | ||
}; | ||
@@ -70,0 +152,0 @@ }; |
@@ -1,102 +0,63 @@ | ||
import { useCallback, useMemo, useRef, useState } from "react"; | ||
import useSWR from "swr"; | ||
import useSWRInfinite from "swr/infinite"; | ||
import { useClerkInstanceContext, useUserContext } from "./contexts"; | ||
import { usePagesOrInfinite, useWithSafeValues } from "./usePagesOrInfinite"; | ||
const useOrganizationList = (params) => { | ||
const { userInvitations } = params || {}; | ||
const shouldUseDefaultOptions = userInvitations === true; | ||
const [paginatedPage, setPaginatedPage] = useState(shouldUseDefaultOptions ? 1 : userInvitations?.initialPage ?? 1); | ||
const initialPageRef = useRef(shouldUseDefaultOptions ? 1 : userInvitations?.initialPage ?? 1); | ||
const pageSizeRef = useRef(shouldUseDefaultOptions ? 10 : userInvitations?.pageSize ?? 10); | ||
const triggerInfinite = shouldUseDefaultOptions ? false : !!userInvitations?.infinite; | ||
const internalKeepPreviousData = shouldUseDefaultOptions ? false : !!userInvitations?.keepPreviousData; | ||
const internalStatus = shouldUseDefaultOptions ? "pending" : userInvitations?.status ?? "pending"; | ||
const { userInvitations, userSuggestions } = params || {}; | ||
const userInvitationsSafeValues = useWithSafeValues(userInvitations, { | ||
initialPage: 1, | ||
pageSize: 10, | ||
status: "pending", | ||
keepPreviousData: false, | ||
infinite: false | ||
}); | ||
const userSuggestionsSafeValues = useWithSafeValues(userSuggestions, { | ||
initialPage: 1, | ||
pageSize: 10, | ||
status: "pending", | ||
keepPreviousData: false, | ||
infinite: false | ||
}); | ||
const clerk = useClerkInstanceContext(); | ||
const user = useUserContext(); | ||
const paginatedParams = typeof userInvitations === "undefined" ? void 0 : { | ||
initialPage: paginatedPage, | ||
pageSize: pageSizeRef.current, | ||
status: internalStatus | ||
const userInvitationsParams = typeof userInvitations === "undefined" ? void 0 : { | ||
initialPage: userInvitationsSafeValues.initialPage, | ||
pageSize: userInvitationsSafeValues.pageSize, | ||
status: userInvitationsSafeValues.status | ||
}; | ||
const canFetch = !!(clerk.loaded && user); | ||
const fetchInvitations = () => user.getOrganizationInvitations(paginatedParams); | ||
const { | ||
data: userInvitationsData, | ||
isValidating: userInvitationsValidating, | ||
isLoading: userInvitationsLoading, | ||
error: userInvitationsError, | ||
mutate: userInvitationsMutate | ||
} = useSWR( | ||
!triggerInfinite && canFetch && paginatedParams ? cacheKey("userInvitations", user, paginatedParams) : null, | ||
fetchInvitations, | ||
{ keepPreviousData: internalKeepPreviousData } | ||
); | ||
const getInfiniteKey = (pageIndex, previousPageData) => { | ||
if (!canFetch || !paginatedParams || !triggerInfinite) { | ||
return null; | ||
} | ||
return cacheKey("userInvitations", user, { | ||
initialPage: initialPageRef.current + pageIndex, | ||
pageSize: pageSizeRef.current, | ||
status: internalStatus | ||
}); | ||
const userSuggestionsParams = typeof userSuggestions === "undefined" ? void 0 : { | ||
initialPage: userSuggestionsSafeValues.initialPage, | ||
pageSize: userSuggestionsSafeValues.pageSize, | ||
status: userSuggestionsSafeValues.status | ||
}; | ||
const { | ||
data: userInvitationsDataInfinite, | ||
isLoading: userInvitationsLoadingInfinite, | ||
isValidating: userInvitationsInfiniteValidating, | ||
error: userInvitationsInfiniteError, | ||
size, | ||
setSize, | ||
mutate: userInvitationsInfiniteMutate | ||
} = useSWRInfinite(getInfiniteKey, ({ initialPage, pageSize, status }) => { | ||
return user.getOrganizationInvitations({ | ||
initialPage, | ||
pageSize, | ||
status | ||
}); | ||
}); | ||
const isomorphicPage = useMemo(() => { | ||
if (triggerInfinite) { | ||
return size; | ||
const isClerkLoaded = !!(clerk.loaded && user); | ||
const invitations = usePagesOrInfinite( | ||
{ | ||
...userInvitationsParams | ||
}, | ||
user?.getOrganizationInvitations, | ||
{ | ||
keepPreviousData: userInvitationsSafeValues.keepPreviousData, | ||
infinite: userInvitationsSafeValues.infinite, | ||
enabled: !!userInvitationsParams | ||
}, | ||
{ | ||
type: "userInvitations", | ||
userId: user?.id | ||
} | ||
return paginatedPage; | ||
}, [triggerInfinite, size, paginatedPage]); | ||
const isomorphicSetPage = useCallback( | ||
(numberOrgFn) => { | ||
if (triggerInfinite) { | ||
void setSize(numberOrgFn); | ||
return; | ||
} | ||
return setPaginatedPage(numberOrgFn); | ||
); | ||
const suggestions = usePagesOrInfinite( | ||
{ | ||
...userSuggestionsParams | ||
}, | ||
[setSize] | ||
user?.getOrganizationSuggestions, | ||
{ | ||
keepPreviousData: userSuggestionsSafeValues.keepPreviousData, | ||
infinite: userSuggestionsSafeValues.infinite, | ||
enabled: !!userSuggestionsParams | ||
}, | ||
{ | ||
type: "userSuggestions", | ||
userId: user?.id | ||
} | ||
); | ||
const isomorphicData = useMemo(() => { | ||
if (triggerInfinite) { | ||
return userInvitationsDataInfinite?.map((a) => a?.data).flat() ?? []; | ||
} | ||
return userInvitationsData?.data ?? []; | ||
}, [triggerInfinite, userInvitationsDataInfinite, userInvitationsData]); | ||
const isomorphicCount = useMemo(() => { | ||
if (triggerInfinite) { | ||
return userInvitationsDataInfinite?.[userInvitationsDataInfinite?.length - 1]?.total_count || 0; | ||
} | ||
return userInvitationsData?.total_count ?? 0; | ||
}, [triggerInfinite, userInvitationsDataInfinite, userInvitationsData]); | ||
const isomorphicIsLoading = triggerInfinite ? userInvitationsLoadingInfinite : userInvitationsLoading; | ||
const isomorphicIsFetching = triggerInfinite ? userInvitationsInfiniteValidating : userInvitationsValidating; | ||
const isomorphicIsError = !!(triggerInfinite ? userInvitationsInfiniteError : userInvitationsError); | ||
const fetchNext = useCallback(() => { | ||
isomorphicSetPage((n) => Math.max(0, n + 1)); | ||
}, [isomorphicSetPage]); | ||
const fetchPrevious = useCallback(() => { | ||
isomorphicSetPage((n) => Math.max(0, n - 1)); | ||
}, [isomorphicSetPage]); | ||
const offsetCount = (initialPageRef.current - 1) * pageSizeRef.current; | ||
const pageCount = Math.ceil((isomorphicCount - offsetCount) / pageSizeRef.current); | ||
const hasNextPage = isomorphicCount - offsetCount * pageSizeRef.current > isomorphicPage * pageSizeRef.current; | ||
const hasPreviousPage = (isomorphicPage - 1) * pageSizeRef.current > offsetCount * pageSizeRef.current; | ||
const unstable__mutate = triggerInfinite ? userInvitationsInfiniteMutate : userInvitationsMutate; | ||
if (!clerk.loaded || !user) { | ||
if (!isClerkLoaded) { | ||
return { | ||
@@ -121,2 +82,17 @@ isLoaded: false, | ||
unstable__mutate: void 0 | ||
}, | ||
userSuggestions: { | ||
data: void 0, | ||
count: void 0, | ||
isLoading: false, | ||
isFetching: false, | ||
isError: false, | ||
page: void 0, | ||
pageCount: void 0, | ||
fetchPage: void 0, | ||
fetchNext: void 0, | ||
fetchPrevious: void 0, | ||
hasNextPage: false, | ||
hasPreviousPage: false, | ||
unstable__mutate: void 0 | ||
} | ||
@@ -126,21 +102,8 @@ }; | ||
return { | ||
isLoaded: canFetch, | ||
isLoaded: isClerkLoaded, | ||
organizationList: createOrganizationList(user.organizationMemberships), | ||
setActive: clerk.setActive, | ||
createOrganization: clerk.createOrganization, | ||
userInvitations: { | ||
data: isomorphicData, | ||
count: isomorphicCount, | ||
isLoading: isomorphicIsLoading, | ||
isFetching: isomorphicIsFetching, | ||
isError: isomorphicIsError, | ||
page: isomorphicPage, | ||
pageCount, | ||
fetchPage: isomorphicSetPage, | ||
fetchNext, | ||
fetchPrevious, | ||
hasNextPage, | ||
hasPreviousPage, | ||
unstable__mutate | ||
} | ||
userInvitations: invitations, | ||
userSuggestions: suggestions | ||
}; | ||
@@ -154,11 +117,2 @@ }; | ||
} | ||
function cacheKey(type, user, pagination) { | ||
return { | ||
type, | ||
userId: user.id, | ||
initialPage: pagination.initialPage, | ||
pageSize: pagination.pageSize, | ||
status: pagination.status | ||
}; | ||
} | ||
export { | ||
@@ -165,0 +119,0 @@ useOrganizationList |
@@ -1,5 +0,14 @@ | ||
import type { GetMembershipsParams, GetPendingInvitationsParams, OrganizationInvitationResource, OrganizationMembershipResource, OrganizationResource } from '@clerk/types'; | ||
import type { GetDomainsParams, GetMembershipRequestParams, GetMembershipsParams, GetPendingInvitationsParams, OrganizationDomainResource, OrganizationInvitationResource, OrganizationMembershipRequestResource, OrganizationMembershipResource, OrganizationResource } from '@clerk/types'; | ||
import type { PaginatedResources, PaginatedResourcesWithDefault } from './types'; | ||
type UseOrganizationParams = { | ||
invitationList?: GetPendingInvitationsParams; | ||
membershipList?: GetMembershipsParams; | ||
domains?: true | (GetDomainsParams & { | ||
infinite?: boolean; | ||
keepPreviousData?: boolean; | ||
}); | ||
membershipRequests?: true | (GetMembershipRequestParams & { | ||
infinite?: boolean; | ||
keepPreviousData?: boolean; | ||
}); | ||
}; | ||
@@ -12,2 +21,4 @@ type UseOrganizationReturn = { | ||
membership: undefined; | ||
domains: PaginatedResourcesWithDefault<OrganizationDomainResource>; | ||
membershipRequests: PaginatedResourcesWithDefault<OrganizationMembershipRequestResource>; | ||
} | { | ||
@@ -19,2 +30,4 @@ isLoaded: true; | ||
membership: undefined; | ||
domains: PaginatedResourcesWithDefault<OrganizationDomainResource>; | ||
membershipRequests: PaginatedResourcesWithDefault<OrganizationMembershipRequestResource>; | ||
} | { | ||
@@ -26,2 +39,4 @@ isLoaded: boolean; | ||
membership: OrganizationMembershipResource | null | undefined; | ||
domains: PaginatedResources<OrganizationDomainResource> | null; | ||
membershipRequests: PaginatedResources<OrganizationMembershipRequestResource> | null; | ||
}; | ||
@@ -28,0 +43,0 @@ type UseOrganization = (params?: UseOrganizationParams) => UseOrganizationReturn; |
@@ -1,2 +0,2 @@ | ||
import type { CreateOrganizationParams, GetUserOrganizationInvitationsParams, OrganizationMembershipResource, OrganizationResource, SetActive, UserOrganizationInvitationResource } from '@clerk/types'; | ||
import type { CreateOrganizationParams, GetUserOrganizationInvitationsParams, GetUserOrganizationSuggestionsParams, OrganizationMembershipResource, OrganizationResource, OrganizationSuggestionResource, SetActive, UserOrganizationInvitationResource } from '@clerk/types'; | ||
import type { PaginatedResources, PaginatedResourcesWithDefault } from './types'; | ||
@@ -8,2 +8,6 @@ type UseOrganizationListParams = { | ||
}); | ||
userSuggestions?: true | (GetUserOrganizationSuggestionsParams & { | ||
infinite?: boolean; | ||
keepPreviousData?: boolean; | ||
}); | ||
}; | ||
@@ -17,2 +21,3 @@ type OrganizationList = ReturnType<typeof createOrganizationList>; | ||
userInvitations: PaginatedResourcesWithDefault<UserOrganizationInvitationResource>; | ||
userSuggestions: PaginatedResourcesWithDefault<OrganizationSuggestionResource>; | ||
} | { | ||
@@ -24,2 +29,3 @@ isLoaded: boolean; | ||
userInvitations: PaginatedResources<UserOrganizationInvitationResource>; | ||
userSuggestions: PaginatedResources<OrganizationSuggestionResource>; | ||
}; | ||
@@ -26,0 +32,0 @@ type UseOrganizationList = (params?: UseOrganizationListParams) => UseOrganizationListReturn; |
{ | ||
"name": "@clerk/shared", | ||
"version": "0.22.0-staging.4ea30e8", | ||
"version": "0.22.0-staging.5ea4146", | ||
"description": "Internal package utils used by the Clerk SDKs", | ||
@@ -38,3 +38,3 @@ "types": "./dist/types/index.d.ts", | ||
"devDependencies": { | ||
"@clerk/types": "3.50.0-staging.4ea30e8", | ||
"@clerk/types": "3.50.0-staging.5ea4146", | ||
"@types/glob-to-regexp": "0.4.1", | ||
@@ -41,0 +41,0 @@ "@types/js-cookie": "3.0.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
541724
310
6162