-
Notifications
You must be signed in to change notification settings - Fork 875
Clean up floating toolbar lifecycle on removal #1067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PeterDaveHello
merged 1 commit into
ChatGPTBox-dev:master
from
PeterDaveHello:fix/floating-toolbar-unmount-cleanup
Sep 11, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
tests/setup/content-script-selection-toolbar-loader-hooks.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { readFile } from 'node:fs/promises' | ||
| import { fileURLToPath } from 'node:url' | ||
|
|
||
| const contentScriptStubs = new Map([ | ||
| ['./styles.scss', 'test:styles'], | ||
| ['../components/DecisionCard', 'test:decision-card'], | ||
| ['./site-adapters', 'test:site-adapters'], | ||
| ['./selection-tools', 'test:selection-tools'], | ||
| ['./menu-tools', 'test:menu-tools'], | ||
| ['../config/index.mjs', 'test:config'], | ||
| ['../utils', 'test:utils'], | ||
| ['../components/FloatingToolbar', 'test:floating-toolbar'], | ||
| ['webextension-polyfill', 'test:browser'], | ||
| ['../config/language.mjs', 'test:language'], | ||
| ['../_locales/i18n-react', 'test:i18n-react'], | ||
| ['i18next', 'test:i18next'], | ||
| ['../services/init-session.mjs', 'test:init-session'], | ||
| ['../services/wrappers.mjs', 'test:wrappers'], | ||
| ['../services/apis/chatgpt-web.mjs', 'test:chatgpt-api'], | ||
| ['../components/WebJumpBackNotification', 'test:jump-back'], | ||
| ['./port-error.mjs', 'test:port-error'], | ||
| ]) | ||
|
|
||
| const floatingToolbarStubs = new Map([ | ||
| ['../ConversationCard', 'test:floating-conversation-card'], | ||
| ['../../content-script/selection-tools', 'test:floating-selection-tools'], | ||
| ['../../utils', 'test:floating-utils'], | ||
| ['react-draggable', 'test:floating-draggable'], | ||
| ['../../hooks/use-clamp-window-size', 'test:floating-window-size'], | ||
| ['react-i18next', 'test:floating-i18n'], | ||
| ['../../hooks/use-config.mjs', 'test:floating-config'], | ||
| ]) | ||
|
|
||
| const sources = { | ||
| 'test:styles': '', | ||
| 'test:decision-card': 'export default function DecisionCard() { return null }', | ||
| 'test:site-adapters': 'export const config = {}', | ||
| 'test:selection-tools': 'export const config = {}', | ||
| 'test:menu-tools': 'export const config = {}', | ||
| 'test:config': ` | ||
| export const chatgptWebModelKeys = [] | ||
| export const getPreferredLanguageKey = async () => 'en' | ||
| export const getUserConfig = () => globalThis.__SELECTION_TOOLBAR_TEST__.getUserConfig() | ||
| export const isUsingChatgptWebModel = () => false | ||
| export const setAccessToken = async () => {} | ||
| export const setUserConfig = async () => {} | ||
| `, | ||
| 'test:utils': ` | ||
| export const createElementAtPosition = () => { | ||
| const element = document.createElement('div') | ||
| document.documentElement.append(element) | ||
| globalThis.__SELECTION_TOOLBAR_TEST__.createdContainers.push(element) | ||
| return element | ||
| } | ||
| export const cropText = async (text) => text | ||
| export const endsWithQuestionMark = () => false | ||
| export const getApiModesStringArrayFromConfig = () => [] | ||
| export const getClientPosition = () => ({ x: 0, y: 0 }) | ||
| export const getPossibleElementByQuerySelector = () => null | ||
| `, | ||
| 'test:floating-toolbar': ` | ||
| export default function FloatingToolbar() { | ||
| globalThis.__SELECTION_TOOLBAR_TEST__.renderCount += 1 | ||
| return null | ||
| } | ||
| `, | ||
| 'test:browser': ` | ||
| const event = { addListener() {}, removeListener() {} } | ||
| export default { | ||
| runtime: { onMessage: event, sendMessage: async () => {} }, | ||
| storage: { onChanged: event }, | ||
| } | ||
| `, | ||
| 'test:language': `export const getPreferredLanguage = async () => 'English'`, | ||
| 'test:i18n-react': '', | ||
| 'test:i18next': 'export const changeLanguage = async () => {}', | ||
| 'test:init-session': 'export const initSession = () => ({})', | ||
| 'test:wrappers': ` | ||
| export const getChatGptAccessToken = async () => null | ||
| export const registerPortListener = () => {} | ||
| `, | ||
| 'test:chatgpt-api': 'export const generateAnswersWithChatgptWebApi = async () => {}', | ||
| 'test:jump-back': 'export default function WebJumpBackNotification() { return null }', | ||
| 'test:port-error': ` | ||
| export const getPortErrorMessage = (error) => String(error) | ||
| export const shouldDelegatePortError = () => false | ||
| `, | ||
| 'test:floating-conversation-card': ` | ||
| import { useLayoutEffect } from 'preact/hooks' | ||
| export default function ConversationCard(props) { | ||
| const state = globalThis.__FLOATING_TOOLBAR_TEST__ | ||
| state.onClose = props.onClose | ||
| useLayoutEffect(() => () => { | ||
| state.cleanupCount += 1 | ||
| state.cleanupSawConnectedContainer = state.container.isConnected | ||
| }, []) | ||
| return null | ||
| } | ||
| `, | ||
| 'test:floating-selection-tools': 'export const config = {}', | ||
| 'test:floating-utils': ` | ||
|
pullfrog[bot] marked this conversation as resolved.
|
||
| export const getClientPosition = () => ({ x: 0, y: 0 }) | ||
| export const isMobile = () => false | ||
| export const setElementPositionInViewport = (_container, x, y) => ({ x, y }) | ||
| `, | ||
| 'test:floating-draggable': ` | ||
| export default function Draggable(props) { | ||
| return props.children | ||
| } | ||
| `, | ||
| 'test:floating-window-size': 'export const useClampWindowSize = () => [1000, 1000]', | ||
| 'test:floating-i18n': 'export const useTranslation = () => ({ t: (value) => value })', | ||
| 'test:floating-config': ` | ||
| import { useLayoutEffect } from 'preact/hooks' | ||
| const config = { | ||
| alwaysPinWindow: false, | ||
| themeMode: 'light', | ||
| activeSelectionTools: [], | ||
| customSelectionTools: [], | ||
| } | ||
| export const useConfig = (onLoad) => { | ||
| useLayoutEffect(() => { | ||
| onLoad() | ||
| }, []) | ||
| return config | ||
| } | ||
| `, | ||
| } | ||
|
|
||
| export async function resolve(specifier, context, nextResolve) { | ||
| if (context.parentURL?.startsWith('test:') && specifier === 'preact/hooks') { | ||
| return nextResolve(specifier, { ...context, parentURL: import.meta.url }) | ||
| } | ||
|
|
||
| if (context.parentURL?.endsWith('/src/content-script/index.jsx')) { | ||
| const stubUrl = contentScriptStubs.get(specifier) | ||
| if (stubUrl) return { url: stubUrl, shortCircuit: true } | ||
| } | ||
|
|
||
| if (context.parentURL?.endsWith('/src/components/FloatingToolbar/index.jsx')) { | ||
| const stubUrl = floatingToolbarStubs.get(specifier) | ||
| if (stubUrl) return { url: stubUrl, shortCircuit: true } | ||
| } | ||
|
|
||
| return nextResolve(specifier, context) | ||
|
pullfrog[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export async function load(url, context, nextLoad) { | ||
| if (url.startsWith('test:')) { | ||
| return { | ||
| shortCircuit: true, | ||
| format: 'module', | ||
| source: sources[url], | ||
| } | ||
| } | ||
|
|
||
| if (url.startsWith('file://') && url.endsWith('.jsx') && !url.includes('node_modules')) { | ||
| const source = await readFile(fileURLToPath(url), 'utf8') | ||
| const esbuild = await import('esbuild') | ||
| const result = await esbuild.transform(source, { | ||
| loader: 'jsx', | ||
| jsx: 'automatic', | ||
| jsxImportSource: 'preact', | ||
| }) | ||
| return { shortCircuit: true, format: 'module', source: result.code } | ||
| } | ||
|
|
||
| return nextLoad(url, context) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.