Skip to content

Commit 92c7faa

Browse files
committed
fixed user table update
1 parent de7ac12 commit 92c7faa

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

apps/dashboard/src/components/data-table/user-table.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ export function extendUsers(users: ServerUser[]): ExtendedServerUser[] {
184184

185185
export function UserTable() {
186186
const stackAdminApp = useAdminApp();
187-
const [users, setUsers] = useState<ExtendedServerUser[]>([]);
187+
const [filters, setFilters] = useState<Parameters<typeof stackAdminApp.listUsers>[0]>({ limit: 10, orderBy: "signedUpAt", desc: true });
188+
const users = extendUsers(stackAdminApp.useUsers(filters));
188189

189190
const onUpdate = async (options: {
190191
cursor: string,
@@ -193,7 +194,11 @@ export function UserTable() {
193194
columnFilters: ColumnFiltersState,
194195
globalFilters: any,
195196
}) => {
196-
let filters: any = {};
197+
let filters: Parameters<typeof stackAdminApp.listUsers>[0] = {
198+
cursor: options.cursor,
199+
limit: options.limit,
200+
query: options.globalFilters,
201+
};
197202

198203
const orderMap = {
199204
signedUpAt: "signedUpAt",
@@ -203,13 +208,8 @@ export function UserTable() {
203208
filters.desc = options.sorting[0].desc;
204209
}
205210

206-
const users = await stackAdminApp.listUsers({
207-
cursor: options.cursor,
208-
limit: options.limit,
209-
query: options.globalFilters,
210-
...filters,
211-
});
212-
setUsers(extendUsers(users));
211+
setFilters(filters);
212+
const users = await stackAdminApp.listUsers(filters);
213213
return { nextCursor: users.nextCursor };
214214
};
215215

packages/stack-shared/src/utils/caches.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ export class AsyncCache<D extends any[], T> {
6060
return cache;
6161
}
6262

63+
async refreshWhere(predicate: (dependencies: D) => boolean) {
64+
const promises: Promise<T>[] = [];
65+
for (const [dependencies, cache] of this._map) {
66+
if (predicate(dependencies)) {
67+
promises.push(cache.refresh());
68+
}
69+
}
70+
await Promise.all(promises);
71+
}
72+
6373
readonly isCacheAvailable = this._createKeyed("isCacheAvailable");
6474
readonly getIfCached = this._createKeyed("getIfCached");
6575
readonly getOrWait = this._createKeyed("getOrWait");

packages/stack/src/lib/stack-app.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ class _StackClientAppImpl<HasTokenStore extends boolean, ProjectId extends strin
314314
async (session, [providerId, scope, redirect]) => {
315315
return await this._getUserOAuthConnectionCacheFn({
316316
getUser: async () => await this._currentUserCache.getOrWait([session], "write-only"),
317-
getOrWaitOAuthToken: async () => await this._currentUserOAuthConnectionAccessTokensCache.getOrWait([session, providerId, scope || ""], "write-only"),
318-
useOAuthToken: () => useAsyncCache(this._currentUserOAuthConnectionAccessTokensCache, [session, providerId, scope || ""], "useOAuthToken"),
317+
getOrWaitOAuthToken: async () => await this._currentUserOAuthConnectionAccessTokensCache.getOrWait([session, providerId, scope || ""] as const, "write-only"),
318+
useOAuthToken: () => useAsyncCache(this._currentUserOAuthConnectionAccessTokensCache, [session, providerId, scope || ""] as const, "useOAuthToken"),
319319
providerId,
320320
scope,
321321
redirect,
@@ -748,7 +748,7 @@ class _StackClientAppImpl<HasTokenStore extends boolean, ProjectId extends strin
748748
return result.map((crud) => app._clientTeamUserFromCrud(crud));
749749
},
750750
useUsers() {
751-
const result = useAsyncCache(app._teamMemberProfilesCache, [app._getSession(), crud.id], "team.useUsers()");
751+
const result = useAsyncCache(app._teamMemberProfilesCache, [app._getSession(), crud.id] as const, "team.useUsers()");
752752
return result.map((crud) => app._clientTeamUserFromCrud(crud));
753753
},
754754
async update(data: TeamUpdateOptions){
@@ -902,7 +902,7 @@ class _StackClientAppImpl<HasTokenStore extends boolean, ProjectId extends strin
902902
function useConnectedAccount(id: ProviderType, options: { or: 'redirect', scopes?: string[] }): OAuthConnection;
903903
function useConnectedAccount(id: ProviderType, options?: { or?: 'redirect', scopes?: string[] }): OAuthConnection | null {
904904
const scopeString = options?.scopes?.join(" ");
905-
return useAsyncCache(app._currentUserOAuthConnectionCache, [session, id, scopeString || "", options?.or === 'redirect'], "user.useConnectedAccount()");
905+
return useAsyncCache(app._currentUserOAuthConnectionCache, [session, id, scopeString || "", options?.or === 'redirect'] as const, "user.useConnectedAccount()");
906906
}
907907

908908
return {
@@ -954,7 +954,7 @@ class _StackClientAppImpl<HasTokenStore extends boolean, ProjectId extends strin
954954
},
955955
usePermissions(scope: Team, options?: { recursive?: boolean }): TeamPermission[] {
956956
const recursive = options?.recursive ?? true;
957-
const permissions = useAsyncCache(app._currentUserPermissionsCache, [session, scope.id, recursive], "user.usePermissions()");
957+
const permissions = useAsyncCache(app._currentUserPermissionsCache, [session, scope.id, recursive] as const, "user.usePermissions()");
958958
return useMemo(() => permissions.map((crud) => app._clientTeamPermissionFromCrud(crud)), [permissions]);
959959
},
960960
usePermission(scope: Team, permissionId: string): TeamPermission | null {
@@ -992,7 +992,7 @@ class _StackClientAppImpl<HasTokenStore extends boolean, ProjectId extends strin
992992
return app._editableTeamProfileFromCrud(result);
993993
},
994994
useTeamProfile(team: Team) {
995-
const result = useAsyncCache(app._currentUserTeamProfileCache, [session, team.id], "user.useTeamProfile()");
995+
const result = useAsyncCache(app._currentUserTeamProfileCache, [session, team.id] as const, "user.useTeamProfile()");
996996
return app._editableTeamProfileFromCrud(result);
997997
},
998998
async delete() {
@@ -1004,7 +1004,7 @@ class _StackClientAppImpl<HasTokenStore extends boolean, ProjectId extends strin
10041004
return result.map((crud) => app._clientContactChannelFromCrud(crud));
10051005
},
10061006
useContactChannels() {
1007-
const result = useAsyncCache(app._clientContactChannelsCache, [session], "user.useContactChannels()");
1007+
const result = useAsyncCache(app._clientContactChannelsCache, [session] as const, "user.useContactChannels()");
10081008
return result.map((crud) => app._clientContactChannelFromCrud(crud));
10091009
},
10101010
async createContactChannel(data: ContactChannelCreateOptions) {
@@ -1623,8 +1623,8 @@ class _StackServerAppImpl<HasTokenStore extends boolean, ProjectId extends strin
16231623
async ([userId, providerId, scope, redirect]) => {
16241624
return await this._getUserOAuthConnectionCacheFn({
16251625
getUser: async () => await this._serverUserCache.getOrWait([userId], "write-only"),
1626-
getOrWaitOAuthToken: async () => await this._serverUserOAuthConnectionAccessTokensCache.getOrWait([userId, providerId, scope || ""], "write-only"),
1627-
useOAuthToken: () => useAsyncCache(this._serverUserOAuthConnectionAccessTokensCache, [userId, providerId, scope || ""], "user.useConnectedAccount()"),
1626+
getOrWaitOAuthToken: async () => await this._serverUserOAuthConnectionAccessTokensCache.getOrWait([userId, providerId, scope || ""] as const, "write-only"),
1627+
useOAuthToken: () => useAsyncCache(this._serverUserOAuthConnectionAccessTokensCache, [userId, providerId, scope || ""] as const, "user.useConnectedAccount()"),
16281628
providerId,
16291629
scope,
16301630
redirect,
@@ -1724,7 +1724,7 @@ class _StackServerAppImpl<HasTokenStore extends boolean, ProjectId extends strin
17241724
function useConnectedAccount(id: ProviderType, options: { or: 'redirect', scopes?: string[] }): OAuthConnection;
17251725
function useConnectedAccount(id: ProviderType, options?: { or?: 'redirect', scopes?: string[] }): OAuthConnection | null {
17261726
const scopeString = options?.scopes?.join(" ");
1727-
return useAsyncCache(app._serverUserOAuthConnectionCache, [crud.id, id, scopeString || "", options?.or === 'redirect'], "user.useConnectedAccount()");
1727+
return useAsyncCache(app._serverUserOAuthConnectionCache, [crud.id, id, scopeString || "", options?.or === 'redirect'] as const, "user.useConnectedAccount()");
17281728
}
17291729

17301730
return {
@@ -1813,7 +1813,7 @@ class _StackServerAppImpl<HasTokenStore extends boolean, ProjectId extends strin
18131813
},
18141814
usePermissions(scope: Team, options?: { recursive?: boolean }): AdminTeamPermission[] {
18151815
const recursive = options?.recursive ?? true;
1816-
const permissions = useAsyncCache(app._serverTeamUserPermissionsCache, [scope.id, crud.id, recursive], "user.usePermissions()");
1816+
const permissions = useAsyncCache(app._serverTeamUserPermissionsCache, [scope.id, crud.id, recursive] as const, "user.usePermissions()");
18171817
return useMemo(() => permissions.map((crud) => app._serverPermissionFromCrud(crud)), [permissions]);
18181818
},
18191819
async getPermission(scope: Team, permissionId: string): Promise<AdminTeamPermission | null> {
@@ -1848,15 +1848,15 @@ class _StackServerAppImpl<HasTokenStore extends boolean, ProjectId extends strin
18481848
return app._serverEditableTeamProfileFromCrud(result);
18491849
},
18501850
useTeamProfile(team: Team) {
1851-
const result = useAsyncCache(app._serverUserTeamProfileCache, [team.id, crud.id], "user.useTeamProfile()");
1851+
const result = useAsyncCache(app._serverUserTeamProfileCache, [team.id, crud.id] as const, "user.useTeamProfile()");
18521852
return useMemo(() => app._serverEditableTeamProfileFromCrud(result), [result]);
18531853
},
18541854
async listContactChannels() {
18551855
const result = await app._serverContactChannelsCache.getOrWait([crud.id], "write-only");
18561856
return result.map((data) => app._serverContactChannelFromCrud(crud.id, data));
18571857
},
18581858
useContactChannels() {
1859-
const result = useAsyncCache(app._serverContactChannelsCache, [crud.id], "user.useContactChannels()");
1859+
const result = useAsyncCache(app._serverContactChannelsCache, [crud.id] as const, "user.useContactChannels()");
18601860
return useMemo(() => result.map((data) => app._serverContactChannelFromCrud(crud.id, data)), [result]);
18611861
},
18621862
createContactChannel: async (data: ServerContactChannelCreateOptions) => {
@@ -1912,7 +1912,7 @@ class _StackServerAppImpl<HasTokenStore extends boolean, ProjectId extends strin
19121912
return result.map(u => app._serverTeamUserFromCrud(u));
19131913
},
19141914
useUsers() {
1915-
const result = useAsyncCache(app._serverTeamMemberProfilesCache, [crud.id], "team.useUsers()");
1915+
const result = useAsyncCache(app._serverTeamMemberProfilesCache, [crud.id] as const, "team.useUsers()");
19161916
return useMemo(() => result.map(u => app._serverTeamUserFromCrud(u)), [result]);
19171917
},
19181918
async addUser(userId) {
@@ -2042,7 +2042,7 @@ class _StackServerAppImpl<HasTokenStore extends boolean, ProjectId extends strin
20422042
}
20432043

20442044
useUsers(options?: ServerListUsersOptions): ServerUser[] & { nextCursor: string | null } {
2045-
const crud = useAsyncCache(this._serverUsersCache, [options?.cursor, options?.limit, options?.orderBy, options?.desc, options?.query], "useServerUsers()");
2045+
const crud = useAsyncCache(this._serverUsersCache, [options?.cursor, options?.limit, options?.orderBy, options?.desc, options?.query] as const, "useServerUsers()");
20462046
const result: any = crud.items.map((j) => this._serverUserFromCrud(j));
20472047
result.nextCursor = crud.pagination?.next_cursor ?? null;
20482048
return result as any;
@@ -2102,7 +2102,7 @@ class _StackServerAppImpl<HasTokenStore extends boolean, ProjectId extends strin
21022102
protected override async _refreshUsers() {
21032103
await Promise.all([
21042104
super._refreshUsers(),
2105-
this._serverUsersCache.refresh([]),
2105+
this._serverUsersCache.refreshWhere(() => true),
21062106
]);
21072107
}
21082108
}

0 commit comments

Comments
 (0)