Files
outline/app/stores/StarsStore.ts
Tom Moor bf45e97641 chore: Enforce type import consistency (#10968)
* Update types

* fix circular dep

* type imports

* lint type imports and --fix
2025-12-19 23:07:02 -05:00

49 lines
1.3 KiB
TypeScript

import invariant from "invariant";
import { action, runInAction, computed } from "mobx";
import Star from "~/models/Star";
import type { PaginationParams } from "~/types";
import { client } from "~/utils/ApiClient";
import type RootStore from "./RootStore";
import Store from "./base/Store";
export default class StarsStore extends Store<Star> {
constructor(rootStore: RootStore) {
super(rootStore, Star);
}
@action
fetchPage = async (
params?: PaginationParams | undefined
): Promise<Star[]> => {
this.isFetching = true;
try {
const res = await client.post(`/stars.list`, params);
invariant(res?.data, "Data not available");
return runInAction(`StarsStore#fetchPage`, () => {
res.data.documents.forEach(this.rootStore.documents.add);
const models = res.data.stars.map(this.add);
this.addPolicies(res.policies);
this.isLoaded = true;
return models;
});
} finally {
this.isFetching = false;
}
};
@computed
get orderedData(): Star[] {
const stars = Array.from(this.data.values());
return stars.sort((a, b) => {
if (a.index === b.index) {
return a.updatedAt > b.updatedAt ? -1 : 1;
}
return a.index < b.index ? -1 : 1;
});
}
}