Skip to content

Commit 81cf8d4

Browse files
authored
ntp: protections widget (#1685)
* ntp: protections widget * translations * remove button handling * cleanup * more examples * stylelint
1 parent 763e1c4 commit 81cf8d4

File tree

92 files changed

+1663
-1054
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1663
-1054
lines changed

special-pages/pages/new-tab/app/activity/ActivityProvider.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { createContext, h } from 'preact';
22
import { useEffect, useReducer, useRef } from 'preact/hooks';
33
import { useMessaging } from '../types.js';
4-
import { reducer, useConfigSubscription, useInitialDataAndConfig } from '../service.hooks.js';
4+
import { reducer, useInitialData } from '../service.hooks.js';
55
import { useBatchedActivityApi } from '../settings.provider.js';
66
import { BatchedActivityService } from './batched-activity.service.js';
77

88
/**
99
* @typedef {import('../../types/new-tab.js').ActivityData} ActivityData
10-
* @typedef {import('../../types/new-tab.js').ActivityConfig} ActivityConfig
1110
* @typedef {import('../../types/new-tab').TrackingStatus} TrackingStatus
1211
* @typedef {import('../../types/new-tab').HistoryEntry} HistoryEntry
1312
* @typedef {import('../../types/new-tab').DomainActivity} DomainActivity
14-
* @typedef {import('../service.hooks.js').State<import("./batched-activity.service.js").Incoming, ActivityConfig>} State
15-
* @typedef {import('../service.hooks.js').Events<ActivityData, ActivityConfig>} Events
13+
* @typedef {import('../service.hooks.js').State<import("./batched-activity.service.js").Incoming, null>} State
14+
* @typedef {import('../service.hooks.js').Events<ActivityData, null>} Events
1615
*/
1716

1817
/**
@@ -21,10 +20,6 @@ import { BatchedActivityService } from './batched-activity.service.js';
2120
export const ActivityContext = createContext({
2221
/** @type {State} */
2322
state: { status: 'idle', data: null, config: null },
24-
/** @type {() => void} */
25-
toggle: () => {
26-
throw new Error('must implement');
27-
},
2823
});
2924

3025
export const ActivityServiceContext = createContext(/** @type {BatchedActivityService|null} */ ({}));
@@ -49,13 +44,10 @@ export function ActivityProvider(props) {
4944
const service = useService(batched);
5045

5146
// get initial data
52-
useInitialDataAndConfig({ dispatch, service });
53-
54-
// subscribe to toggle + expose a fn for sync toggling
55-
const { toggle } = useConfigSubscription({ dispatch, service });
47+
useInitialData({ dispatch, service });
5648

5749
return (
58-
<ActivityContext.Provider value={{ state, toggle }}>
50+
<ActivityContext.Provider value={{ state }}>
5951
<ActivityServiceContext.Provider value={service.current}>{props.children}</ActivityServiceContext.Provider>
6052
</ActivityContext.Provider>
6153
);

special-pages/pages/new-tab/app/activity/NormalizeDataProvider.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import { ACTION_BURN } from '../burning/BurnProvider.js';
1111

1212
/**
1313
* @typedef {import('../../types/new-tab.js').ActivityData} ActivityData
14-
* @typedef {import('../../types/new-tab.js').ActivityConfig} ActivityConfig
1514
* @typedef {import('../../types/new-tab').TrackingStatus} TrackingStatus
1615
* @typedef {import('../../types/new-tab').HistoryEntry} HistoryEntry
1716
* @typedef {import('../../types/new-tab').DomainActivity} DomainActivity
18-
* @typedef {import('../service.hooks.js').State<import("./batched-activity.service.js").Incoming, ActivityConfig>} State
19-
* @typedef {import('../service.hooks.js').Events<ActivityData, ActivityConfig>} Events
17+
* @typedef {import('../service.hooks.js').State<import("./batched-activity.service.js").Incoming, null>} State
18+
* @typedef {import('../service.hooks.js').Events<ActivityData, null>} Events
2019
*/
2120

2221
/**
@@ -143,7 +142,6 @@ export function SignalStateProvider({ children }) {
143142
if (!service) return;
144143
const anchor = /** @type {HTMLAnchorElement|null} */ (target.closest('a[href][data-url]'));
145144
const button = /** @type {HTMLButtonElement|null} */ (target.closest('button[value][data-action]'));
146-
const toggle = /** @type {HTMLButtonElement|null} */ (target.closest('button[data-toggle]'));
147145
if (anchor) {
148146
const url = anchor.dataset.url;
149147
if (!url) return;
@@ -173,15 +171,10 @@ export function SignalStateProvider({ children }) {
173171
} else {
174172
console.warn('unhandled action:', action);
175173
}
176-
} else if (toggle) {
177-
if (state.config?.expansion === 'collapsed' && batched) {
178-
const next = activity.value.urls.slice(0, Math.min(service.INITIAL, activity.value.urls.length));
179-
setVisibleRange(next);
180-
}
181174
}
182175
}
183176

