Skip to content

Commit 1122ce7

Browse files
committed
Refactor profile management functions: streamline profile validation and UI reset logic
1 parent 882721d commit 1122ce7

File tree

6 files changed

+98
-213
lines changed

6 files changed

+98
-213
lines changed

modules/popup-page-modules-promptShare.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ document.addEventListener('DOMContentLoaded', () => {
9393
// Helper function to manage settings visibility based on both toggle state and collapsible state
9494
function updateSettingsVisibility() {
9595
const isModuleExpanded = crossChatModule.classList.contains('expanded');
96-
const isToggleEnabled = currentSettings.enabled;
97-
98-
// Only show settings if both the module is expanded AND the toggle is enabled
99-
settingsContainer.style.display = (isModuleExpanded && isToggleEnabled) ? 'block' : 'none';
96+
// The visibility of settings now ONLY depends on whether the section is expanded.
97+
// The toggle's state is reflected by the toggle itself, not by hiding the section.
98+
settingsContainer.style.display = isModuleExpanded ? 'block' : 'none';
10099
}
101100

102101
// --- Observer for Section Expansion ---
@@ -144,7 +143,7 @@ document.addEventListener('DOMContentLoaded', () => {
144143
// --- Event Listeners ---
145144
enableToggle.addEventListener('change', () => {
146145
currentSettings.enabled = enableToggle.checked;
147-
updateSettingsVisibility();
146+
// The toggle now only changes the setting and saves it; it does not control UI visibility.
148147
saveModuleSettings();
149148
});
150149

popup-page-scripts/popup-page-advanced.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ document.addEventListener('DOMContentLoaded', () => {
4848
const site = websiteSelect.value;
4949
try {
5050
// First try to get custom selectors
51-
const response = await new Promise(resolve => {
52-
chrome.runtime.sendMessage({
53-
type: 'getCustomSelectors',
54-
site: site
55-
}, resolve);
51+
const response = await chrome.runtime.sendMessage({
52+
type: 'getCustomSelectors',
53+
site: site,
5654
});
5755

5856
if (response && response.selectors) {
@@ -100,12 +98,10 @@ document.addEventListener('DOMContentLoaded', () => {
10098
}
10199

102100
// Save to storage
103-
const response = await new Promise(resolve => {
104-
chrome.runtime.sendMessage({
105-
type: 'saveCustomSelectors',
106-
site: websiteSelect.value,
107-
selectors: config
108-
}, resolve);
101+
const response = await chrome.runtime.sendMessage({
102+
type: 'saveCustomSelectors',
103+
site: websiteSelect.value,
104+
selectors: config,
109105
});
110106

111107
if (response && response.success) {
@@ -123,12 +119,10 @@ document.addEventListener('DOMContentLoaded', () => {
123119
async function resetSelectors() {
124120
try {
125121
// Remove custom selectors
126-
const response = await new Promise(resolve => {
127-
chrome.runtime.sendMessage({
128-
type: 'saveCustomSelectors',
129-
site: websiteSelect.value,
130-
selectors: null // null means remove
131-
}, resolve);
122+
const response = await chrome.runtime.sendMessage({
123+
type: 'saveCustomSelectors',
124+
site: websiteSelect.value,
125+
selectors: null, // null means remove
132126
});
133127

134128
if (response && response.success) {

popup-page-scripts/popup-page-backup-handler.js

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,14 @@ async function handleImportProfile(event) {
110110
event.target.value = '';
111111
}
112112

113-
// Function to overwrite the existing profile with the parsed profile
114-
async function overwriteCurrentProfile() {
115-
const parsedProfile = window.tempParsedProfile;
116-
if (!parsedProfile) {
117-
logToGUIConsole('No parsed profile available to overwrite.');
118-
return;
119-
}
120-
121-
logToGUIConsole(`Overwriting existing profile "${parsedProfile.PROFILE_NAME}" with imported profile.`);
113+
/**
114+
* Handles the logic for saving a profile (new or overwritten) and updating the UI.
115+
* @param {object} parsedProfile - The profile object to save.
116+
* @param {boolean} isOverwrite - Indicates if this is an overwrite operation.
117+
*/
118+
async function saveAndSwitchToImportedProfile(parsedProfile, isOverwrite) {
119+
const actionText = isOverwrite ? 'overwritten' : 'imported';
120+
const actionVerb = isOverwrite ? 'overwrite' : 'import';
122121

123122
try {
124123
await chrome.runtime.sendMessage({
@@ -127,21 +126,32 @@ async function overwriteCurrentProfile() {
127126
config: parsedProfile
128127
});
129128

130-
// Reload profiles and switch to the imported profile
129+
// Reload profiles and switch to the new/overwritten profile
131130
await loadProfiles();
132131
profileSelect.value = parsedProfile.PROFILE_NAME;
133132
await switchProfile(parsedProfile.PROFILE_NAME);
134133

135134
updateInterface();
136-
logToGUIConsole(`Profile "${parsedProfile.PROFILE_NAME}" imported and overwritten successfully.`);
137-
// Use toast notification instead of alert
138-
showToast(`Profile "${parsedProfile.PROFILE_NAME}" has been overwritten successfully.`, 'success');
135+
logToGUIConsole(`Profile "${parsedProfile.PROFILE_NAME}" ${actionText} successfully.`);
136+
showToast(`Profile "${parsedProfile.PROFILE_NAME}" has been ${actionText} successfully.`, 'success');
139137
} catch (error) {
140138
logToGUIConsole(`Failed to save the imported profile: ${error.message}`);
141-
// Use toast notification instead of alert
142-
showToast('Failed to overwrite the existing profile. Please try again.', 'error');
139+
showToast(`Failed to ${actionVerb} the profile. Please try again.`, 'error');
140+
}
141+
}
142+
143+
// Function to overwrite the existing profile with the parsed profile
144+
async function overwriteCurrentProfile() {
145+
const parsedProfile = window.tempParsedProfile;
146+
if (!parsedProfile) {
147+
logToGUIConsole('No parsed profile available to overwrite.');
148+
return;
143149
}
144150

151+
logToGUIConsole(`Overwriting existing profile "${parsedProfile.PROFILE_NAME}" with imported profile.`);
152+
153+
await saveAndSwitchToImportedProfile(parsedProfile, true);
154+
145155
// Hide the confirmation div
146156
document.getElementById('confirmationDiv').style.display = 'none';
147157

@@ -162,27 +172,7 @@ function cancelImport() {
162172
// Function to import profile directly without confirmation
163173
async function importProfile(parsedProfile) {
164174
logToGUIConsole(`Importing profile "${parsedProfile.PROFILE_NAME}" as a new profile.`);
165-
try {
166-
await chrome.runtime.sendMessage({
167-
type: 'saveConfig',
168-
profileName: parsedProfile.PROFILE_NAME,
169-
config: parsedProfile
170-
});
171-
172-
// Reload profiles and switch to the new profile
173-
await loadProfiles();
174-
profileSelect.value = parsedProfile.PROFILE_NAME;
175-
await switchProfile(parsedProfile.PROFILE_NAME);
176-
177-
updateInterface();
178-
logToGUIConsole(`Profile "${parsedProfile.PROFILE_NAME}" imported successfully.`);
179-
// Use toast notification instead of alert
180-
showToast(`Profile "${parsedProfile.PROFILE_NAME}" has been imported successfully.`, 'success');
181-
} catch (error) {
182-
logToGUIConsole(`Failed to save the imported profile: ${error.message}`);
183-
// Use toast notification instead of alert
184-
showToast('Failed to import the profile. Please try again.', 'error');
185-
}
175+
await saveAndSwitchToImportedProfile(parsedProfile, false);
186176
}
187177

188178
// Attach event listeners after DOM content is loaded
@@ -200,3 +190,4 @@ document.addEventListener('DOMContentLoaded', () => {
200190
document.getElementById('confirmOverwrite').addEventListener('click', overwriteCurrentProfile);
201191
document.getElementById('cancelOverwrite').addEventListener('click', cancelImport);
202192
});
193+

popup-page-scripts/popup-page-profiles.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,40 @@ async function switchProfile(profileName) {
7676

7777

7878
// -------------------------
79-
// 3. Add New Empty Profile
79+
// 3. Profile Actions (Add, Copy, Delete)
8080
// -------------------------
8181

8282
/**
83-
* Creates a new empty profile using minimalDefaultConfig.
84-
* @param {string} profileName - The name of the new profile.
83+
* Validates a profile name against being empty or already existing.
84+
* @param {string} profileName - The name to validate.
85+
* @param {string} actionType - The type of action (e.g., 'creation', 'copy') for logging.
86+
* @returns {{isValid: boolean, name: string|null}} - An object indicating if the name is valid and the trimmed name.
8587
*/
86-
async function addNewEmptyProfile(profileName) {
87-
// Trim whitespace and validate profile name
88+
function validateProfileName(profileName, actionType) {
8889
const trimmedProfileName = profileName.trim();
8990
if (trimmedProfileName === "") {
9091
showToast('Profile name cannot be empty.', 'error');
91-
logToGUIConsole('Profile creation failed: Empty name provided.');
92-
return false;
92+
logToGUIConsole(`Profile ${actionType} failed: Empty name provided.`);
93+
return { isValid: false, name: null };
9394
}
9495

95-
// Check if profile name already exists
9696
const existingProfiles = Array.from(profileSelect.options).map(option => option.value);
9797
if (existingProfiles.includes(trimmedProfileName)) {
9898
showToast('A profile with this name already exists.', 'error');
99-
logToGUIConsole(`Profile creation failed: "${trimmedProfileName}" already exists.`);
100-
return false;
99+
logToGUIConsole(`Profile ${actionType} failed: "${trimmedProfileName}" already exists.`);
100+
return { isValid: false, name: null };
101101
}
102+
return { isValid: true, name: trimmedProfileName };
103+
}
104+
105+
/**
106+
* Creates a new empty profile.
107+
* @param {string} profileName - The name of the new profile.
108+
*/
109+
async function addNewEmptyProfile(profileName) {
110+
const validation = validateProfileName(profileName, 'creation');
111+
if (!validation.isValid) return false;
112+
const trimmedProfileName = validation.name;
102113

103114
try {
104115
// Initialize new profile with minimal settings
@@ -123,29 +134,14 @@ async function addNewEmptyProfile(profileName) {
123134
}
124135

125136
// -------------------------
126-
// 4. Copy Current Profile
127-
// -------------------------
128-
129137
/**
130-
* Copies the current profile to a new profile with a specified name.
131-
* @param {string} newProfileName - The name of the new profile.
138+
* Copies the current profile to a new profile.
139+
* @param {string} profileName - The name of the new profile.
132140
*/
133-
async function copyCurrentProfile(newProfileName) {
134-
// Trim whitespace and validate profile name
135-
const trimmedProfileName = newProfileName.trim();
136-
if (trimmedProfileName === "") {
137-
showToast('Profile name cannot be empty.', 'error');
138-
logToGUIConsole('Profile copy failed: Empty name provided.');
139-
return false;
140-
}
141-
142-
// Check if profile name already exists
143-
const existingProfiles = Array.from(profileSelect.options).map(option => option.value);
144-
if (existingProfiles.includes(trimmedProfileName)) {
145-
showToast('A profile with this name already exists.', 'error');
146-
logToGUIConsole(`Profile copy failed: "${trimmedProfileName}" already exists.`);
147-
return false;
148-
}
141+
async function copyCurrentProfile(profileName) {
142+
const validation = validateProfileName(profileName, 'copy');
143+
if (!validation.isValid) return false;
144+
const trimmedProfileName = validation.name;
149145

150146
try {
151147
// Deep copy current profile settings
@@ -206,3 +202,4 @@ async function deleteCurrentProfile() {
206202
}
207203
}
208204

205+

popup-page-scripts/popup-page-script.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ function updateInterface() {
163163
profileSelect.value = currentProfile.PROFILE_NAME;
164164
}
165165

166+
/**
167+
* Resets and hides the profile action UIs (add/copy) and restores the default view.
168+
*/
169+
function resetProfileActionsUI() {
170+
// Hide input containers and clear values/errors
171+
addProfileInput.value = '';
172+
addProfileInput.style.borderColor = '';
173+
addProfileInput.classList.remove('input-error');
174+
addProfileContainer.style.display = 'none';
175+
176+
copyProfileInput.value = '';
177+
copyProfileInput.style.borderColor = '';
178+
copyProfileInput.classList.remove('input-error');
179+
copyProfileContainer.style.display = 'none';
180+
181+
// Show the main action buttons
182+
addProfileButton.style.display = 'inline-block';
183+
copyProfileButton.style.display = 'inline-block';
184+
deleteProfileButton.style.display = 'inline-block';
185+
186+
// Unlock the profile selector
187+
profileSelect.disabled = false;
188+
currentProfileLabel.style.display = 'none';
189+
}
166190
// -------------------------
167191
// 9. Event Listeners
168192
// -------------------------
@@ -223,16 +247,7 @@ document.addEventListener('DOMContentLoaded', () => {
223247
const success = await addNewEmptyProfile(profileName);
224248

225249
if (success) {
226-
// Reset and hide input
227-
addProfileInput.value = '';
228-
addProfileInput.style.borderColor = '';
229-
addProfileContainer.style.display = 'none';
230-
// Show both "Add Profile" and "Duplicate Profile" buttons
231-
addProfileButton.style.display = 'inline-block';
232-
copyProfileButton.style.display = 'inline-block';
233-
deleteProfileButton.style.display = 'inline-block'; // Show delete button after add
234-
profileSelect.disabled = false; // Unlock profile selector
235-
currentProfileLabel.style.display = 'none';
250+
resetProfileActionsUI();
236251
} else {
237252
// On failure (e.g., duplicate name), keep UI open for correction
238253
addProfileInput.classList.add('input-error');
@@ -255,16 +270,7 @@ document.addEventListener('DOMContentLoaded', () => {
255270
const success = await copyCurrentProfile(newProfileName);
256271

257272
if (success) {
258-
// Reset and hide input
259-
copyProfileInput.value = '';
260-
copyProfileInput.style.borderColor = '';
261-
copyProfileContainer.style.display = 'none';
262-
// Show both "Add Profile" and "Duplicate Profile" buttons
263-
copyProfileButton.style.display = 'inline-block';
264-
addProfileButton.style.display = 'inline-block';
265-
deleteProfileButton.style.display = 'inline-block'; // Show delete button after copy
266-
profileSelect.disabled = false; // Unlock profile selector
267-
currentProfileLabel.style.display = 'none';
273+
resetProfileActionsUI();
268274
} else {
269275
// On failure (e.g., duplicate name), keep UI open for correction
270276
copyProfileInput.classList.add('input-error');
@@ -277,22 +283,12 @@ document.addEventListener('DOMContentLoaded', () => {
277283

278284
// Cancel Add Profile Button Click
279285
cancelAddProfileButton.addEventListener('click', () => {
280-
addProfileContainer.style.display = 'none';
281-
addProfileButton.style.display = 'inline-block';
282-
copyProfileButton.style.display = 'inline-block';
283-
deleteProfileButton.style.display = 'inline-block'; // Show delete button
284-
profileSelect.disabled = false; // Unlock profile selector
285-
currentProfileLabel.style.display = 'none';
286+
resetProfileActionsUI();
286287
});
287288

288289
// Cancel Copy Profile Button Click
289290
cancelCopyProfileButton.addEventListener('click', () => {
290-
copyProfileContainer.style.display = 'none';
291-
addProfileButton.style.display = 'inline-block';
292-
copyProfileButton.style.display = 'inline-block';
293-
deleteProfileButton.style.display = 'inline-block'; // Show delete button
294-
profileSelect.disabled = false; // Unlock profile selector
295-
currentProfileLabel.style.display = 'none';
291+
resetProfileActionsUI();
296292
});
297293

298294
// Button management

0 commit comments

Comments
 (0)