Files
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

37 lines
920 B
TypeScript

import { orderBy } from "es-toolkit/compat";
import View from "~/models/View";
import type RootStore from "./RootStore";
import Store, { RPCAction } from "./base/Store";
export default class ViewsStore extends Store<View> {
actions = [RPCAction.List, RPCAction.Create];
constructor(rootStore: RootStore) {
super(rootStore, View);
}
inDocument(documentId: string): View[] {
return orderBy(
this.orderedData.filter((view) => view.documentId === documentId),
"lastViewedAt",
"desc"
);
}
countForDocument(documentId: string): number {
const views = this.inDocument(documentId);
return views.reduce((memo, view) => memo + view.count, 0);
}
touch(documentId: string, userId: string) {
const view = this.orderedData.find(
(view) => view.documentId === documentId && view.userId === userId
);
if (!view) {
return;
}
view.touch();
}
}