feat: list reordering (#154)

This commit is contained in:
Razboy20
2024-03-13 16:45:32 -05:00
committed by GitHub
parent 91f62e1943
commit 038ebaa268
7 changed files with 187 additions and 98 deletions

View File

@@ -39,10 +39,12 @@ export default function useSchedules(): [active: UserSchedule | null, schedules:
useEffect(() => {
const l1 = UserScheduleStore.listen('schedules', ({ newValue }) => {
setSchedules(newValue.map(s => new UserSchedule(s)));
schedulesCache = newValue.map(s => new UserSchedule(s));
setSchedules(schedulesCache);
});
const l2 = UserScheduleStore.listen('activeIndex', ({ newValue }) => {
activeIndexCache = newValue;
setActiveIndex(newValue);
});
@@ -55,11 +57,23 @@ export default function useSchedules(): [active: UserSchedule | null, schedules:
// recompute active schedule on a schedule/index change
useEffect(() => {
setActiveSchedule(schedules[activeIndex]);
});
}, [activeIndex, schedules]);
return [activeSchedule, schedules];
}
export function getActiveSchedule(): UserSchedule | null {
return schedulesCache[activeIndexCache] || null;
}
export async function replaceSchedule(oldSchedule: UserSchedule, newSchedule: UserSchedule) {
const schedules = await UserScheduleStore.get('schedules');
const oldIndex = schedules.findIndex(s => s.name === oldSchedule.name);
schedules[oldIndex] = newSchedule;
await UserScheduleStore.set('schedules', schedules);
console.log('schedule replaced');
}
/**
* Switches the active schedule to the one with the specified name.
* @param name - The name of the schedule to switch to.