From 009de628285ce9c6571e492f8e3f52cdeeed4459 Mon Sep 17 00:00:00 2001 From: Samuel Gunter <29130894+Samathingamajig@users.noreply.github.com> Date: Mon, 20 Jan 2025 23:18:00 -0600 Subject: [PATCH] feat(settings): add option to always open calendar in new tab (#488) * feat(settings): add option to always open calendar in new tab * fix: await setting --------- Co-authored-by: doprz <52579214+doprz@users.noreply.github.com> --- .../handler/calendarBackgroundHandler.ts | 6 ++-- src/shared/storage/OptionsStore.ts | 5 ++++ src/views/components/settings/Settings.tsx | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/pages/background/handler/calendarBackgroundHandler.ts b/src/pages/background/handler/calendarBackgroundHandler.ts index 4181faad..8e19ccd3 100644 --- a/src/pages/background/handler/calendarBackgroundHandler.ts +++ b/src/pages/background/handler/calendarBackgroundHandler.ts @@ -2,6 +2,8 @@ import type { TabWithId } from '@background/util/openNewTab'; import openNewTab from '@background/util/openNewTab'; import { tabs } from '@shared/messages'; import type { CalendarBackgroundMessages } from '@shared/messages/CalendarMessages'; +import { OptionsStore } from '@shared/storage/OptionsStore'; +import { CRX_PAGES } from '@shared/types/CRXPages'; import type { MessageHandler } from 'chrome-extension-toolkit'; const getAllTabInfos = async () => { @@ -21,13 +23,13 @@ const getAllTabInfos = async () => { const calendarBackgroundHandler: MessageHandler = { async switchToCalendarTab({ data, sendResponse }) { const { uniqueId } = data; - const calendarUrl = chrome.runtime.getURL(`calendar.html`); + const calendarUrl = chrome.runtime.getURL(CRX_PAGES.CALENDAR); const allTabs = await getAllTabInfos(); const openCalendarTabInfo = allTabs.find(tab => tab.url?.startsWith(calendarUrl)); - if (openCalendarTabInfo !== undefined) { + if (openCalendarTabInfo !== undefined && !(await OptionsStore.get('alwaysOpenCalendarInNewTab'))) { const tabid = openCalendarTabInfo.tab.id; await chrome.tabs.update(tabid, { active: true }); diff --git a/src/shared/storage/OptionsStore.ts b/src/shared/storage/OptionsStore.ts index 605b86a7..9e51bdd5 100644 --- a/src/shared/storage/OptionsStore.ts +++ b/src/shared/storage/OptionsStore.ts @@ -18,6 +18,9 @@ export interface IOptionsStore { /** whether we should automatically refresh the data for the waitlist, course status, and other info with the latest data from UT's site */ enableDataRefreshing: boolean; + + /** whether we should open the calendar in a new tab; default is to focus an existing calendar tab */ + alwaysOpenCalendarInNewTab: boolean; } export const OptionsStore = createSyncStore({ @@ -26,6 +29,7 @@ export const OptionsStore = createSyncStore({ enableHighlightConflicts: true, enableScrollToLoad: true, enableDataRefreshing: true, + alwaysOpenCalendarInNewTab: false, }); /** @@ -40,6 +44,7 @@ export const initSettings = async () => enableHighlightConflicts: await OptionsStore.get('enableHighlightConflicts'), enableScrollToLoad: await OptionsStore.get('enableScrollToLoad'), enableDataRefreshing: await OptionsStore.get('enableDataRefreshing'), + alwaysOpenCalendarInNewTab: await OptionsStore.get('alwaysOpenCalendarInNewTab'), }) satisfies IOptionsStore; // Clothing retailer right diff --git a/src/views/components/settings/Settings.tsx b/src/views/components/settings/Settings.tsx index 1996a08c..47e8765f 100644 --- a/src/views/components/settings/Settings.tsx +++ b/src/views/components/settings/Settings.tsx @@ -90,6 +90,7 @@ export default function Settings(): JSX.Element { const [highlightConflicts, setHighlightConflicts] = useState(false); const [loadAllCourses, setLoadAllCourses] = useState(false); const [_enableDataRefreshing, setEnableDataRefreshing] = useState(false); + const [calendarNewTab, setCalendarNewTab] = useState(false); const showMigrationDialog = useMigrationDialog(); @@ -122,12 +123,14 @@ export default function Settings(): JSX.Element { enableHighlightConflicts, enableScrollToLoad, enableDataRefreshing, + alwaysOpenCalendarInNewTab, } = await initSettings(); setEnableCourseStatusChips(enableCourseStatusChips); setShowTimeLocation(enableTimeAndLocationInPopup); setHighlightConflicts(enableHighlightConflicts); setLoadAllCourses(enableScrollToLoad); setEnableDataRefreshing(enableDataRefreshing); + setCalendarNewTab(alwaysOpenCalendarInNewTab); }; fetchGitHubStats(); @@ -167,6 +170,11 @@ export default function Settings(): JSX.Element { // console.log('enableDataRefreshing', newValue); }); + const l6 = OptionsStore.listen('alwaysOpenCalendarInNewTab', async ({ newValue }) => { + setCalendarNewTab(newValue); + // console.log('alwaysOpenCalendarInNewTab', newValue); + }); + // Remove listeners when the component is unmounted return () => { OptionsStore.removeListener(l1); @@ -174,6 +182,7 @@ export default function Settings(): JSX.Element { OptionsStore.removeListener(l3); OptionsStore.removeListener(l4); OptionsStore.removeListener(l5); + OptionsStore.removeListener(l6); window.removeEventListener('keydown', handleKeyPress); }; @@ -431,6 +440,27 @@ export default function Settings(): JSX.Element { +
+
+ + Always Open Calendar in New Tab + +

+ Always opens the calendar view in a new tab when navigating to the calendar + page. May prevent issues where the calendar refuses to open. +

+
+ { + setCalendarNewTab(!calendarNewTab); + OptionsStore.set('alwaysOpenCalendarInNewTab', !calendarNewTab); + }} + /> +
+ + +