This commit is contained in:
2026-04-21 18:46:41 +02:00
parent 99ed8bae59
commit cb7555590f
6 changed files with 265 additions and 110 deletions

View File

@@ -0,0 +1,70 @@
import {
createContext,
ReactNode,
useContext,
useMemo,
useState,
} from "react";
export interface BirthdayEntry {
id: string;
name: string;
date: string;
}
interface AddBirthdayInput {
name: string;
date: string;
}
interface BirthdaysContextValue {
birthdays: BirthdayEntry[];
addBirthday: (input: AddBirthdayInput) => void;
}
const BirthdaysContext = createContext<BirthdaysContextValue | undefined>(
undefined
);
export function BirthdaysProvider({ children }: { children: ReactNode }) {
const [birthdays, setBirthdays] = useState<BirthdayEntry[]>([]);
const value = useMemo(
() => ({
birthdays,
addBirthday: ({ name, date }: AddBirthdayInput) => {
const trimmedName = name.trim();
if (!trimmedName) {
return;
}
setBirthdays((currentBirthdays) => [
...currentBirthdays,
{
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
name: trimmedName,
date,
},
]);
},
}),
[birthdays]
);
return (
<BirthdaysContext.Provider value={value}>
{children}
</BirthdaysContext.Provider>
);
}
export function useBirthdays() {
const context = useContext(BirthdaysContext);
if (!context) {
throw new Error("useBirthdays must be used within a BirthdaysProvider");
}
return context;
}