Skip to content

Commit e7d9ab2

Browse files
committed
differentiate between user + auto
1 parent c73d4af commit e7d9ab2

File tree

9 files changed

+43
-25
lines changed

9 files changed

+43
-25
lines changed

special-pages/pages/history/app/components/Sidebar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function Sidebar({ ranges }) {
6262
*/
6363
function onClick(range) {
6464
if (range === 'all') {
65-
dispatch({ kind: 'reset' });
65+
dispatch({ kind: 'search-by-term', value: '' });
6666
} else if (range) {
6767
dispatch({ kind: 'search-by-range', value: range });
6868
}

special-pages/pages/history/app/global/Providers/HistoryServiceProvider.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { signal, useSignal, useSignalEffect } from '@preact/signals';
77
import { generateHeights, generateViewIds } from '../../utils.js';
88

99
/**
10-
* @typedef {{kind: 'search-commit', params: URLSearchParams}
10+
* @typedef {import('../../../types/history.ts').HistoryQuery['source']} Source
11+
* @typedef {{kind: 'search-commit', params: URLSearchParams, source: Source}
1112
* | {kind: 'delete-range'; value: string }
1213
* | {kind: 'delete-all'; }
1314
* | {kind: 'delete-term'; term: string }
@@ -84,7 +85,7 @@ export function HistoryServiceProvider({ service, children, initial }) {
8485
function dispatch(action) {
8586
switch (action.kind) {
8687
case 'search-commit': {
87-
const asQuery = paramsToQuery(action.params, 'user');
88+
const asQuery = paramsToQuery(action.params, action.source);
8889
service.trigger(asQuery);
8990
break;
9091
}

special-pages/pages/history/app/global/Providers/QueryProvider.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { signal, useSignal } from '@preact/signals';
55
/**
66
* @typedef {import('../../../types/history.ts').Range} Range
77
* @typedef {import('../../../types/history.ts').RangeId} RangeId
8+
* @typedef {import('../../../types/history.ts').HistoryQuery['source']} Source
89
* @typedef {{
910
* term: string | null,
1011
* range: RangeId | null,
1112
* domain: string | null,
13+
* source: Source,
1214
* }} QueryState - this is the value the entire application can read/observe
1315
*/
1416

@@ -25,6 +27,7 @@ const QueryContext = createContext(
2527
term: /** @type {string|null} */ (null),
2628
range: /** @type {RangeId|null} */ (null),
2729
domain: /** @type {string|null} */ (null),
30+
source: /** @type {Source} */ ('initial'),
2831
})
2932
),
3033
);
@@ -49,6 +52,7 @@ export function QueryProvider({ children, query = { term: '' } }) {
4952
term: 'term' in query ? query.term : null,
5053
range: 'range' in query ? query.range : null,
5154
domain: 'domain' in query ? query.domain : null,
55+
source: /** @type {Source} */ ('initial'),
5256
};
5357
const queryState = useSignal(initial);
5458

@@ -60,19 +64,24 @@ export function QueryProvider({ children, query = { term: '' } }) {
6064
queryState.value = (() => {
6165
switch (action.kind) {
6266
case 'reset': {
63-
return { term: '', domain: null, range: null };
67+
return { term: '', domain: null, range: null, source: /** @type {const} */ ('auto') };
6468
}
6569
case 'search-by-domain': {
66-
return { term: null, domain: action.value, range: null };
70+
return { term: null, domain: action.value, range: null, source: /** @type {const} */ ('user') };
6771
}
6872
case 'search-by-range': {
69-
return { term: null, domain: null, range: /** @type {RangeId} */ (action.value) };
73+
return {
74+
term: null,
75+
domain: null,
76+
range: /** @type {RangeId} */ (action.value),
77+
source: /** @type {const} */ ('user'),
78+
};
7079
}
7180
case 'search-by-term': {
72-
return { term: action.value, domain: null, range: null };
81+
return { term: action.value, domain: null, range: null, source: /** @type {const} */ ('user') };
7382
}
7483
default:
75-
return { term: '', domain: null, range: null };
84+
return { term: '', domain: null, range: null, source: /** @type {const} */ ('auto') };
7685
}
7786
})();
7887
}

special-pages/pages/history/app/global/hooks/useSearchCommit.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ export function useSearchCommit() {
2727
clearTimeout(timer);
2828
if (next.term !== null) {
2929
const term = next.term;
30+
const source = next.source;
3031
timer = setTimeout(() => {
3132
const params = new URLSearchParams();
3233
params.set('q', term);
33-
dispatch({ kind: 'search-commit', params });
34+
dispatch({ kind: 'search-commit', params, source });
3435
}, settings.typingDebounce);
3536
}
3637
if (next.domain !== null) {
3738
const params = new URLSearchParams();
3839
params.set('domain', next.domain);
39-
dispatch({ kind: 'search-commit', params });
40+
dispatch({ kind: 'search-commit', params, source: next.source });
4041
}
4142
return null;
4243
});
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useHistoryServiceDispatch } from '../Providers/HistoryServiceProvider.js';
2-
import { useComputed, useSignalEffect } from '@preact/signals';
32
import { useQueryContext } from '../Providers/QueryProvider.js';
3+
import { useEffect } from 'preact/hooks';
44

