diff --git a/src/background.ts b/src/background.ts index e0e6c73..027c7de 100644 --- a/src/background.ts +++ b/src/background.ts @@ -4,6 +4,7 @@ import { DEFAULT_GROUP, DEFAULT_PROMPT, getStorage, setStorage } from "./utils"; chrome.runtime.onInstalled.addListener((details) => { if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) { setStorage("isOn", true); + setStorage("isAutoPosition", false); setStorage("types", DEFAULT_GROUP); setStorage("prompt", DEFAULT_PROMPT); } @@ -120,6 +121,17 @@ async function processTabAndGroup(tab: chrome.tabs.Tab, types: any) { if (groupId !== undefined) { // Existing group is valid, add tab to this group. await chrome.tabs.group({ tabIds: tab.id, groupId }); + + const isAutoPosition = await getStorage("isAutoPosition"); + + const currentWindowTabs = await chrome.tabs.query({ + windowId: tab.windowId, + }); + const isRightmost = + tab.index == Math.max(...currentWindowTabs.map((tab) => tab.index)); + if (isAutoPosition && isRightmost) { + await chrome.tabGroups.move(groupId, { index: -1 }); + } } else { // If no valid group is found, create a new group for this type await createGroupWithTitle(tab.id, type); diff --git a/src/popup.tsx b/src/popup.tsx index 3db5383..70b17c4 100644 --- a/src/popup.tsx +++ b/src/popup.tsx @@ -11,12 +11,16 @@ const Popup = () => { const [openAIKey, setOpenAIKey] = useState(""); const [types, setTypes] = useState([]); const [isOn, setIsOn] = useState(true); + const [isAutoPosition, setIsAutoPosition] = useState( + false + ); const [newType, setNewType] = useState(""); const [isLoading, setIsLoading] = useState(false); useEffect(() => { getStorage("openai_key").then(setOpenAIKey); getStorage("isOn").then(setIsOn); + getStorage("isAutoPosition").then(setIsAutoPosition); getStorage("types").then((types) => { if (!types) { setTypes(DEFAULT_GROUP); @@ -59,6 +63,13 @@ const Popup = () => { }); }; + const enableAutoPosition = () => { + setIsAutoPosition((isAutoGroupPosition) => { + setStorage("isAutoPosition", !isAutoPosition); + return !isAutoPosition; + }); + }; + const ungroup = async () => { try { const tabs = await chrome.tabs.query({ currentWindow: true }); @@ -201,6 +212,23 @@ const Popup = () => { Allow automatic grouping + +
+ + + Allow automatic position + +
); };