V1.13.1: add another, straight from the one just added
- A weapon, item, spell, monster or NPC added by hand opens its own pop-out, as before. That pop-out now has "+ Add another weapon" (or item, spell, monster, NPC) at its top, which goes straight back to an empty form: no closing it and pressing "+ Add" again. - All packages at version 1.13.1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,11 +25,35 @@ interface SectionProps<T> {
|
||||
campaignId: string;
|
||||
entries: T[] | undefined;
|
||||
onOpen: (entry: EntryRef) => void;
|
||||
// The "add by hand" pop-out is wanted from outside: "add another", pressed
|
||||
// in the pop-out of the one just added.
|
||||
formRequested: boolean;
|
||||
onFormClosed: () => void;
|
||||
}
|
||||
|
||||
// Which of a list's two "add" pop-outs is open: the form, or the library.
|
||||
type Adding = 'form' | 'library' | null;
|
||||
|
||||
// Which "add" pop-out a list has open, and what went wrong in it.
|
||||
function useAdding(formRequested: boolean, onFormClosed: () => void) {
|
||||
const [adding, setAdding] = useState<Adding>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
return {
|
||||
adding: formRequested ? ('form' as const) : adding,
|
||||
error,
|
||||
setError,
|
||||
open: (next: Adding) => {
|
||||
setError(null);
|
||||
setAdding(next);
|
||||
},
|
||||
close: () => {
|
||||
setError(null);
|
||||
setAdding(null);
|
||||
onFormClosed();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function SectionCard({
|
||||
title,
|
||||
addLabel,
|
||||
@@ -117,15 +141,10 @@ const StartingOption = () => (
|
||||
|
||||
const startingBadge = (entry: { isStartingOption: boolean }) => entry.isStartingOption && <span className="badge">Starting</span>;
|
||||
|
||||
export function WeaponsSection({ campaignId, entries, onOpen }: SectionProps<Weapon>) {
|
||||
export function WeaponsSection({ campaignId, entries, onOpen, formRequested, onFormClosed }: SectionProps<Weapon>) {
|
||||
const createWeapon = useCreateWeapon(campaignId);
|
||||
const addFromLibrary = useAddWeaponFromLibrary(campaignId);
|
||||
const [adding, setAdding] = useState<Adding>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const open = (next: Adding) => {
|
||||
setError(null);
|
||||
setAdding(next);
|
||||
};
|
||||
const { adding, error, setError, open, close } = useAdding(formRequested, onFormClosed);
|
||||
|
||||
async function handleSubmit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
@@ -146,8 +165,8 @@ export function WeaponsSection({ campaignId, entries, onOpen }: SectionProps<Wea
|
||||
effects: [],
|
||||
effectNote: null,
|
||||
});
|
||||
setAdding(null);
|
||||
onOpen({ kind: 'weapon', id: weapon.id });
|
||||
close();
|
||||
onOpen({ kind: 'weapon', id: weapon.id, isNew: true });
|
||||
} catch (err) {
|
||||
setError(extractErrorMessage(err));
|
||||
}
|
||||
@@ -186,11 +205,11 @@ export function WeaponsSection({ campaignId, entries, onOpen }: SectionProps<Wea
|
||||
renderMeta={(w) => `${w.category} · ${w.damageDice} ${w.damageType} · starts at level 1`}
|
||||
renderDetail={(w) => effectsSummary(w.effects, w.effectNote)}
|
||||
error={error}
|
||||
onClose={() => setAdding(null)}
|
||||
onClose={close}
|
||||
/>
|
||||
)}
|
||||
{adding === 'form' && (
|
||||
<Modal title="Add a weapon" subtitle="What it does while equipped is set once it is added." onClose={() => setAdding(null)}>
|
||||
<Modal title="Add a weapon" subtitle="What it does while equipped is set once it is added." onClose={close}>
|
||||
{error && <ErrorBanner message={error} />}
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="grid grid-cols-2">
|
||||
@@ -220,15 +239,10 @@ export function WeaponsSection({ campaignId, entries, onOpen }: SectionProps<Wea
|
||||
);
|
||||
}
|
||||
|
||||
export function ItemsSection({ campaignId, entries, onOpen }: SectionProps<Item>) {
|
||||
export function ItemsSection({ campaignId, entries, onOpen, formRequested, onFormClosed }: SectionProps<Item>) {
|
||||
const createItem = useCreateItem(campaignId);
|
||||
const addFromLibrary = useAddItemFromLibrary(campaignId);
|
||||
const [adding, setAdding] = useState<Adding>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const open = (next: Adding) => {
|
||||
setError(null);
|
||||
setAdding(next);
|
||||
};
|
||||
const { adding, error, setError, open, close } = useAdding(formRequested, onFormClosed);
|
||||
|
||||
async function handleSubmit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
@@ -245,8 +259,8 @@ export function ItemsSection({ campaignId, entries, onOpen }: SectionProps<Item>
|
||||
effects: [],
|
||||
effectNote: null,
|
||||
});
|
||||
setAdding(null);
|
||||
onOpen({ kind: 'item', id: item.id });
|
||||
close();
|
||||
onOpen({ kind: 'item', id: item.id, isNew: true });
|
||||
} catch (err) {
|
||||
setError(extractErrorMessage(err));
|
||||
}
|
||||
@@ -285,11 +299,11 @@ export function ItemsSection({ campaignId, entries, onOpen }: SectionProps<Item>
|
||||
renderMeta={(i) => [i.category, effectsSummary(i.effects, i.effectNote), i.damageDice].filter(Boolean).join(' · ')}
|
||||
renderDetail={(i) => i.description}
|
||||
error={error}
|
||||
onClose={() => setAdding(null)}
|
||||
onClose={close}
|
||||
/>
|
||||
)}
|
||||
{adding === 'form' && (
|
||||
<Modal title="Add an item" subtitle="What it does while equipped is set once it is added." onClose={() => setAdding(null)}>
|
||||
<Modal title="Add an item" subtitle="What it does while equipped is set once it is added." onClose={close}>
|
||||
{error && <ErrorBanner message={error} />}
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="grid grid-cols-2">
|
||||
@@ -312,15 +326,10 @@ export function ItemsSection({ campaignId, entries, onOpen }: SectionProps<Item>
|
||||
);
|
||||
}
|
||||
|
||||
export function SpellsSection({ campaignId, entries, onOpen }: SectionProps<Spell>) {
|
||||
export function SpellsSection({ campaignId, entries, onOpen, formRequested, onFormClosed }: SectionProps<Spell>) {
|
||||
const createSpell = useCreateSpell(campaignId);
|
||||
const addFromLibrary = useAddSpellFromLibrary(campaignId);
|
||||
const [adding, setAdding] = useState<Adding>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const open = (next: Adding) => {
|
||||
setError(null);
|
||||
setAdding(next);
|
||||
};
|
||||
const { adding, error, setError, open, close } = useAdding(formRequested, onFormClosed);
|
||||
|
||||
async function handleSubmit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
@@ -339,8 +348,8 @@ export function SpellsSection({ campaignId, entries, onOpen }: SectionProps<Spel
|
||||
isStartingOption: fd.get('isStartingOption') === 'on',
|
||||
revives: fd.get('revives') === 'on',
|
||||
});
|
||||
setAdding(null);
|
||||
onOpen({ kind: 'spell', id: spell.id });
|
||||
close();
|
||||
onOpen({ kind: 'spell', id: spell.id, isNew: true });
|
||||
} catch (err) {
|
||||
setError(extractErrorMessage(err));
|
||||
}
|
||||
@@ -381,11 +390,11 @@ export function SpellsSection({ campaignId, entries, onOpen }: SectionProps<Spel
|
||||
}
|
||||
renderDetail={(s) => s.description}
|
||||
error={error}
|
||||
onClose={() => setAdding(null)}
|
||||
onClose={close}
|
||||
/>
|
||||
)}
|
||||
{adding === 'form' && (
|
||||
<Modal title="Add a spell" onClose={() => setAdding(null)}>
|
||||
<Modal title="Add a spell" onClose={close}>
|
||||
{error && <ErrorBanner message={error} />}
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="grid grid-cols-2">
|
||||
@@ -422,15 +431,10 @@ export function SpellsSection({ campaignId, entries, onOpen }: SectionProps<Spel
|
||||
);
|
||||
}
|
||||
|
||||
export function MonstersSection({ campaignId, entries, onOpen }: SectionProps<Monster>) {
|
||||
export function MonstersSection({ campaignId, entries, onOpen, formRequested, onFormClosed }: SectionProps<Monster>) {
|
||||
const createMonster = useCreateMonster(campaignId);
|
||||
const addFromLibrary = useAddMonsterFromLibrary(campaignId);
|
||||
const [adding, setAdding] = useState<Adding>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const open = (next: Adding) => {
|
||||
setError(null);
|
||||
setAdding(next);
|
||||
};
|
||||
const { adding, error, setError, open, close } = useAdding(formRequested, onFormClosed);
|
||||
|
||||
return (
|
||||
<SectionCard
|
||||
@@ -461,11 +465,11 @@ export function MonstersSection({ campaignId, entries, onOpen }: SectionProps<Mo
|
||||
}
|
||||
renderDetail={(m) => [m.specialTrait, m.notes].filter(Boolean).join(' — ') || null}
|
||||
error={error}
|
||||
onClose={() => setAdding(null)}
|
||||
onClose={close}
|
||||
/>
|
||||
)}
|
||||
{adding === 'form' && (
|
||||
<Modal title="Add a monster" wide onClose={() => setAdding(null)}>
|
||||
<Modal title="Add a monster" wide onClose={close}>
|
||||
{error && <ErrorBanner message={error} />}
|
||||
<MonsterForm
|
||||
saving={createMonster.isPending}
|
||||
@@ -473,8 +477,8 @@ export function MonstersSection({ campaignId, entries, onOpen }: SectionProps<Mo
|
||||
setError(null);
|
||||
try {
|
||||
const monster = await createMonster.mutateAsync(stats);
|
||||
setAdding(null);
|
||||
onOpen({ kind: 'monster', id: monster.id });
|
||||
close();
|
||||
onOpen({ kind: 'monster', id: monster.id, isNew: true });
|
||||
} catch (err) {
|
||||
setError(extractErrorMessage(err));
|
||||
throw err;
|
||||
@@ -487,14 +491,9 @@ export function MonstersSection({ campaignId, entries, onOpen }: SectionProps<Mo
|
||||
);
|
||||
}
|
||||
|
||||
export function NpcsSection({ campaignId, entries, onOpen }: SectionProps<Npc>) {
|
||||
export function NpcsSection({ campaignId, entries, onOpen, formRequested, onFormClosed }: SectionProps<Npc>) {
|
||||
const createNpc = useCreateNpc(campaignId);
|
||||
const [adding, setAdding] = useState<Adding>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const open = (next: Adding) => {
|
||||
setError(null);
|
||||
setAdding(next);
|
||||
};
|
||||
const { adding, error, setError, open, close } = useAdding(formRequested, onFormClosed);
|
||||
|
||||
async function handleSubmit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
@@ -514,8 +513,8 @@ export function NpcsSection({ campaignId, entries, onOpen }: SectionProps<Npc>)
|
||||
attackBonus: num('attackBonus'),
|
||||
damageDice: text('damageDice'),
|
||||
});
|
||||
setAdding(null);
|
||||
onOpen({ kind: 'npc', id: npc.id });
|
||||
close();
|
||||
onOpen({ kind: 'npc', id: npc.id, isNew: true });
|
||||
} catch (err) {
|
||||
setError(extractErrorMessage(err));
|
||||
}
|
||||
@@ -545,7 +544,7 @@ export function NpcsSection({ campaignId, entries, onOpen }: SectionProps<Npc>)
|
||||
onOpen={(id) => onOpen({ kind: 'npc', id })}
|
||||
/>
|
||||
{adding === 'form' && (
|
||||
<Modal title="Add an NPC" subtitle="Only the name is needed. What they sell is set once they are added." onClose={() => setAdding(null)}>
|
||||
<Modal title="Add an NPC" subtitle="Only the name is needed. What they sell is set once they are added." onClose={close}>
|
||||
{error && <ErrorBanner message={error} />}
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="grid grid-cols-2">
|
||||
|
||||
Reference in New Issue
Block a user