Files
outline/app/stores/GroupUsersStore.ts
Tom Moor 0139b91b5d chore: Replace lodash with es-toolkit (#12281)
* chore: Replace lodash with es-toolkit

Migrate all direct lodash imports to es-toolkit/compat for a smaller,
faster, lodash-compatible utility library. Transitive lodash usage from
other packages remains unchanged.

* fix: Restore isPlainObject semantics in CanCan policy

The lodash migration aliased `isObject` to `lodash/isPlainObject` and
the codemod incorrectly mapped the local name to es-toolkit's `isObject`,
which also returns true for arrays and functions. This caused condition
objects in policy definitions to be skipped, breaking authorization
checks across the codebase.

* fix: Restore unicode-aware length counting in validators

es-toolkit/compat's size() returns string.length, while lodash's _.size()
counts unicode code points. Switch to [...value].length to preserve the
previous behavior so multi-byte characters like emoji count as one.
2026-05-06 21:03:47 -04:00

124 lines
3.3 KiB
TypeScript

import invariant from "invariant";
import { filter } from "es-toolkit/compat";
import { action, 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";
export default class GroupUsersStore extends Store<GroupUser> {
actions = [RPCAction.Create, RPCAction.Update, RPCAction.Delete];
constructor(rootStore: RootStore) {
super(rootStore, GroupUser);
}
@action
fetchPage = async (
params: PaginationParams | undefined
): Promise<PaginatedResponse<GroupUser>> => {
this.isFetching = true;
try {
const res = await client.post(`/groups.memberships`, params);
invariant(res?.data, "Data not available");
let response: PaginatedResponse<GroupUser> = [];
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
async create({
groupId,
userId,
permission = GroupPermission.Member,
}: {
groupId: string;
userId: string;
permission?: GroupPermission;
}) {
const res = await client.post("/groups.add_user", {
id: groupId,
userId,
permission,
});
invariant(res?.data, "Group Membership data should be available");
return runInAction(`GroupUsersStore#create`, () => {
res.data.users.forEach(this.rootStore.users.add);
res.data.groups.forEach(this.rootStore.groups.add);
const groupMemberships = res.data.groupMemberships.map(this.add);
return groupMemberships[0];
});
}
@action
async delete({ groupId, userId }: { groupId: string; userId: string }) {
const res = await client.post("/groups.remove_user", {
id: groupId,
userId,
});
invariant(res?.data, "Group Membership data should be available");
this.remove(`${userId}-${groupId}`);
runInAction(`GroupUsersStore#delete`, () => {
res.data.groups.forEach(this.rootStore.groups.add);
this.isLoaded = true;
});
}
@action
async update({
groupId,
userId,
permission,
}: {
groupId: string;
userId: string;
permission?: GroupPermission;
}) {
const res = await client.post("/groups.update_user", {
id: groupId,
userId,
permission,
});
invariant(res?.data, "Group Membership data should be available");
return runInAction(`GroupUsersStore#update`, () => {
res.data.users.forEach(this.rootStore.users.add);
res.data.groups.forEach(this.rootStore.groups.add);
const groupMemberships = res.data.groupMemberships.map(this.add);
return groupMemberships[0];
});
}
@action
removeGroupMemberships = (groupId: string) => {
this.data.forEach((_, key) => {
if (key.includes(groupId)) {
this.remove(key);
}
});
};
inGroup = (groupId: string) =>
filter(this.orderedData, (member) => member.groupId === groupId);
}