-
Notifications
You must be signed in to change notification settings - Fork 28
ntp: omnibar states #1882
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
Open
shakyShane
wants to merge
2
commits into
main
Choose a base branch
from
08-08-ntp_omnibar_states
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ntp: omnibar states #1882
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { h } from 'preact'; | ||
import { Centered } from '../components/Layout.js'; | ||
import { OmnibarCustomized } from '../omnibar/components/OmnibarCustomized.js'; | ||
import { PersistentTermsProvider } from '../omnibar/components/PersistentTermsProvider.js'; | ||
|
||
export function factory() { | ||
return ( | ||
<Centered data-entry-point="omnibar"> | ||
<OmnibarCustomized /> | ||
<PersistentTermsProvider> | ||
<OmnibarCustomized /> | ||
</PersistentTermsProvider> | ||
</Centered> | ||
); | ||
} |
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
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
60 changes: 60 additions & 0 deletions
60
special-pages/pages/new-tab/app/omnibar/components/PersistentTermsProvider.js
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all of the naming here is a mess - can be addressed after the POC is confirmed working |
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,60 @@ | ||
import { h, createContext } from 'preact'; | ||
import { useCallback, useContext, useEffect, useState } from 'preact/hooks'; | ||
import { OmnibarTerm } from '../omnibar.term.js'; | ||
import { OmnibarContext, useOmnibarService } from './OmnibarProvider.js'; | ||
|
||
const InitialTermContext = createContext(/** @type {OmnibarTerm|null} */ (null)); | ||
|
||
/** | ||
* @param {object} props | ||
* @param {import('preact').ComponentChildren} props.children | ||
*/ | ||
export function PersistentTermsProvider({ children }) { | ||
const [terms] = useState(() => new OmnibarTerm()); | ||
return <InitialTermContext.Provider value={terms}>{children}</InitialTermContext.Provider>; | ||
} | ||
|
||
/** | ||
* When the Omnibar Service becomes ready, subscribe to tab updates so that we can prune old state | ||
*/ | ||
export function PruneInitialTerms() { | ||
const terms = useContext(InitialTermContext); | ||
const service = useOmnibarService(); | ||
useEffect(() => { | ||
return service?.onConfig(({ source, data }) => { | ||
if (source === 'subscription') { | ||
if (typeof data.tabId === 'string' && Array.isArray(data.tabIds)) { | ||
terms?.prune({ preserve: data.tabIds }); | ||
} | ||
} | ||
}); | ||
}, [service]); | ||
return null; | ||
} | ||
|
||
/** | ||
* A normal set-state, but with values recorded. Must be used when the Omnibar Service is ready | ||
*/ | ||
export function useQueryWithPersistence() { | ||
const terms = useContext(InitialTermContext); | ||
const { state } = useContext(OmnibarContext); | ||
|
||
if (state.status !== 'ready') { | ||
throw new Error('Cannot use `useQueryWithPersistence` without Omnibar Service being ready.'); | ||
} | ||
|
||
const [query, _setQuery] = useState(() => terms?.byId(state.config.tabId) || ''); | ||
|
||
/** @type {(term: string) => void} */ | ||
const setter = useCallback( | ||
(term) => { | ||
if (state.config.tabId) { | ||
terms?.update({ id: state.config.tabId, term }); | ||
} | ||
_setQuery(term); | ||
}, | ||
[state.config.tabId], | ||
); | ||
|
||
return /** @type {const} */ ([query, setter]); | ||
} |
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
31 changes: 31 additions & 0 deletions
31
special-pages/pages/new-tab/app/omnibar/integration-tests/omnibar.persistence.spec.js
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,31 @@ | ||
import { test } from '@playwright/test'; | ||
import { NewtabPage } from '../../../integration-tests/new-tab.page.js'; | ||
import { OmnibarPage } from './omnibar.page.js'; | ||
|
||
test.describe('omnibar widget persistence', () => { | ||
test('remembers input across tabs', async ({ page }, workerInfo) => { | ||
const ntp = NewtabPage.create(page, workerInfo); | ||
const omnibar = new OmnibarPage(ntp); | ||
await ntp.reducedMotion(); | ||
await ntp.openPage({ additional: { omnibar: true, 'omnibar.tabs': true } }); | ||
await omnibar.ready(); | ||
|
||
// first fill | ||
await omnibar.searchInput().fill('shoes'); | ||
|
||
// switch | ||
await omnibar.didSwitchToTab('02', ['01', '02']); | ||
await omnibar.expectInputValue(''); | ||
|
||
// second fill | ||
await omnibar.searchInput().fill('dresses'); | ||
|
||
// back to first | ||
await omnibar.didSwitchToTab('01', ['01', '02']); | ||
await omnibar.expectInputValue('shoes'); | ||
|
||
// back to second again | ||
await omnibar.didSwitchToTab('02', ['01', '02']); | ||
await omnibar.expectInputValue('dresses'); | ||
}); | ||
}); |
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
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,62 @@ | ||
export class OmnibarTerm { | ||
#values = new Map(); | ||
|
||
name() { | ||
return 'OmnibarTerm'; | ||
} | ||
|
||
/** | ||
* Updates the value associated with a given identifier. | ||
* | ||
* @param {object} args | ||
* @param {string} args.id | ||
* @param {string} args.term | ||
*/ | ||
update({ id, term }) { | ||
if (string(id) && string(term)) { | ||
this.#values.set(id, term); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} params | ||
* @param {string[]} params.preserve | ||
*/ | ||
prune({ preserve }) { | ||
for (const key of this.#values.keys()) { | ||
if (!preserve.includes(key)) { | ||
this.#values.delete(key); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} args | ||
* @param {string} args.id | ||
*/ | ||
remove({ id }) { | ||
if (string(id)) { | ||
this.#values.delete(id); | ||
} | ||
} | ||
|
||
/** | ||
* @param {string|null|undefined} id | ||
* @return {string} | ||
*/ | ||
byId(id) { | ||
if (!string(id)) return ''; | ||
const value = this.#values.get(id); | ||
if (!string(value)) return ''; | ||
return value; | ||
} | ||
} | ||
|
||
/** | ||
* @param {unknown} input | ||
*/ | ||
function string(input) { | ||
if (typeof input !== 'string') return ''; | ||
if (input.trim().length < 1) return ''; | ||
return input; | ||
} |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This state needs to live for the lifespan of the page, not the widget, so we go 1 level higher