Skip to content

Commit 34d838a

Browse files
committed
feat(popup): lock settings modification during active session and sync UI states
1 parent 026ae5b commit 34d838a

4 files changed

Lines changed: 51 additions & 28 deletions

File tree

src/i18n/locales/ar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const ar: LocaleStrings = {
1616
reason_time: 'السبب: انتهاء الوقت المسموح',
1717
reason_bypass: 'السبب: محاولة تحايل',
1818
lockedBtn: 'مقفول!',
19+
sessionActiveBtn: 'جلسة نشطة!',
1920
selectPlatform: 'اختر المنصة...',
2021
youtubeShorts: 'يوتيوب شورتس',
2122
},

src/i18n/locales/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const en: LocaleStrings = {
1616
reason_time: 'Triggered by: Time Limit',
1717
reason_bypass: 'Triggered by: Anti-Bypass System',
1818
lockedBtn: 'Locked!',
19+
sessionActiveBtn: 'Session Active!',
1920
selectPlatform: 'Select Platform...',
2021
youtubeShorts: 'YouTube Shorts',
2122
},

src/i18n/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface LocaleStrings {
1616
reason_time: string;
1717
reason_bypass: string;
1818
lockedBtn: string;
19+
sessionActiveBtn: string;
1920
selectPlatform: string;
2021
youtubeShorts: string;
2122
};

src/ui/popup/index.ts

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function updateUITexts() {
4848
document.body.dir = i18n.language === 'ar' ? 'rtl' : 'ltr';
4949
updateProgressBar();
5050
}
51+
5152
function updateProgressBar() {
5253
const isLimitEnabled = toggleLimit.checked;
5354
const isTimeEnabled = toggleTime.checked;
@@ -98,6 +99,40 @@ function updateProgressBar() {
9899
}
99100
}
100101

102+
function syncUIState(
103+
isBlocked: boolean,
104+
count: number,
105+
timeMs: number,
106+
isLimitEnabled: boolean,
107+
isTimeEnabled: boolean,
108+
) {
109+
const timeSpentMins = Math.floor(timeMs / (60 * 1000));
110+
const hasConsumed = count > 0 || timeSpentMins > 0;
111+
112+
if (isBlocked) {
113+
limitInput.disabled = true;
114+
timeInput.disabled = true;
115+
toggleLimit.disabled = true;
116+
toggleTime.disabled = true;
117+
btnSave.disabled = true;
118+
btnSave.textContent = i18n.t.popup.lockedBtn;
119+
} else if (hasConsumed) {
120+
limitInput.disabled = true;
121+
timeInput.disabled = true;
122+
toggleLimit.disabled = true;
123+
toggleTime.disabled = true;
124+
btnSave.disabled = true;
125+
btnSave.textContent = i18n.t.popup.sessionActiveBtn;
126+
} else {
127+
limitInput.disabled = !isLimitEnabled;
128+
timeInput.disabled = !isTimeEnabled;
129+
toggleLimit.disabled = false;
130+
toggleTime.disabled = false;
131+
btnSave.disabled = false;
132+
btnSave.textContent = i18n.t.popup.saveBtn;
133+
}
134+
}
135+
101136
function lockUIForSelection() {
102137
limitInput.value = '';
103138
timeInput.value = '';
@@ -139,24 +174,7 @@ async function loadPlatformData() {
139174
toggleLimit.checked = isLimitEnabled;
140175
toggleTime.checked = isTimeEnabled;
141176

142-
if (isBlocked) {
143-
limitInput.disabled = true;
144-
timeInput.disabled = true;
145-
toggleLimit.disabled = true;
146-
toggleTime.disabled = true;
147-
btnSave.disabled = true;
148-
btnSave.textContent = i18n.t.popup.lockedBtn;
149-
btnSave.classList.add('warning');
150-
} else {
151-
limitInput.disabled = !isLimitEnabled;
152-
timeInput.disabled = !isTimeEnabled;
153-
toggleLimit.disabled = false;
154-
toggleTime.disabled = false;
155-
btnSave.disabled = false;
156-
btnSave.textContent = i18n.t.popup.saveBtn;
157-
btnSave.classList.remove('warning');
158-
}
159-
177+
syncUIState(isBlocked, count, timeSpentMs, isLimitEnabled, isTimeEnabled);
160178
updateProgressBar();
161179
}
162180

@@ -322,15 +340,13 @@ chrome.storage.onChanged.addListener((changes) => {
322340

323341
if (checkLock) {
324342
void storage.isCurrentlyBlocked(activePlatform).then((isBlocked) => {
325-
if (isBlocked && !btnSave.disabled) {
326-
limitInput.disabled = true;
327-
timeInput.disabled = true;
328-
toggleLimit.disabled = true;
329-
toggleTime.disabled = true;
330-
btnSave.disabled = true;
331-
btnSave.textContent = i18n.t.popup.lockedBtn;
332-
btnSave.classList.add('warning');
333-
}
343+
syncUIState(
344+
isBlocked,
345+
currentConsumedCount,
346+
currentConsumedTimeMs,
347+
toggleLimit.checked,
348+
toggleTime.checked,
349+
);
334350
});
335351
}
336352
});
@@ -340,7 +356,11 @@ chrome.storage.onChanged.addListener((changes) => {
340356
if (!activePlatform) return;
341357

342358
const isBlocked = await storage.isCurrentlyBlocked(activePlatform);
343-
if (isBlocked) {
359+
const count = await storage.getCount(activePlatform);
360+
const timeSpentMs = await storage.getTimeSpent(activePlatform);
361+
const hasConsumed = count > 0 || Math.floor(timeSpentMs / (60 * 1000)) > 0;
362+
363+
if (isBlocked || hasConsumed) {
344364
toggleLimit.disabled = true;
345365
toggleTime.disabled = true;
346366
limitInput.disabled = true;

0 commit comments

Comments
 (0)