Compare commits

...

2 Commits

Author SHA1 Message Date
d44fe72ff1 Fixes manifest 2025-10-11 13:54:02 -07:00
64aaad69d7 Support not-present relationships 2025-10-11 13:47:58 -07:00
4 changed files with 46 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
{ {
"short_name": "TanStack App", "short_name": "DM Companion",
"name": "Create TanStack App Sample", "name": "Dungeon Master Companion",
"icons": [ "icons": [
{ {
"src": "favicon.ico", "src": "favicon.ico",

View File

@@ -30,6 +30,9 @@ export function DocumentView({
if (v.type === "ready") { if (v.type === "ready") {
return v.value.secondary.length.toString(); return v.value.secondary.length.toString();
} }
if (v.type === "empty") {
return "0";
}
return "..."; return "...";
}); });

View File

@@ -1,14 +1,15 @@
import _ from "lodash"; import { relationshipsForDocument } from "@/lib/relationships";
import type { AnyDocument, DocumentId, Relationship } from "@/lib/types"; import type { AnyDocument, DocumentId, Relationship } from "@/lib/types";
import _ from "lodash";
import type { DocumentAction } from "./actions"; import type { DocumentAction } from "./actions";
import { import {
ready, empty,
loading, loading,
mapResult,
ready,
unloaded, unloaded,
type DocumentState, type DocumentState,
mapResult,
} from "./state"; } from "./state";
import { relationshipsForDocument } from "@/lib/relationships";
function setLoadingDocument( function setLoadingDocument(
docId: DocumentId, docId: DocumentId,
@@ -47,6 +48,36 @@ function setDocument(state: DocumentState, doc: AnyDocument): DocumentState {
}; };
} }
function setAllRelationshipsEmpty(
docId: DocumentId,
state: DocumentState,
): DocumentState {
const prevDocResult = state.documents[docId];
if (prevDocResult?.type !== "ready") {
return state;
}
const prevDoc = prevDocResult.value.doc;
const relationships = prevDocResult.value.relationships;
return {
...state,
documents: {
...state.documents,
[docId]: ready({
...prevDocResult.value,
relationships: Object.fromEntries(
relationshipsForDocument(prevDoc).map((relType) =>
relationships[relType]?.type === "ready"
? [relType, relationships[relType]]
: [relType, empty()],
),
),
}),
},
};
}
function setRelationship( function setRelationship(
docId: DocumentId, docId: DocumentId,
state: DocumentState, state: DocumentState,
@@ -118,8 +149,11 @@ export function reducer(
setDocument, setDocument,
action.relationships.reduce( action.relationships.reduce(
setRelationship.bind(null, action.doc.id), setRelationship.bind(null, action.doc.id),
setAllRelationshipsEmpty(
action.doc.id,
setDocument(state, action.doc), setDocument(state, action.doc),
), ),
),
); );
case "removeDocument": case "removeDocument":
return removeDocument(action.docId, state); return removeDocument(action.docId, state);

View File

@@ -10,11 +10,13 @@ export type Result<V> =
| { type: "unloaded" } | { type: "unloaded" }
| { type: "error"; err: unknown } | { type: "error"; err: unknown }
| { type: "loading" } | { type: "loading" }
| { type: "empty" }
| { type: "ready"; value: V }; | { type: "ready"; value: V };
export const unloaded = (): Result<any> => ({ type: "unloaded" }); export const unloaded = (): Result<any> => ({ type: "unloaded" });
export const error = (err: unknown): Result<any> => ({ type: "error", err }); export const error = (err: unknown): Result<any> => ({ type: "error", err });
export const loading = (): Result<any> => ({ type: "loading" }); export const loading = (): Result<any> => ({ type: "loading" });
export const empty = (): Result<any> => ({ type: "empty" });
export const ready = <V>(value: V): Result<V> => ({ type: "ready", value }); export const ready = <V>(value: V): Result<V> => ({ type: "ready", value });
export const mapResult = <A, B>( export const mapResult = <A, B>(