mirror of
https://github.com/urbit/shrub.git
synced 2024-12-19 08:32:39 +03:00
hark: upgrade to hooks api
This commit is contained in:
parent
eadaa25e9a
commit
e22d38fbef
@ -8,8 +8,6 @@ import _ from 'lodash';
|
||||
import { compose } from 'lodash/fp';
|
||||
import { makePatDa } from '~/logic/lib/util';
|
||||
import { describeNotification, getReferent } from '../lib/hark';
|
||||
import { reduceState } from '../state/base';
|
||||
import useHarkState, { HarkState } from '../state/hark';
|
||||
|
||||
function calculateCount(json: any, state: HarkState) {
|
||||
state.notificationsCount = Object.keys(state.unreadNotes).length;
|
||||
@ -263,7 +261,7 @@ function added(json: any, state: HarkState): HarkState {
|
||||
const [fresh] = _.partition(state.unreadNotes, ({ index: idx }) => !notifIdxEqual(index, idx));
|
||||
state.unreadNotes = [...fresh, { index, notification }];
|
||||
|
||||
if ('Notification' in window && !useHarkState.getState().doNotDisturb) {
|
||||
if ('Notification' in window && !state.doNotDisturb) {
|
||||
const description = describeNotification(data);
|
||||
const referent = getReferent(data);
|
||||
new Notification(`${description} ${referent}`, {
|
||||
@ -412,37 +410,17 @@ export function reduce(data, state) {
|
||||
return reducer(state);
|
||||
}
|
||||
|
||||
export const HarkReducer = (json: any) => {
|
||||
const data = _.get(json, 'harkUpdate', false);
|
||||
if (data) {
|
||||
console.log(data);
|
||||
reduceState(useHarkState, data, [reduce]);
|
||||
}
|
||||
const graphHookData = _.get(json, 'hark-graph-hook-update', false);
|
||||
if (graphHookData) {
|
||||
reduceState<HarkState, any>(useHarkState, graphHookData, [
|
||||
// @ts-ignore investigate zustand types
|
||||
graphInitial,
|
||||
// @ts-ignore investigate zustand types
|
||||
graphIgnore,
|
||||
// @ts-ignore investigate zustand types
|
||||
graphListen,
|
||||
// @ts-ignore investigate zustand types
|
||||
graphWatchSelf,
|
||||
// @ts-ignore investigate zustand types
|
||||
graphMentions
|
||||
]);
|
||||
}
|
||||
const groupHookData = _.get(json, 'hark-group-hook-update', false);
|
||||
if (groupHookData) {
|
||||
reduceState<HarkState, any>(useHarkState, groupHookData, [
|
||||
// @ts-ignore investigate zustand types
|
||||
groupInitial,
|
||||
// @ts-ignore investigate zustand types
|
||||
groupListen,
|
||||
// @ts-ignore investigate zustand types
|
||||
groupIgnore
|
||||
]);
|
||||
}
|
||||
};
|
||||
export const reduceGraph = [
|
||||
graphInitial,
|
||||
graphIgnore,
|
||||
graphListen,
|
||||
graphWatchSelf,
|
||||
graphMentions
|
||||
];
|
||||
|
||||
export const reduceGroup = [
|
||||
groupInitial,
|
||||
groupListen,
|
||||
groupIgnore
|
||||
];
|
||||
|
||||
|
@ -1,18 +1,24 @@
|
||||
import { NotificationGraphConfig, Timebox, Unreads, dateToDa } from "@urbit/api";
|
||||
import {
|
||||
NotificationGraphConfig,
|
||||
Timebox,
|
||||
Unreads
|
||||
} from '@urbit/api';
|
||||
import { patp2dec } from 'urbit-ob';
|
||||
import BigIntOrderedMap from "@urbit/api/lib/BigIntOrderedMap";
|
||||
import {useCallback} from "react";
|
||||
import _ from 'lodash';
|
||||
import BigIntOrderedMap from '@urbit/api/lib/BigIntOrderedMap';
|
||||
import api from '~/logic/api';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
// import { harkGraphHookReducer, harkGroupHookReducer, harkReducer } from "~/logic/subscription/hark";
|
||||
import { createState } from './base';
|
||||
import { createState, createSubscription, reduceState, reduceStateN } from './base';
|
||||
import { reduce, reduceGraph, reduceGroup } from '../reducers/hark-update';
|
||||
|
||||
export const HARK_FETCH_MORE_COUNT = 3;
|
||||
|
||||
export interface HarkState {
|
||||
archivedNotifications: BigIntOrderedMap<Timebox>;
|
||||
doNotDisturb: boolean;
|
||||
// getMore: () => Promise<boolean>;
|
||||
// getSubset: (offset: number, count: number, isArchive: boolean) => Promise<void>;
|
||||
getMore: () => Promise<boolean>;
|
||||
getSubset: (offset: number, count: number, isArchive: boolean) => Promise<void>;
|
||||
// getTimeSubset: (start?: Date, end?: Date) => Promise<void>;
|
||||
notifications: BigIntOrderedMap<Timebox>;
|
||||
unreadNotes: Timebox;
|
||||
@ -22,57 +28,80 @@ export interface HarkState {
|
||||
unreads: Unreads;
|
||||
}
|
||||
|
||||
const useHarkState = createState<HarkState>('Hark', {
|
||||
archivedNotifications: new BigIntOrderedMap<Timebox>(),
|
||||
doNotDisturb: false,
|
||||
unreadNotes: [],
|
||||
// getMore: async (): Promise<boolean> => {
|
||||
// const state = get();
|
||||
// const offset = state.notifications.size || 0;
|
||||
// await state.getSubset(offset, HARK_FETCH_MORE_COUNT, false);
|
||||
// // TODO make sure that state has mutated at this point.
|
||||
// return offset === (state.notifications.size || 0);
|
||||
// },
|
||||
// getSubset: async (offset, count, isArchive): Promise<void> => {
|
||||
// const api = useApi();
|
||||
// const where = isArchive ? 'archive' : 'inbox';
|
||||
// const result = await api.scry({
|
||||
// app: 'hark-store',
|
||||
// path: `/recent/${where}/${offset}/${count}`
|
||||
// });
|
||||
// harkReducer(result);
|
||||
// return;
|
||||
// },
|
||||
// getTimeSubset: async (start, end): Promise<void> => {
|
||||
// const api = useApi();
|
||||
// const s = start ? dateToDa(start) : '-';
|
||||
// const e = end ? dateToDa(end) : '-';
|
||||
// const result = await api.scry({
|
||||
// app: 'hark-hook',
|
||||
// path: `/recent/${s}/${e}`
|
||||
// });
|
||||
// harkGroupHookReducer(result);
|
||||
// harkGraphHookReducer(result);
|
||||
// return;
|
||||
// },
|
||||
notifications: new BigIntOrderedMap<Timebox>(),
|
||||
notificationsCount: 0,
|
||||
notificationsGraphConfig: {
|
||||
watchOnSelf: false,
|
||||
mentions: false,
|
||||
watching: []
|
||||
},
|
||||
notificationsGroupConfig: [],
|
||||
unreads: {
|
||||
graph: {},
|
||||
group: {}
|
||||
}
|
||||
}, ['unreadNotes', 'notifications', 'archivedNotifications', 'unreads', 'notificationsCount']);
|
||||
const useHarkState = createState<HarkState>(
|
||||
'Hark',
|
||||
(set, get) => ({
|
||||
archivedNotifications: new BigIntOrderedMap<Timebox>(),
|
||||
doNotDisturb: false,
|
||||
unreadNotes: [],
|
||||
getMore: async (): Promise<boolean> => {
|
||||
const state = get();
|
||||
const offset = state.notifications.size || 0;
|
||||
await state.getSubset(offset, HARK_FETCH_MORE_COUNT, false);
|
||||
const newState = get();
|
||||
return offset === (newState?.notifications?.size || 0);
|
||||
},
|
||||
getSubset: async (offset, count, isArchive): Promise<void> => {
|
||||
const where = isArchive ? 'archive' : 'inbox';
|
||||
const { harkUpdate } = await api.scry({
|
||||
app: 'hark-store',
|
||||
path: `/recent/${where}/${offset}/${count}`
|
||||
});
|
||||
reduceState(useHarkState, harkUpdate, [reduce]);
|
||||
},
|
||||
notifications: new BigIntOrderedMap<Timebox>(),
|
||||
notificationsCount: 0,
|
||||
notificationsGraphConfig: {
|
||||
watchOnSelf: false,
|
||||
mentions: false,
|
||||
watching: []
|
||||
},
|
||||
notificationsGroupConfig: [],
|
||||
unreads: {
|
||||
graph: {},
|
||||
group: {}
|
||||
}
|
||||
}),
|
||||
[
|
||||
'unreadNotes',
|
||||
'notifications',
|
||||
'archivedNotifications',
|
||||
'unreads',
|
||||
'notificationsCount'
|
||||
],
|
||||
[
|
||||
(set, get) => createSubscription('hark-store', '/updates', (j) => {
|
||||
const d = _.get(j, 'harkUpdate', false);
|
||||
if (d) {
|
||||
reduceStateN(get(), d, [reduce]);
|
||||
}
|
||||
}),
|
||||
(set, get) => createSubscription('hark-graph-hook', '/updates', (j) => {
|
||||
const graphHookData = _.get(j, 'hark-graph-hook-update', false);
|
||||
if (graphHookData) {
|
||||
reduceStateN(get(), graphHookData, reduceGraph);
|
||||
}
|
||||
}),
|
||||
(set, get) => createSubscription('hark-group-hook', '/updates', (j) => {
|
||||
const data = _.get(j, 'hark-group-hook-update', false);
|
||||
if (data) {
|
||||
reduceStateN(get(), data, reduceGroup);
|
||||
}
|
||||
})
|
||||
]
|
||||
);
|
||||
|
||||
export function useHarkDm(ship: string) {
|
||||
return useHarkState(useCallback(s => {
|
||||
return s.unreads.graph[`/ship/~${window.ship}/dm-inbox`]?.[`/${patp2dec(ship)}`];
|
||||
}, [ship]));
|
||||
return useHarkState(
|
||||
useCallback(
|
||||
(s) => {
|
||||
return s.unreads.graph[`/ship/~${window.ship}/dm-inbox`]?.[
|
||||
`/${patp2dec(ship)}`
|
||||
];
|
||||
},
|
||||
[ship]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export default useHarkState;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import _ from 'lodash';
|
||||
import { unstable_batchedUpdates } from 'react-dom';
|
||||
import { unstable_batchedUpdates as batchedUpdates } from 'react-dom';
|
||||
import { Cage } from '~/types/cage';
|
||||
import ConnectionReducer from '../reducers/connection';
|
||||
import { ContactReducer } from '../reducers/contact-update';
|
||||
@ -7,7 +7,6 @@ import GcpReducer from '../reducers/gcp-reducer';
|
||||
import { GraphReducer } from '../reducers/graph-update';
|
||||
import GroupReducer from '../reducers/group-update';
|
||||
import { GroupViewReducer } from '../reducers/group-view';
|
||||
import { HarkReducer } from '../reducers/hark-update';
|
||||
import InviteReducer from '../reducers/invite-update';
|
||||
import LaunchReducer from '../reducers/launch-update';
|
||||
import MetadataReducer from '../reducers/metadata-update';
|
||||
@ -45,7 +44,7 @@ export default class GlobalStore extends BaseStore<StoreState> {
|
||||
}
|
||||
|
||||
reduce(data: Cage, state: StoreState) {
|
||||
unstable_batchedUpdates(() => {
|
||||
batchedUpdates(() => {
|
||||
// debug shim
|
||||
const tag = Object.keys(data)[0];
|
||||
const oldActions = this.pastActions[tag] || [];
|
||||
@ -58,7 +57,6 @@ export default class GlobalStore extends BaseStore<StoreState> {
|
||||
this.launchReducer.reduce(data);
|
||||
this.connReducer.reduce(data, this.state);
|
||||
GraphReducer(data);
|
||||
HarkReducer(data);
|
||||
ContactReducer(data);
|
||||
this.settingsReducer.reduce(data);
|
||||
this.gcpReducer.reduce(data);
|
||||
|
@ -17,7 +17,6 @@ export default class GlobalSubscription extends BaseSubscription<StoreState> {
|
||||
this.subscribe('/all', 'contact-store');
|
||||
this.subscribe('/all', 's3-store');
|
||||
this.subscribe('/keys', 'graph-store');
|
||||
this.subscribe('/updates', 'hark-store');
|
||||
this.subscribe('/updates', 'hark-graph-hook');
|
||||
this.subscribe('/updates', 'hark-group-hook');
|
||||
this.subscribe('/all', 'settings-store');
|
||||
|
Loading…
Reference in New Issue
Block a user