55
/**
66
* Synchronizes the `derivedRange` signal with the browser's URL and issues a
@@ -10,14 +10,12 @@ import { useQueryContext } from '../Providers/QueryProvider.js';
1010
export function useSearchCommitForRange() {
1111
const dispatch = useHistoryServiceDispatch();
1212
const query = useQueryContext();
13-
const derivedRange = useComputed(() => {
14-
return query.value.range;
15-
});
1613

17-
useSignalEffect(() => {
14+
useEffect(() => {
1815
let timer;
1916
let counter = 0;
20-
const sub = derivedRange.subscribe((nextRange) => {
17+
const sub = query.subscribe((nextQuery) => {
18+
const { range } = nextQuery;
2119
if (counter === 0) {
2220
counter += 1;
2321
return;
@@ -27,16 +25,16 @@ export function useSearchCommitForRange() {
2725
url.searchParams.delete('q');
2826
url.searchParams.delete('range');
2927

30-
if (nextRange !== null) {
31-
url.searchParams.set('range', nextRange);
28+
if (range !== null) {
29+
url.searchParams.set('range', range);
3230
window.history.replaceState(null, '', url.toString());
33-
dispatch({ kind: 'search-commit', params: new URLSearchParams(url.searchParams) });
31+
dispatch({ kind: 'search-commit', params: new URLSearchParams(url.searchParams), source: 'user' });
3432
}
3533
});
3634

3735
return () => {
3836
sub();
3937
clearTimeout(timer);
4038
};
41-
});
39+
}, [query, dispatch]);
4240
}

special-pages/pages/history/integration-tests/history-selections.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ test.describe('history selections', () => {
165165
await hp.deletesAllForTerm('example.com', { action: 'delete' });
166166

167167
// should have reset the UI now
168-
await hp.didMakeNthQuery({ nth: 2, query: { term: '' } });
168+
await hp.didMakeNthQuery({ nth: 2, query: { term: '' }, source: 'auto' });
169169
});
170170
test('removes all selections with ESC key', async ({ page }, workerInfo) => {
171171
const hp = HistoryTestPage.create(page, workerInfo).withEntries(2000);

special-pages/pages/history/integration-tests/history.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ test.describe('history', () => {
104104
await hp.selectsAll();
105105
await hp.didMakeNthQuery({ nth: 1, query: { term: '' } });
106106
});
107-
test('deleting range from sidebar items + resetting the query state', async ({ page }, workerInfo) => {
107+
test('deleting range from sidebar i tems + resetting the query state', async ({ page }, workerInfo) => {
108108
const hp = HistoryTestPage.create(page, workerInfo).withEntries(2000);
109109
await hp.openPage({});
110110
await hp.didMakeInitialQueries({ term: '' });
@@ -116,7 +116,7 @@ test.describe('history', () => {
116116
await hp.sideBarItemWasRemoved('Show history for today');
117117

118118
// makes a new query for default data
119-
await hp.didMakeNthQuery({ nth: 2, query: { term: '' } });
119+
await hp.didMakeNthQuery({ nth: 2, query: { term: '' }, source: 'auto' });
120120
});
121121
test('deleting sidebar items, but dismissing modal', async ({ page }, workerInfo) => {
122122
const hp = HistoryTestPage.create(page, workerInfo).withEntries(2000);
@@ -190,7 +190,7 @@ test.describe('history', () => {
190190
await hp.didDeleteDomain('youtube.com');
191191

192192
// should have reset the UI now
193-
await hp.didMakeNthQuery({ nth: 2, query: { term: '' } });
193+
await hp.didMakeNthQuery({ nth: 2, query: { term: '' }, source: 'auto' });
194194
});
195195
test('does not concatenate results if the query is not an addition', async ({ page }, workerInfo) => {
196196
const hp = HistoryTestPage.create(page, workerInfo).withEntries(1);

special-pages/pages/history/messages/query.request.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
"const": "user",
3232
"title": "User Source",
3333
"description": "Indicates the query was following a user interaction"
34+
},
35+
{
36+
"const": "auto",
37+
"title": "Auto Source",
38+
"description": "Indicates the query was triggered automatically, for example in response to another action (like delete)"
3439
}
3540
]
3641
}

special-pages/pages/history/types/history.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export type InitialSource = "initial";
4141
* Indicates the query was following a user interaction
4242
*/
4343
export type UserSource = "user";
44+
/**
45+
* Indicates the query was triggered automatically, for example in response to another action (like delete)
46+
*/
47+
export type AutoSource = "auto";
4448
export type Favicon = null | {
4549
src: string;
4650
maxAvailableSize?: number;
@@ -211,7 +215,7 @@ export interface HistoryQuery {
211215
* Maximum number of records to return
212216
*/
213217
limit: number;
214-
source: InitialSource | UserSource;
218+
source: InitialSource | UserSource | AutoSource;
215219
}
216220
export interface SearchTerm {
217221
term: string;

0 commit comments

Comments
 (0)