feat: schedule list item action menu (#230)
* 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>
This commit is contained in:
37
src/pages/background/lib/handleDuplicate.ts
Normal file
37
src/pages/background/lib/handleDuplicate.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
|
||||
|
||||
/**
|
||||
* Duplicates a new schedule with the given name.
|
||||
* Assumes that each schedule has a unique name.
|
||||
* @param scheduleName the name of the schedule to handle duplication for
|
||||
* @param schedules the list of UserSchedules to find existing names
|
||||
* @returns the new name for the schedule, of the form `{baseName}({index})`
|
||||
*/
|
||||
export default async function handleDuplicate(scheduleName: string): Promise<string> {
|
||||
const schedules = await UserScheduleStore.get('schedules');
|
||||
|
||||
// No point in checking for duplicates if the name is unique
|
||||
if (schedules.find(schedule => schedule.name === scheduleName) === undefined) {
|
||||
return scheduleName;
|
||||
}
|
||||
|
||||
// Regex for matching `{baseName}({index})`, where match[1] = baseName, match[2] = (index)
|
||||
const regex = /^(.+?)(\(\d+\))?$/;
|
||||
|
||||
// Extract base name and existing index
|
||||
const match = scheduleName.match(regex);
|
||||
const baseName = match && match[1] ? match[1] : scheduleName;
|
||||
|
||||
// Extract number from parentheses and increment
|
||||
let index = match && match[2] ? parseInt(match[2].slice(1, -1), 10) + 1 : 1;
|
||||
|
||||
let newName: string;
|
||||
|
||||
// Increment until an unused index is found
|
||||
do {
|
||||
newName = `${baseName} (${index++})`;
|
||||
// eslint-disable-next-line @typescript-eslint/no-loop-func
|
||||
} while (schedules.find(schedule => schedule.name === newName));
|
||||
|
||||
return newName;
|
||||
}
|
||||
Reference in New Issue
Block a user