///
import {
action,
computed,
Default,
NAME,
pattern,
Stream,
UI,
type VNode,
Writable,
} from "commontools";
/** Input for creating a new event detail piece */
interface EventDetailInput {
title?: Writable>;
date?: Writable>;
time?: Writable>;
notes?: Writable>;
}
/**
* Output shape of the event piece - this is what gets stored in calendars
* #event
*/
interface EventDetailOutput {
[NAME]: string;
[UI]: VNode;
title: string;
date: string;
time: string;
notes: string;
summary: string;
setTitle: Stream<{ title: string }>;
setDate: Stream<{ date: string }>;
setTime: Stream<{ time: string }>;
setNotes: Stream<{ notes: string }>;
}
// Re-export the Output type as EventPiece for use in collections
export type EventPiece = EventDetailOutput;
export default pattern(
({ title, date, time, notes }) => {
const setTitle = action(({ title: newTitle }: { title: string }) => {
title.set(newTitle);
});
const setDate = action(({ date: newDate }: { date: string }) => {
date.set(newDate);
});
const setTime = action(({ time: newTime }: { time: string }) => {
time.set(newTime);
});
const setNotes = action(({ notes: newNotes }: { notes: string }) => {
notes.set(newNotes);
});
return {
[NAME]: computed(() => `Event: ${title.get()}`),
[UI]: (
{title || "New Event"}
),
title,
date,
time,
notes,
summary: computed(() => {
const t = title.get();
const d = date.get();
const tm = time.get();
return `${t}${d ? ` on ${d}` : ""}${tm ? ` at ${tm}` : ""}${
notes.get() ? `: ${notes.get().slice(0, 150)}` : ""
}`;
}),
setTitle,
setDate,
setTime,
setNotes,
};
},
);