Files
outline/app/stores/UserMembershipsStore.ts
Tom Moor adbffc0734 chore: clear mechanical lint warnings (Phase 1) (#12198)
* chore: clear mechanical lint warnings

Drops 44 oxlint warnings (559 → 515) by fixing easy mechanical rules
across the codebase: no-useless-escape, no-duplicate-type-constituents,
no-redundant-type-constituents, no-unused-expressions,
no-meaningless-void-operator, require-array-sort-compare, await-thenable.

* chore: drop callback parameter from useCallback deps

The `open` argument is a parameter of the callback, not a closed-over
variable, so it doesn't belong in the deps array.

* chore: promote cleared lint rules to errors

Promotes the rules cleared in this PR from warn to error so future
violations fail the lint:

- no-unused-expressions
- typescript/await-thenable
- typescript/no-duplicate-type-constituents
- typescript/no-meaningless-void-operator
- typescript/require-array-sort-compare

Removes the override that suppressed no-useless-escape on source
files (the global rule is already error) and fixes the 21 escape
violations that this exposed in regex character classes and template
literals.

* chore: address PR review feedback

- usePinnedDocuments: simplify UrlId to plain string instead of the
  intersection trick.
- PlantUML embed: move - to end of character class so it's a literal
  hyphen rather than a range operator.
- checkboxes: type token params as Token | undefined to match the
  actual call sites that pass tokens[index - 2] etc.
2026-04-28 20:00:03 -04:00

119 lines
3.2 KiB
TypeScript

import invariant from "invariant";
import { action, runInAction, computed } from "mobx";
import UserMembership from "~/models/UserMembership";
import type { PaginationParams } from "~/types";
import { client } from "~/utils/ApiClient";
import type RootStore from "./RootStore";
import Store, { PAGINATION_SYMBOL, RPCAction } from "./base/Store";
export default class UserMembershipsStore extends Store<UserMembership> {
actions = [
RPCAction.List,
RPCAction.Create,
RPCAction.Delete,
RPCAction.Update,
];
constructor(rootStore: RootStore) {
super(rootStore, UserMembership);
}
@action
fetchPage = async (params?: PaginationParams): Promise<UserMembership[]> => {
this.isFetching = true;
try {
const res = await client.post(`/userMemberships.list`, params);
invariant(res?.data, "Data not available");
return runInAction(`UserMembershipsStore#fetchPage`, () => {
res.data.documents.forEach(this.rootStore.documents.add);
this.addPolicies(res.policies);
this.isLoaded = true;
return res.data.memberships.map(this.add);
});
} finally {
this.isFetching = false;
}
};
@action
fetchDocumentMemberships = async (
params: (PaginationParams & { id: string }) | undefined
): Promise<UserMembership[]> => {
this.isFetching = true;
try {
const res = await client.post(`/documents.memberships`, params);
invariant(res?.data, "Data not available");
return runInAction(`MembershipsStore#fetchDocmentMemberships`, () => {
res.data.users.forEach(this.rootStore.users.add);
const response = res.data.memberships.map(this.add);
this.isLoaded = true;
response[PAGINATION_SYMBOL] = res.pagination;
return response;
});
} finally {
this.isFetching = false;
}
};
@action
async create({ documentId, userId, permission }: Partial<UserMembership>) {
const res = await client.post("/documents.add_user", {
id: documentId,
userId,
permission,
});
return runInAction(`UserMembershipsStore#create`, () => {
invariant(res?.data, "Membership data should be available");
res.data.users.forEach(this.rootStore.users.add);
const memberships = res.data.memberships.map(this.add);
return memberships[0];
});
}
@action
async delete({ documentId, userId }: UserMembership) {
await client.post("/documents.remove_user", {
id: documentId,
userId,
});
this.removeAll({ userId, documentId });
}
@computed
get orderedData(): UserMembership[] {
const memberships = Array.from(this.data.values());
return memberships.sort((a, b) => {
if (a.index === b.index) {
return a.updatedAt > b.updatedAt ? -1 : 1;
}
return a.index < b.index ? -1 : 1;
});
}
/**
* Returns the user membership associated with the document.
*/
getByDocumentId = (documentId: string): UserMembership | undefined => {
const membership = this.find({ documentId });
if (membership) {
return membership;
}
const document = this.rootStore.documents.get(documentId);
return document?.parentDocumentId
? this.getByDocumentId(document.parentDocumentId)
: undefined;
};
}