-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
history: documenting the data model (#1468)
- Loading branch information
1 parent
82a9ec9
commit aacf81d
Showing
49 changed files
with
2,169 additions
and
270 deletions.
There are no files selected for viewing
This file contains 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,125 @@ | ||
import { h, createContext } from 'preact'; | ||
import { useContext } from 'preact/hooks'; | ||
import { useSignalEffect } from '@preact/signals'; | ||
import { paramsToQuery } from './history.service.js'; | ||
import { OVERSCAN_AMOUNT } from './constants.js'; | ||
import { usePlatformName } from './types.js'; | ||
import { eventToTarget } from '../../../shared/handlers.js'; | ||
|
||
// Create the context | ||
const HistoryServiceContext = createContext({ | ||
service: /** @type {import("./history.service").HistoryService} */ ({}), | ||
initial: /** @type {import("./history.service").ServiceData} */ ({}), | ||
}); | ||
|
||
// Provider component | ||
/** | ||
* Provides a context for the history service, allowing dependent components to access it. | ||
* | ||
* @param {Object} props - The properties object for the HistoryServiceProvider component. | ||
* @param {import("./history.service").HistoryService} props.service - The history service instance to be provided through the context. | ||
* @param {import("./history.service").ServiceData} props.initial - The history service instance to be provided through the context. | ||
* @param {import("preact").ComponentChild} props.children - The child components that will consume the history service context. | ||
*/ | ||
export function HistoryServiceProvider({ service, initial, children }) { | ||
const platFormName = usePlatformName(); | ||
useSignalEffect(() => { | ||
// Add a listener for the 'search-commit' event | ||
window.addEventListener('search-commit', (/** @type {CustomEvent<{params: URLSearchParams}>} */ event) => { | ||
const detail = event.detail; | ||
if (detail && detail.params instanceof URLSearchParams) { | ||
const asQuery = paramsToQuery(detail.params); | ||
service.trigger(asQuery); | ||
} else { | ||
console.error('missing detail.params from search-commit event'); | ||
} | ||
}); | ||
|
||
// Cleanup the event listener on unmount | ||
return () => { | ||
window.removeEventListener('search-commit', this); | ||
}; | ||
}); | ||
|
||
useSignalEffect(() => { | ||
function handler(/** @type {CustomEvent<{start: number, end: number}>} */ event) { | ||
if (!service.query.data) throw new Error('unreachable'); | ||
const { end } = event.detail; | ||
const memory = service.query.data.results; | ||
if (memory.length - end < OVERSCAN_AMOUNT) { | ||
service.requestMore(); | ||
} | ||
} | ||
window.addEventListener('range-change', handler); | ||
return () => { | ||
window.removeEventListener('range-change', handler); | ||
}; | ||
}); | ||
|
||
useSignalEffect(() => { | ||
function handler(/** @type {MouseEvent} */ event) { | ||
if (!(event.target instanceof Element)) return; | ||
const btn = /** @type {HTMLButtonElement|null} */ (event.target.closest('button')); | ||
const anchor = /** @type {HTMLButtonElement|null} */ (event.target.closest('a[href][data-url]')); | ||
if (btn) { | ||
if (btn?.dataset.titleMenu) { | ||
event.stopImmediatePropagation(); | ||
event.preventDefault(); | ||
return confirm(`todo: title menu for ${btn.dataset.titleMenu}`); | ||
} | ||
if (btn?.dataset.rowMenu) { | ||
event.stopImmediatePropagation(); | ||
event.preventDefault(); | ||
return confirm(`todo: row menu for ${btn.dataset.rowMenu}`); | ||
} | ||
if (btn?.dataset.deleteRange) { | ||
event.stopImmediatePropagation(); | ||
event.preventDefault(); | ||
return confirm(`todo: delete range for ${btn.dataset.deleteRange}`); | ||
} | ||
if (btn?.dataset.deleteAll) { | ||
event.stopImmediatePropagation(); | ||
event.preventDefault(); | ||
return confirm(`todo: delete all`); | ||
} | ||
} else if (anchor) { | ||
const url = anchor.dataset.url; | ||
if (!url) return; | ||
event.preventDefault(); | ||
event.stopImmediatePropagation(); | ||
const target = eventToTarget(event, platFormName); | ||
service.openUrl(url, target); | ||
return; | ||
} | ||
return null; | ||
} | ||
document.addEventListener('click', handler); | ||
|
||
const handleAuxClick = (event) => { | ||
const anchor = /** @type {HTMLButtonElement|null} */ (event.target.closest('a[href][data-url]')); | ||
const url = anchor?.dataset.url; | ||
if (anchor && url && event.button === 1) { | ||
event.preventDefault(); | ||
event.stopImmediatePropagation(); | ||
const target = eventToTarget(event, platFormName); | ||
service.openUrl(url, target); | ||
} | ||
}; | ||
document.addEventListener('auxclick', handleAuxClick); | ||
|
||
return () => { | ||
document.removeEventListener('auxclick', handleAuxClick); | ||
document.removeEventListener('click', handler); | ||
}; | ||
}); | ||
return <HistoryServiceContext.Provider value={{ service, initial }}>{children}</HistoryServiceContext.Provider>; | ||
} | ||
|
||
// Hook for consuming the context | ||
export function useHistory() { | ||
const context = useContext(HistoryServiceContext); | ||
if (!context) { | ||
throw new Error('useHistoryService must be used within a HistoryServiceProvider'); | ||
} | ||
return context; | ||
} |
This file contains 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,38 @@ | ||
export class Settings { | ||
/** | ||
* @param {object} params | ||
* @param {{name: 'macos' | 'windows'}} [params.platform] | ||
* @param {number} [params.typingDebounce=500] how long to debounce typing in the search field | ||
*/ | ||
constructor({ platform = { name: 'macos' }, typingDebounce = 100 }) { | ||
this.platform = platform; | ||
this.typingDebounce = typingDebounce; | ||
} | ||
|
||
withPlatformName(name) { | ||
/** @type {ImportMeta['platform'][]} */ | ||
const valid = ['windows', 'macos']; | ||
if (valid.includes(/** @type {any} */ (name))) { | ||
return new Settings({ | ||
...this, | ||
platform: { name }, | ||
}); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* @param {null|undefined|number|string} value | ||
*/ | ||
withDebounce(value) { | ||
if (!value) return this; | ||
const input = String(value).trim(); | ||
if (input.match(/^\d+$/)) { | ||
return new Settings({ | ||
...this, | ||
typingDebounce: parseInt(input, 10), | ||
}); | ||
} | ||
return this; | ||
} | ||
} |
This file contains 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 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 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.