Co-authored-by: doprz <52579214+doprz@users.noreply.github.com> Co-authored-by: Samuel Gunter <29130894+Samathingamajig@users.noreply.github.com>
50 lines
1.9 KiB
TypeScript
50 lines
1.9 KiB
TypeScript
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
|
|
import type { UserSchedule } from '@shared/types/UserSchedule';
|
|
import { generateRandomId } from '@shared/util/random';
|
|
import type { Serialized } from 'chrome-extension-toolkit';
|
|
|
|
/**
|
|
* Creates a new schedule with the given name
|
|
*
|
|
* @param scheduleName - The name of the schedule to create
|
|
* @returns Undefined if successful, otherwise an error message
|
|
*/
|
|
export default async function createSchedule(scheduleName: string) {
|
|
const schedules = await UserScheduleStore.get('schedules');
|
|
// get the number of schedules that either have the same name or have the same name with a number appended (e.g. "New Schedule (1)")
|
|
// this way we can prevent duplicate schedule names and increment the number if necessary
|
|
|
|
// Regex to match schedule names that follow the pattern "ScheduleName" or "ScheduleName (1)", "ScheduleName (2)", etc.
|
|
const regex = new RegExp(`^${scheduleName}( \\(\\d+\\))?$`);
|
|
|
|
// Find how many schedules match the base name or follow the pattern with a number
|
|
const count = schedules.filter(s => regex.test(s.name)).length;
|
|
|
|
// If any matches are found, append the next number to the schedule name
|
|
let name = scheduleName;
|
|
if (count > 0) {
|
|
name = `${scheduleName} (${count})`;
|
|
}
|
|
|
|
const newSchedule: Serialized<UserSchedule> = {
|
|
id: generateRandomId(),
|
|
name,
|
|
courses: [],
|
|
hours: 0,
|
|
updatedAt: Date.now(),
|
|
};
|
|
schedules.push(newSchedule);
|
|
|
|
await UserScheduleStore.set('schedules', schedules);
|
|
|
|
// Automatically switch to the new schedule
|
|
await UserScheduleStore.set('activeIndex', schedules.length - 1);
|
|
|
|
// If there is only one schedule, set the active index to the new schedule
|
|
if (schedules.length <= 1) {
|
|
await UserScheduleStore.set('activeIndex', 0);
|
|
}
|
|
|
|
return newSchedule.id;
|
|
}
|