184-
const didClick = useCallback(didClick_, [service, batched, state.config.expansion]);
177+
const didClick = useCallback(didClick_, [service, batched]);
185178
const firstUrls = state.data.activity.map((x) => x.url);
186179
const keys = useSignal(normalizeKeys([], firstUrls));
187180

special-pages/pages/new-tab/app/activity/activity.md

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ Notes:
5858
- on `macOS`, `history.title` should be a path-like string to match current implementations
5959
- `etldPlusOne` will be used for fallback favicons/colors, so the logic should match the NTP
6060

61-
### `activity_getConfig`
62-
- {@link "NewTab Messages".ActivityGetConfigRequest}
63-
- Used to fetch the initial config data (eg: expanded vs collapsed)
64-
- returns {@link "NewTab Messages".ActivityConfig}
65-
6661
### `activity_getUrls`
6762
- {@link "NewTab Messages".ActivityGetUrlsRequest}
6863
- Used to fetch the initial config data (eg: expanded vs collapsed)
@@ -142,24 +137,8 @@ by sending the notification `activity_burnAnimationComplete`
142137
}
143138
```
144139

145-
### `activity_onConfigUpdate`
146-
- {@link "NewTab Messages".ActivityOnDataUpdateSubscription }
147-
- The widget config
148-
- returns {@link "NewTab Messages".ActivityConfig}
149-
150140
## Notifications:
151141

152-
### `activity_setConfig`
153-
- {@link "NewTab Messages".ActivitySetConfigNotification}
154-
- Sent when the user toggles the expansion of the activity feed
155-
- sends {@link "NewTab Messages".ActivityConfig}
156-
- example payload:
157-
```json
158-
{
159-
"expansion": "collapsed"
160-
}
161-
```
162-
163142
### `activity_addFavorite`
164143
- {@link "NewTab Messages".ActivityAddFavoriteNotification}
165144
- Sent when the user clicks the favorite icon

special-pages/pages/new-tab/app/activity/batched-activity.service.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* @typedef {import("../../types/new-tab.js").ActivityData} ActivityData
3-
* @typedef {import("../../types/new-tab.js").ActivityConfig} ActivityConfig
43
* @typedef {import("../../types/new-tab.js").UrlInfo} UrlInfo
54
* @typedef {import("../../types/new-tab.js").PatchData} PatchData
65
* @typedef {import('../../types/new-tab.js').DomainActivity} DomainActivity
@@ -89,13 +88,6 @@ export class BatchedActivityService {
8988
return next;
9089
});
9190

92-
/** @type {Service<ActivityConfig>} */
93-
this.configService = new Service({
94-
initial: () => ntp.messaging.request('activity_getConfig'),
95-
subscribe: (cb) => ntp.messaging.subscribe('activity_onConfigUpdate', cb),
96-
persist: (data) => ntp.messaging.notify('activity_setConfig', data),
97-
});
98-
9991
/** @type {EventTarget|null} */
10092
this.burns = new EventTarget();
10193
this.burnUnsub = this.ntp.messaging.subscribe('activity_onBurnComplete', () => {
@@ -108,21 +100,17 @@ export class BatchedActivityService {
108100
}
109101

110102
/**
111-
* @returns {Promise<{data: ActivityData; config: ActivityConfig }>}
103+
* @returns {Promise<ActivityData>}
112104
* @internal
113105
*/
114106
async getInitial() {
115-
const configPromise = this.configService.fetchInitial();
116-
const dataPromise = this.dataService.fetchInitial();
117-
const [config, data] = await Promise.all([configPromise, dataPromise]);
118-
return { config, data };
107+
return await this.dataService.fetchInitial();
119108
}
120109

121110
/**
122111
* @internal
123112
*/
124113
destroy() {
125-
this.configService.destroy();
126114
this.dataService.destroy();
127115
this.burnUnsub();
128116
this.burns = null;
@@ -158,28 +146,6 @@ export class BatchedActivityService {
158146
this.dataService.triggerFetch();
159147
}
160148
}
161-
162-
/**
163-
* @param {(evt: {data: ActivityConfig, source: InvocationSource}) => void} cb
164-
* @internal
165-
*/
166-
onConfig(cb) {
167-
return this.configService.onData(cb);
168-
}
169-
/**
170-
* Update the in-memory data immediate and persist.
171-
* Any state changes will be broadcast to consumers synchronously
172-
* @internal
173-
*/
174-
toggleExpansion() {
175-
this.configService.update((old) => {
176-
if (old.expansion === 'expanded') {
177-
return { ...old, expansion: /** @type {const} */ ('collapsed') };
178-
} else {
179-
return { ...old, expansion: /** @type {const} */ ('expanded') };
180-
}
181-
});
182-
}
183149
/**
184150
* @param {string} url
185151
*/

special-pages/pages/new-tab/app/activity/components/Activity.examples.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { h } from 'preact';
22
import { Activity, ActivityBody } from './Activity.js';
3-
import { noop } from '../../utils.js';
43
import { signal } from '@preact/signals';
54
import { generateSampleData } from '../mocks/activity.mock-transport.js';
65
import { normalizeData, NormalizedDataContext } from '../NormalizeDataProvider.js';
@@ -10,12 +9,12 @@ import { normalizeData, NormalizedDataContext } from '../NormalizeDataProvider.j
109
export const activityExamples = {
1110
'activity.empty': {
1211
factory: () => {
13-
return <Activity expansion={'expanded'} itemCount={0} trackerCount={0} toggle={noop('toggle')} batched={false} />;
12+
return <Activity itemCount={0} batched={false} />;
1413
},
1514
},
1615
'activity.few': {
1716
factory: () => (
18-
<Activity expansion={'expanded'} itemCount={10} trackerCount={20} toggle={noop('toggle')} batched={false}>
17+
<Activity itemCount={10} batched={false}>
1918
<Mock size={3}>
2019
<ActivityBody canBurn={false} visibility={'visible'} />
2120
</Mock>
@@ -24,7 +23,7 @@ export const activityExamples = {
2423
},
2524
'activity.noTrackers': {
2625
factory: () => (
27-
<Activity expansion={'expanded'} itemCount={20} trackerCount={0} toggle={noop('toggle')} batched={false}>
26+
<Activity itemCount={20} batched={false}>
2827
<Mock size={1}>
2928
<ActivityBody canBurn={false} visibility={'visible'} />
3029
</Mock>
@@ -33,7 +32,7 @@ export const activityExamples = {
3332
},
3433
'activity.noActivity.someTrackers': {
3534
factory: () => (
36-
<Activity expansion={'collapsed'} itemCount={0} trackerCount={56} toggle={noop('toggle')} batched={false}>
35+
<Activity itemCount={0} batched={false}>
3736
<Mock size={0}>
3837
<ActivityBody canBurn={false} visibility={'visible'} />
3938
</Mock>

0 commit comments

Comments
 (0)