This commit is contained in:
Morten Olsen
2022-05-02 14:26:11 +02:00
commit d83a4aebc7
77 changed files with 16638 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
import { useAsyncCallback } from "#/hooks/async";
import { Task } from "#/types/task";
import { set } from "date-fns";
import { useContext, useMemo } from "react"
import { useDate } from "../calendar";
import { useTasks } from "../tasks";
import { AgendaContextContext } from "./context"
const toToday = (today: Date, target: Date) => set(target, {
year: today.getFullYear(),
month: today.getMonth(),
date: today.getDate(),
})
export const useAgendaContext = () => {
const { contexts } = useContext(AgendaContextContext);
return contexts;
}
export const useSetAgendaContext = () => {
const { set } = useContext(AgendaContextContext);
const result = useAsyncCallback(set, [set]);
return result;
}
export const useTasksWithContext = () => {
const { all } = useTasks();
const date = useDate();
const contexts = useAgendaContext();
const withContext = useMemo<(Task & { enabled: boolean })[]>(
() => all.map((task) => {
const context = contexts[task.id];
if (!context) {
return { ...task, enabled: true };
}
return {
...task,
locations: context.locations?.length || 0 > 0 ? context.locations : task.locations,
start: {
min: context.startMin ? toToday(date, context.startMin) : task.start.min,
max: context.startMax ? toToday(date, context.startMax) : task.start.max,
},
duration: {
...task.duration,
min: context.duration || task.duration.min,
},
count: context.count,
enabled: typeof context.enabled === 'undefined' ? true : context.enabled,
}
}),
[all, contexts],
);
return withContext;
}