mirror of
https://github.com/outline/outline.git
synced 2026-12-03 18:10:36 +03:00
* refactor: DRY up app stores with fetchPaginated helper and IndexedStore Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * refactor: Resolve related models by store responseKey instead of a key map Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
93 lines
2.2 KiB
TypeScript
93 lines
2.2 KiB
TypeScript
import { orderBy } from "es-toolkit/compat";
|
|
import { action, computed, makeObservable, override } from "mobx";
|
|
import { invariant } from "mobx-utils";
|
|
import naturalSort from "@shared/utils/naturalSort";
|
|
import Template from "~/models/Template";
|
|
import { client } from "~/utils/ApiClient";
|
|
import type RootStore from "./RootStore";
|
|
import Store from "./base/Store";
|
|
|
|
export default class TemplatesStore extends Store<Template> {
|
|
constructor(rootStore: RootStore) {
|
|
super(rootStore, Template);
|
|
makeObservable(this);
|
|
}
|
|
|
|
/**
|
|
* Templates that are published, and so available to insert into documents.
|
|
*/
|
|
@computed
|
|
get published(): Template[] {
|
|
return this.orderedData.filter(
|
|
(template) => template.isActive && !template.isDraft
|
|
);
|
|
}
|
|
|
|
@computed
|
|
get alphabetical(): Template[] {
|
|
return naturalSort(this.published, "title");
|
|
}
|
|
|
|
@computed
|
|
get all(): Template[] {
|
|
return this.orderedData.filter((d) => !d.deletedAt);
|
|
}
|
|
|
|
@action
|
|
duplicate = async (
|
|
template: Template,
|
|
options?: {
|
|
title?: string;
|
|
publish?: boolean;
|
|
}
|
|
) => {
|
|
const res = await client.post("/templates.duplicate", {
|
|
id: template.id,
|
|
...options,
|
|
});
|
|
invariant(res?.data, "Data should be available");
|
|
|
|
this.addPolicies(res.policies);
|
|
this.add(res.data);
|
|
};
|
|
|
|
@action
|
|
templatize = async ({
|
|
id,
|
|
collectionId,
|
|
publish,
|
|
}: {
|
|
id: string;
|
|
collectionId: string | null;
|
|
publish: boolean;
|
|
}): Promise<Template | undefined> => {
|
|
const res = await client.post("/documents.templatize", {
|
|
id,
|
|
collectionId,
|
|
publish,
|
|
});
|
|
invariant(res?.data, "Data should be available");
|
|
|
|
this.addPolicies(res.policies);
|
|
this.add(res.data);
|
|
return this.data.get(res.data.id);
|
|
};
|
|
|
|
get(id: string): Template | undefined {
|
|
return id
|
|
? (this.data.get(id) ??
|
|
this.orderedData.find((doc) => id.endsWith(doc.urlId)))
|
|
: undefined;
|
|
}
|
|
|
|
@computed
|
|
get active(): Template | undefined {
|
|
return this.rootStore.ui.getActiveModels(Template)?.[0];
|
|
}
|
|
|
|
@override
|
|
get orderedData(): Template[] {
|
|
return orderBy(Array.from(this.data.values()), "createdAt", "desc");
|
|
}
|
|
}
|