= ({
return (
+ {isConverting && }
{children}
@@ -123,23 +131,27 @@ const ImageUpload: React.FC = ({
type AvatarEditorDialogProps = {
file: File;
onUpload: (blob: Blob, file: File) => void;
- isUploading: boolean;
borderRadius: number;
submitText: string;
};
const AvatarEditorDialog: React.FC = observer(
- ({ file, onUpload, isUploading, borderRadius, submitText }) => {
+ ({ file, onUpload, borderRadius, submitText }) => {
const { ui } = useStores();
const { t } = useTranslation();
const [zoom, setZoom] = useState(1);
+ const [isUploading, setIsUploading] = useState(false);
const avatarEditorRef = useRef(null);
const handleUpload = React.useCallback(() => {
- const canvas = avatarEditorRef.current?.getImage();
- invariant(canvas, "canvas is not defined");
- const blob = dataUrlToBlob(canvas.toDataURL());
- onUpload(blob, file);
+ setIsUploading(true);
+ // Allow the button to render as disabled before the canvas is converted,
+ // which hangs rendering for large images.
+ setTimeout(() => {
+ const canvas = avatarEditorRef.current?.getImage();
+ invariant(canvas, "canvas is not defined");
+ onUpload(dataUrlToBlob(canvas.toDataURL()), file);
+ }, 0);
}, [file, onUpload]);
const handleZoom = React.useCallback(
diff --git a/app/scenes/TeamDelete.tsx b/app/scenes/TeamDelete.tsx
index 9e4195cb58..202586b7a2 100644
--- a/app/scenes/TeamDelete.tsx
+++ b/app/scenes/TeamDelete.tsx
@@ -102,11 +102,11 @@ function TeamDelete({ onSubmit }: Props) {
) : (
<>
-
- Deleting the {{ workspaceName }} workspace will
- destroy all collections, documents, users, and associated data.
- You will be immediately logged out of {{ appName }}.
-
+ }}
+ />
>
)}
diff --git a/app/stores/AuthStore.ts b/app/stores/AuthStore.ts
index e53886c897..7136702fcd 100644
--- a/app/stores/AuthStore.ts
+++ b/app/stores/AuthStore.ts
@@ -1,7 +1,14 @@
import * as Sentry from "@sentry/react";
import invariant from "invariant";
import { isNil } from "es-toolkit/compat";
-import { observable, action, computed, autorun, runInAction } from "mobx";
+import {
+ action,
+ autorun,
+ computed,
+ makeObservable,
+ observable,
+ runInAction,
+} from "mobx";
import { getCookie, setCookie } from "tiny-cookie";
import { toError } from "@shared/utils/error";
import type { CustomTheme } from "@shared/types";
@@ -42,20 +49,16 @@ export default class AuthStore extends Store {
/* The ID of the user that is currently signed in. */
@observable
- public currentUserId?: string | null;
-
+ public currentUserId?: string | null = undefined;
/* The ID of the team that is currently signed in. */
@observable
- public currentTeamId?: string | null;
-
+ public currentTeamId?: string | null = undefined;
/* A short-lived token to be used to authenticate with the collaboration server. */
@observable
- public collaborationToken?: string | null;
-
+ public collaborationToken?: string | null = undefined;
/* When set, the user will be redirected to this URL after logging out. */
@observable
- public logoutRedirectUri?: string;
-
+ public logoutRedirectUri?: string = undefined;
/* A list of teams that the current user has access to. */
@observable
public availableTeams?: {
@@ -68,24 +71,23 @@ export default class AuthStore extends Store {
/* The authentication provider the user signed in with. */
@observable
- public lastSignedIn?: string | null;
-
+ public lastSignedIn?: string | null = undefined;
/* Whether the user is currently suspended. */
@observable
public isSuspended = false;
/* The email address to contact if the user is suspended. */
@observable
- public suspendedContactEmail?: string | null;
-
+ public suspendedContactEmail?: string | null = undefined;
/* The auth configuration for the current domain. */
@observable
- public config: Config | null | undefined;
+ public config: Config | null | undefined = undefined;
rootStore: RootStore;
constructor(rootStore: RootStore) {
super(rootStore, Team);
+ makeObservable(this);
this.rootStore = rootStore;
@@ -210,7 +212,7 @@ export default class AuthStore extends Store {
});
invariant(res?.data, "Auth not available");
- runInAction("AuthStore#refresh", () => {
+ runInAction(() => {
const { data } = res;
this.addPolicies(res.policies);
this.add(data.team);
@@ -288,7 +290,7 @@ export default class AuthStore extends Store {
@action
deleteUser = async (data: { code: string }) => {
await client.post(`/users.delete`, data);
- runInAction("AuthStore#deleteUser", () => {
+ runInAction(() => {
this.currentUserId = null;
this.currentTeamId = null;
this.collaborationToken = null;
@@ -302,7 +304,7 @@ export default class AuthStore extends Store {
deleteTeam = async (data: { code: string }) => {
await client.post(`/teams.delete`, data);
- runInAction("AuthStore#deleteTeam", () => {
+ runInAction(() => {
this.currentUserId = null;
this.currentTeamId = null;
this.availableTeams = this.availableTeams?.filter(
diff --git a/app/stores/AuthenticationProvidersStore.ts b/app/stores/AuthenticationProvidersStore.ts
index 697b408724..4cc18f56dc 100644
--- a/app/stores/AuthenticationProvidersStore.ts
+++ b/app/stores/AuthenticationProvidersStore.ts
@@ -1,5 +1,5 @@
import { orderBy } from "es-toolkit/compat";
-import { computed } from "mobx";
+import { override } from "mobx";
import AuthenticationProvider from "~/models/AuthenticationProvider";
import type RootStore from "./RootStore";
import Store, { RPCAction } from "./base/Store";
@@ -11,7 +11,7 @@ export default class AuthenticationProvidersStore extends Store {
+export default class CollectionsStore extends IndexedStore {
constructor(rootStore: RootStore) {
super(rootStore, Collection);
+ makeObservable(this);
}
/**
@@ -35,21 +36,15 @@ export default class CollectionsStore extends Store {
return this.orderedData.filter((c) => c.isActive);
}
- @computed
+ @override
get orderedData(): Collection[] {
- let collections = Array.from(this.data.values());
- collections = collections
- .filter((collection) => !collection.deletedAt)
- .filter((collection) => {
- const can = this.rootStore.policies.abilities(collection.id);
- return isEmpty(can) || can.readDocument;
- });
- return collections.sort((a, b) => {
- if (a.index === b.index) {
- return a.updatedAt > b.updatedAt ? -1 : 1;
+ return super.orderedData.filter((collection) => {
+ if (collection.deletedAt) {
+ return false;
}
- return a.index < b.index ? -1 : 1;
+ const can = this.rootStore.policies.abilities(collection.id);
+ return isEmpty(can) || can.readDocument;
});
}
@@ -129,7 +124,7 @@ export default class CollectionsStore extends Store {
const res = await client.post("/collections.archive", {
id: collection.id,
});
- runInAction("Collection#archive", () => {
+ runInAction(() => {
invariant(res?.data, "Data should be available");
this.add(res.data);
this.addPolicies(res.policies);
@@ -141,7 +136,7 @@ export default class CollectionsStore extends Store {
const res = await client.post("/collections.restore", {
id: collection.id,
});
- runInAction("Collection#restore", () => {
+ runInAction(() => {
invariant(res?.data, "Data should be available");
this.add(res.data);
this.addPolicies(res.policies);
@@ -163,34 +158,17 @@ export default class CollectionsStore extends Store {
return result;
}
- @action
fetchNamedPage = async (
request = "list",
- options:
- | (PaginationParams & { statusFilter: CollectionStatusFilter[] })
- | undefined
- ): Promise => {
- this.isFetching = true;
-
- try {
- const res = await client.post(`/collections.${request}`, options);
- invariant(res?.data, "Collection list not available");
- return runInAction("CollectionsStore#fetchNamedPage", () => {
- const collections = res.data.map(this.add);
- this.addPolicies(res.policies);
- this.isLoaded = true;
- return collections;
- });
- } finally {
- this.isFetching = false;
- }
- };
+ options: (PaginationParams & { filters?: Filter[] }) | undefined
+ ): Promise =>
+ this.fetchPaginated(`/collections.${request}`, options);
@action
fetchArchived = async (options?: PaginationParams): Promise =>
this.fetchNamedPage("list", {
...options,
- statusFilter: [CollectionStatusFilter.Archived],
+ filters: [{ field: "archivedAt", operator: "isNotNull" }],
});
get(id: string = ""): Collection | undefined {
diff --git a/app/stores/CommentsStore.ts b/app/stores/CommentsStore.ts
index cb531c002a..5d44fb6762 100644
--- a/app/stores/CommentsStore.ts
+++ b/app/stores/CommentsStore.ts
@@ -1,6 +1,6 @@
import invariant from "invariant";
import { compact, differenceBy, keyBy, orderBy, uniq } from "es-toolkit/compat";
-import { action, computed } from "mobx";
+import { action, makeObservable, override } from "mobx";
import Comment from "~/models/Comment";
import { type CommentSortOption, CommentSortType } from "~/types";
import { client } from "~/utils/ApiClient";
@@ -10,6 +10,7 @@ import Store from "./base/Store";
export default class CommentsStore extends Store {
constructor(rootStore: RootStore) {
super(rootStore, Comment);
+ makeObservable(this);
}
/**
@@ -146,7 +147,7 @@ export default class CommentsStore extends Store {
return this.data.get(res.data.id) as Comment;
};
- @computed
+ @override
get orderedData(): Comment[] {
return orderBy(Array.from(this.data.values()), "createdAt", "asc");
}
diff --git a/app/stores/DialogsStore.ts b/app/stores/DialogsStore.ts
index dbc3f6d332..737986be4d 100644
--- a/app/stores/DialogsStore.ts
+++ b/app/stores/DialogsStore.ts
@@ -1,4 +1,4 @@
-import { observable, action } from "mobx";
+import { action, makeObservable, observable } from "mobx";
import { v4 as uuidv4 } from "uuid";
import * as React from "react";
@@ -20,11 +20,15 @@ type GuideDefinition = {
export default class DialogsStore {
@observable.shallow
- guide: GuideDefinition;
+ guide: GuideDefinition | undefined = undefined;
@observable.shallow
modalStack = new Map();
+ constructor() {
+ makeObservable(this);
+ }
+
openGuide = ({
title,
content,
diff --git a/app/stores/DocumentPresenceStore.ts b/app/stores/DocumentPresenceStore.ts
index 261844f94d..63b108e35a 100644
--- a/app/stores/DocumentPresenceStore.ts
+++ b/app/stores/DocumentPresenceStore.ts
@@ -1,4 +1,4 @@
-import { observable, action } from "mobx";
+import { action, makeObservable, observable } from "mobx";
import type { AwarenessChangeEvent } from "~/types";
import type RootStore from "./RootStore";
@@ -16,6 +16,7 @@ export default class PresenceStore {
constructor(rootStore: RootStore) {
this.rootStore = rootStore;
+ makeObservable(this);
}
/**
diff --git a/app/stores/DocumentsStore.ts b/app/stores/DocumentsStore.ts
index 77a6850bde..169e8c5403 100644
--- a/app/stores/DocumentsStore.ts
+++ b/app/stores/DocumentsStore.ts
@@ -1,6 +1,13 @@
import invariant from "invariant";
-import { compact, filter, omitBy, orderBy } from "es-toolkit/compat";
-import { observable, action, computed, runInAction } from "mobx";
+import { compact, omitBy, orderBy } from "es-toolkit/compat";
+import {
+ action,
+ computed,
+ makeObservable,
+ observable,
+ override,
+ runInAction,
+} from "mobx";
import type { DirectionFilter, SortFilter } from "@shared/types";
import {
AttachmentPreset,
@@ -11,7 +18,6 @@ import type { Filter } from "@shared/helpers/FilterHelper";
import { subtractDate } from "@shared/utils/date";
import { bytesToHumanReadable } from "@shared/utils/files";
import naturalSort from "@shared/utils/naturalSort";
-import type { JSONObject } from "@shared/types";
import type RootStore from "~/stores/RootStore";
import Store from "~/stores/base/Store";
import Document from "~/models/Document";
@@ -52,7 +58,7 @@ export default class DocumentsStore extends Store {
similar: Map = new Map();
@observable
- movingDocumentId: string | null | undefined;
+ movingDocumentId: string | null | undefined = undefined;
importFileTypes: string[] = [
".md",
@@ -84,6 +90,7 @@ export default class DocumentsStore extends Store {
constructor(rootStore: RootStore) {
super(rootStore, Document);
+ makeObservable(this);
}
@computed
@@ -93,7 +100,7 @@ export default class DocumentsStore extends Store {
@computed
get all(): Document[] {
- return filter(this.orderedData, (d) => !d.archivedAt && !d.deletedAt);
+ return this.orderedData.filter((d) => !d.archivedAt && !d.deletedAt);
}
@computed
@@ -117,15 +124,14 @@ export default class DocumentsStore extends Store {
createdByUser(userId: string): Document[] {
return orderBy(
- filter(this.all, (d) => d.createdBy?.id === userId),
+ this.all.filter((d) => d.createdBy?.id === userId),
"updatedAt",
"desc"
);
}
inCollection(collectionId: string): Document[] {
- return filter(
- this.all,
+ return this.all.filter(
(document) => document.collectionId === collectionId
);
}
@@ -144,12 +150,11 @@ export default class DocumentsStore extends Store {
document.isArchived &&
!document.isDeleted;
- return filter(this.orderedData, filterCond);
+ return this.orderedData.filter(filterCond);
}
unarchivedInCollection(collectionId: string): Document[] {
- return filter(
- this.orderedData,
+ return this.orderedData.filter(
(document) =>
document.collectionId === collectionId &&
!document.isArchived &&
@@ -158,8 +163,7 @@ export default class DocumentsStore extends Store {
}
publishedInCollection(collectionId: string): Document[] {
- return filter(
- this.all,
+ return this.all.filter(
(document) =>
document.collectionId === collectionId && !!document.publishedAt
);
@@ -250,16 +254,14 @@ export default class DocumentsStore extends Store {
);
if (options.userId) {
- deleted = filter(
- deleted,
+ deleted = deleted.filter(
(document) => document.deletedBy?.id === options.userId
);
}
if (options.dateFilter) {
const cutoff = subtractDate(new Date(), options.dateFilter);
- deleted = filter(
- deleted,
+ deleted = deleted.filter(
(document) =>
!!document.deletedAt && new Date(document.deletedAt) >= cutoff
);
@@ -279,14 +281,12 @@ export default class DocumentsStore extends Store {
collectionId?: string;
} = {}
): Document[] => {
- let drafts = filter(
- orderBy(this.all, "updatedAt", "desc"),
+ let drafts = orderBy(this.all, "updatedAt", "desc").filter(
(doc) => !doc.publishedAt
);
if (options.dateFilter) {
- drafts = filter(
- drafts,
+ drafts = drafts.filter(
(draft) =>
new Date(draft.updatedAt) >=
subtractDate(new Date(), options.dateFilter || "year")
@@ -294,9 +294,9 @@ export default class DocumentsStore extends Store {
}
if (options.collectionId) {
- drafts = filter(drafts, {
- collectionId: options.collectionId,
- });
+ drafts = drafts.filter(
+ (draft) => draft.collectionId === options.collectionId
+ );
}
return drafts;
@@ -314,7 +314,7 @@ export default class DocumentsStore extends Store {
const res = await client.post("/relationships.list", { documentId });
invariant(res?.data, "Relationships not available");
- runInAction("DocumentsStore#fetchRelationships", () => {
+ runInAction(() => {
res.data.documents.forEach(this.add);
this.addPolicies(res.policies);
@@ -359,32 +359,17 @@ export default class DocumentsStore extends Store {
});
invariant(res?.data, "Document list not available");
- runInAction("DocumentsStore#fetchChildDocuments", () => {
+ runInAction(() => {
res.data.forEach(this.add);
this.addPolicies(res.policies);
});
};
- @action
fetchNamedPage = async (
request = "list",
options: FetchPageParams | undefined
- ): Promise => {
- this.isFetching = true;
-
- try {
- const res = await client.post(`/documents.${request}`, options);
- invariant(res?.data, "Document list not available");
- return runInAction("DocumentsStore#fetchNamedPage", () => {
- const documents = res.data.map(this.add);
- this.addPolicies(res.policies);
- this.isLoaded = true;
- return documents;
- });
- } finally {
- this.isFetching = false;
- }
- };
+ ): Promise =>
+ this.fetchPaginated(`/documents.${request}`, options);
@action
fetchArchived = async (options?: PaginationParams): Promise =>
@@ -455,14 +440,14 @@ export default class DocumentsStore extends Store {
@action
searchTitles = async (options?: SearchParams): Promise => {
- const compactedOptions = omitBy(options, (o) => !o) as JSONObject;
+ const compactedOptions = omitBy(options, (o) => !o);
const res = await client.post("/documents.search_titles", {
...compactedOptions,
});
invariant(res?.data, "Search response should be available");
// add the documents and associated policies to the store
- runInAction("DocumentsStore#searchTitles", () => {
+ runInAction(() => {
res.data.forEach(this.add);
this.addPolicies(res.policies);
});
@@ -486,14 +471,14 @@ export default class DocumentsStore extends Store {
@action
search = async (options: SearchParams): Promise => {
- const compactedOptions = omitBy(options, (o) => !o) as JSONObject;
+ const compactedOptions = omitBy(options, (o) => !o);
const res = await client.post("/documents.search", {
...compactedOptions,
});
invariant(res?.data, "Search response should be available");
// add the documents and associated policies to the store
- runInAction("DocumentsStore#search", () => {
+ runInAction(() => {
res.data.forEach((result: SearchResult) => this.add(result.document));
this.addPolicies(res.policies);
});
@@ -640,7 +625,7 @@ export default class DocumentsStore extends Store {
return this.add(res.data);
};
- @action
+ @override
async delete(
document: Document,
options?: {
@@ -685,7 +670,7 @@ export default class DocumentsStore extends Store {
const res = await client.post("/documents.archive", {
id: document.id,
});
- runInAction("Document#archive", () => {
+ runInAction(() => {
invariant(res?.data, "Data should be available");
document.updateData(res.data);
this.addPolicies(res.policies);
@@ -709,7 +694,7 @@ export default class DocumentsStore extends Store {
revisionId: options.revisionId,
collectionId: options.collectionId,
});
- runInAction("Document#restore", () => {
+ runInAction(() => {
invariant(res?.data, "Data should be available");
document.updateData(res.data);
this.addPolicies(res.policies);
@@ -732,7 +717,7 @@ export default class DocumentsStore extends Store {
...options,
});
- runInAction("Document#unpublish", () => {
+ runInAction(() => {
invariant(res?.data, "Data should be available");
// unpublishing could sometimes detach the document from the collection.
// so, get the collection id before data is updated.
diff --git a/app/stores/EmojiStore.ts b/app/stores/EmojiStore.ts
index 616eb632ab..67c42a536e 100644
--- a/app/stores/EmojiStore.ts
+++ b/app/stores/EmojiStore.ts
@@ -1,4 +1,4 @@
-import { computed } from "mobx";
+import { override } from "mobx";
import Emoji from "~/models/Emoji";
import naturalSort from "@shared/utils/naturalSort";
import type RootStore from "./RootStore";
@@ -17,7 +17,7 @@ export default class EmojisStore extends Store {
super(rootStore, Emoji);
}
- @computed
+ @override
get orderedData(): Emoji[] {
return naturalSort(Array.from(this.data.values()), "name");
}
diff --git a/app/stores/GroupMembershipsStore.ts b/app/stores/GroupMembershipsStore.ts
index 028968f493..3e5f37f6a1 100644
--- a/app/stores/GroupMembershipsStore.ts
+++ b/app/stores/GroupMembershipsStore.ts
@@ -1,5 +1,5 @@
import invariant from "invariant";
-import { action, runInAction } from "mobx";
+import { override } from "mobx";
import {
type CollectionPermission,
type DocumentPermission,
@@ -8,11 +8,7 @@ import GroupMembership from "~/models/GroupMembership";
import type { PaginationParams } from "~/types";
import { client } from "~/utils/ApiClient";
import type RootStore from "./RootStore";
-import Store, {
- PAGINATION_SYMBOL,
- type PaginatedResponse,
- RPCAction,
-} from "./base/Store";
+import Store, { type PaginatedResponse, RPCAction } from "./base/Store";
export default class GroupMembershipsStore extends Store {
actions = [RPCAction.Create, RPCAction.Delete];
@@ -26,13 +22,12 @@ export default class GroupMembershipsStore extends Store {
*
* @param id the ID of the membership to remove.
*/
- @action
+ @override
remove(id: string, options?: { permanent?: boolean }): void {
super.remove(id, options);
this.rootStore.policies.removeForMembership(id);
}
- @action
fetchPage = async ({
collectionId,
documentId,
@@ -42,38 +37,19 @@ export default class GroupMembershipsStore extends Store {
collectionId?: string;
groupId?: string;
}): Promise> => {
- this.isFetching = true;
+ const [endpoint, body] = collectionId
+ ? ["/collections.group_memberships", { id: collectionId, ...params }]
+ : documentId
+ ? ["/documents.group_memberships", { id: documentId, ...params }]
+ : ["/groupMemberships.list", params];
- try {
- const res = collectionId
- ? await client.post(`/collections.group_memberships`, {
- id: collectionId,
- ...params,
- })
- : documentId
- ? await client.post(`/documents.group_memberships`, {
- id: documentId,
- ...params,
- })
- : await client.post(`/groupMemberships.list`, params);
- invariant(res?.data, "Data not available");
-
- let response: PaginatedResponse = [];
- runInAction(`GroupMembershipsStore#fetchPage`, () => {
- res.data.groups?.forEach(this.rootStore.groups.add);
- res.data.documents?.forEach(this.rootStore.documents.add);
- response = res.data.groupMemberships.map(this.add);
- this.isLoaded = true;
- });
-
- response[PAGINATION_SYMBOL] = res.pagination;
- return response;
- } finally {
- this.isFetching = false;
- }
+ return this.fetchPaginated(endpoint, body, [
+ this.rootStore.groups,
+ this.rootStore.documents,
+ ]);
};
- @action
+ @override
async create({
collectionId,
documentId,
@@ -102,7 +78,7 @@ export default class GroupMembershipsStore extends Store {
return cgm[0];
}
- @action
+ @override
async delete({
collectionId,
documentId,
diff --git a/app/stores/GroupUsersStore.ts b/app/stores/GroupUsersStore.ts
index 155980b58e..3e41a46ee5 100644
--- a/app/stores/GroupUsersStore.ts
+++ b/app/stores/GroupUsersStore.ts
@@ -1,49 +1,28 @@
import invariant from "invariant";
-import { filter } from "es-toolkit/compat";
-import { action, runInAction } from "mobx";
+import { action, makeObservable, override, runInAction } from "mobx";
import GroupUser from "~/models/GroupUser";
import type { PaginationParams } from "~/types";
import { GroupPermission } from "@shared/types";
import { client } from "~/utils/ApiClient";
import type RootStore from "./RootStore";
-import Store, {
- type PaginatedResponse,
- PAGINATION_SYMBOL,
- RPCAction,
-} from "./base/Store";
+import Store, { type PaginatedResponse, RPCAction } from "./base/Store";
export default class GroupUsersStore extends Store {
actions = [RPCAction.Create, RPCAction.Update, RPCAction.Delete];
+ responseKey = "groupMemberships";
+
constructor(rootStore: RootStore) {
super(rootStore, GroupUser);
+ makeObservable(this);
}
- @action
fetchPage = async (
params: PaginationParams | undefined
- ): Promise> => {
- this.isFetching = true;
+ ): Promise> =>
+ this.fetchPaginated("/groups.memberships", params, [this.rootStore.users]);
- try {
- const res = await client.post(`/groups.memberships`, params);
- invariant(res?.data, "Data not available");
-
- let response: PaginatedResponse = [];
- runInAction(`GroupUsersStore#fetchPage`, () => {
- res.data.users.forEach(this.rootStore.users.add);
- response = res.data.groupMemberships.map(this.add);
- this.isLoaded = true;
- });
-
- response[PAGINATION_SYMBOL] = res.pagination;
- return response;
- } finally {
- this.isFetching = false;
- }
- };
-
- @action
+ @override
async create({
groupId,
userId,
@@ -60,7 +39,7 @@ export default class GroupUsersStore extends Store {
});
invariant(res?.data, "Group Membership data should be available");
- return runInAction(`GroupUsersStore#create`, () => {
+ return runInAction(() => {
res.data.users.forEach(this.rootStore.users.add);
res.data.groups.forEach(this.rootStore.groups.add);
@@ -69,7 +48,7 @@ export default class GroupUsersStore extends Store {
});
}
- @action
+ @override
async delete({ groupId, userId }: { groupId: string; userId: string }) {
const res = await client.post("/groups.remove_user", {
id: groupId,
@@ -77,13 +56,13 @@ export default class GroupUsersStore extends Store {
});
invariant(res?.data, "Group Membership data should be available");
this.remove(`${userId}-${groupId}`);
- runInAction(`GroupUsersStore#delete`, () => {
+ runInAction(() => {
res.data.groups.forEach(this.rootStore.groups.add);
this.isLoaded = true;
});
}
- @action
+ @override
async update({
groupId,
userId,
@@ -100,7 +79,7 @@ export default class GroupUsersStore extends Store {
});
invariant(res?.data, "Group Membership data should be available");
- return runInAction(`GroupUsersStore#update`, () => {
+ return runInAction(() => {
res.data.users.forEach(this.rootStore.users.add);
res.data.groups.forEach(this.rootStore.groups.add);
@@ -119,7 +98,7 @@ export default class GroupUsersStore extends Store {
};
inGroup = (groupId: string) =>
- filter(this.orderedData, (member) => member.groupId === groupId);
+ this.orderedData.filter((member) => member.groupId === groupId);
/**
* Returns the membership of a user in a group, if loaded.
diff --git a/app/stores/GroupsStore.ts b/app/stores/GroupsStore.ts
index 22bb61c126..7fc60dcc60 100644
--- a/app/stores/GroupsStore.ts
+++ b/app/stores/GroupsStore.ts
@@ -1,12 +1,9 @@
-import invariant from "invariant";
-import { filter } from "es-toolkit/compat";
-import { action, runInAction, computed } from "mobx";
+import { override } from "mobx";
import naturalSort from "@shared/utils/naturalSort";
import Group from "~/models/Group";
import type { PaginationParams } from "~/types";
-import { client } from "~/utils/ApiClient";
import type RootStore from "./RootStore";
-import Store from "./base/Store";
+import Store, { type PaginatedResponse } from "./base/Store";
type FetchPageParams = PaginationParams & { query?: string };
@@ -15,31 +12,15 @@ export default class GroupsStore extends Store {
super(rootStore, Group);
}
- @computed
+ @override
get orderedData(): Group[] {
return naturalSort(Array.from(this.data.values()), "name");
}
- @action
- fetchPage = async (params: FetchPageParams | undefined): Promise => {
- this.isFetching = true;
-
- try {
- const res = await client.post(`/groups.list`, params);
- invariant(res?.data, "Data not available");
-
- let models: Group[] = [];
- runInAction(`GroupsStore#fetchPage`, () => {
- this.addPolicies(res.policies);
- models = res.data.groups.map(this.add);
- res.data.groupMemberships.forEach(this.rootStore.groupUsers.add);
- this.isLoaded = true;
- });
- return models;
- } finally {
- this.isFetching = false;
- }
- };
+ fetchPage = async (
+ params: FetchPageParams | undefined
+ ): Promise> =>
+ this.fetchPaginated("/groups.list", params, [this.rootStore.groupUsers]);
/**
* Returns groups that are in the given collection, optionally filtered by a query.
@@ -49,12 +30,11 @@ export default class GroupsStore extends Store {
* @returns A list of groups that are in the given collection.
*/
inCollection = (collectionId: string, query?: string) => {
- const memberships = filter(
- this.rootStore.groupMemberships.orderedData,
+ const memberships = this.rootStore.groupMemberships.orderedData.filter(
(member) => member.collectionId === collectionId
);
const groupIds = memberships.map((member) => member.groupId);
- const groups = filter(this.orderedData, (group) =>
+ const groups = this.orderedData.filter((group) =>
groupIds.includes(group.id)
);
@@ -69,13 +49,11 @@ export default class GroupsStore extends Store