mirror of
https://github.com/outline/outline.git
synced 2026-06-13 03:14:59 +03:00
adbffc0734
* 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.
98 lines
2.3 KiB
TypeScript
98 lines
2.3 KiB
TypeScript
import invariant from "invariant";
|
|
import { action, runInAction, computed } from "mobx";
|
|
import Pin from "~/models/Pin";
|
|
import type { PaginationParams } from "~/types";
|
|
import { client } from "~/utils/ApiClient";
|
|
import { AuthorizationError, NotFoundError } from "~/utils/errors";
|
|
import type RootStore from "./RootStore";
|
|
import Store from "./base/Store";
|
|
|
|
type FetchParams = PaginationParams & { collectionId?: string };
|
|
|
|
export default class PinsStore extends Store<Pin> {
|
|
constructor(rootStore: RootStore) {
|
|
super(rootStore, Pin);
|
|
}
|
|
|
|
@action
|
|
async fetchOne({
|
|
documentId,
|
|
collectionId,
|
|
}: {
|
|
documentId: string;
|
|
collectionId: string | null;
|
|
}) {
|
|
const pin = this.orderedData.find(
|
|
(p) => p.documentId === documentId && p.collectionId === collectionId
|
|
);
|
|
|
|
if (pin) {
|
|
return pin;
|
|
}
|
|
|
|
this.isFetching = true;
|
|
|
|
try {
|
|
const res = await client.post(`/${this.apiEndpoint}.info`, {
|
|
documentId,
|
|
collectionId,
|
|
});
|
|
if (!res) {
|
|
return;
|
|
}
|
|
invariant(res?.data, "Data should be available");
|
|
return this.add(res.data);
|
|
} catch (err) {
|
|
if (err instanceof AuthorizationError || err instanceof NotFoundError) {
|
|
return;
|
|
}
|
|
throw err;
|
|
} finally {
|
|
this.isFetching = false;
|
|
}
|
|
}
|
|
|
|
@action
|
|
fetchPage = async (params?: FetchParams): Promise<Pin[]> => {
|
|
this.isFetching = true;
|
|
|
|
try {
|
|
const res = await client.post(`/pins.list`, params);
|
|
invariant(res?.data, "Data not available");
|
|
|
|
let models: Pin[] = [];
|
|
runInAction(`PinsStore#fetchPage`, () => {
|
|
res.data.documents.forEach(this.rootStore.documents.add);
|
|
models = res.data.pins.map(this.add);
|
|
this.addPolicies(res.policies);
|
|
this.isLoaded = true;
|
|
});
|
|
|
|
return models;
|
|
} finally {
|
|
this.isFetching = false;
|
|
}
|
|
};
|
|
|
|
inCollection = (collectionId: string) =>
|
|
this.orderedData.filter((pin) => pin.collectionId === collectionId);
|
|
|
|
@computed
|
|
get home() {
|
|
return this.orderedData.filter((pin) => !pin.collectionId);
|
|
}
|
|
|
|
@computed
|
|
get orderedData(): Pin[] {
|
|
const pins = Array.from(this.data.values());
|
|
|
|
return pins.sort((a, b) => {
|
|
if (a.index === b.index) {
|
|
return a.updatedAt > b.updatedAt ? -1 : 1;
|
|
}
|
|
|
|
return a.index < b.index ? -1 : 1;
|
|
});
|
|
}
|
|
}
|