Files
outline/app/stores/TemplatesStore.ts
T
Tom MoorandClaude Fable 5.1 6a34835260 refactor: DRY up app stores with a shared page fetch helper and IndexedStore (#13633)
* 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>
2026-09-04 05:10:15 -04:00

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");
}
}