* feat: action menu for schedule list item * feat: schedule action menu functionality * feat: dialog provider popups for delete * feat: duplicate schedule satiesfies type * refactor: change non-null assertion to early return for rename schedule * refactor: move schedule list item dialog providers to util file * style: run prettier * chore: inline object with satisfies operator * fix: border issues * style: change popups to match figma * fix: update import for schedule list item dialog providers * style: change dropdown text style to match figma * fix: add back dialog context * style: rounded edges when hovering over action + soften border color * chore: cleanup and improve styling * fix: dialog in popupmain --------- Co-authored-by: doprz <52579214+doprz@users.noreply.github.com> Co-authored-by: Razboy20 <razboy20@gmail.com>
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
|
|
|
|
import handleDuplicate from './handleDuplicate';
|
|
|
|
/**
|
|
* Renames a schedule with the specified name to a new name.
|
|
* @param scheduleId - The id of the schedule to be renamed.
|
|
* @param newName - The new name for the schedule.
|
|
* @returns A promise that resolves to the new name if successful, otherwise undefined.
|
|
*/
|
|
export default async function renameSchedule(scheduleId: string, newName: string): Promise<string | undefined> {
|
|
const schedules = await UserScheduleStore.get('schedules');
|
|
|
|
const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
|
|
if (scheduleIndex === -1) {
|
|
return undefined;
|
|
}
|
|
const schedule = schedules[scheduleIndex];
|
|
if (schedule === undefined) {
|
|
return undefined;
|
|
}
|
|
|
|
// if old name is of the form `{baseName}{index}` and newName === baseName, do nothing.
|
|
const oldName = schedule.name;
|
|
const regex = /^(.+?)(\(\d+\))?$/;
|
|
const match = oldName?.match(regex);
|
|
const baseName = match?.[1] ?? '';
|
|
const baseNameOfNewName = newName.match(regex)?.[1];
|
|
|
|
if (baseName === baseNameOfNewName) {
|
|
return oldName;
|
|
}
|
|
|
|
const updatedName = await handleDuplicate(newName);
|
|
|
|
schedule.name = updatedName;
|
|
schedule.updatedAt = Date.now();
|
|
|
|
await UserScheduleStore.set('schedules', schedules);
|
|
return newName;
|
|
}
|