notifications: refactor pokes

This commit is contained in:
Liam Fitzgerald 2021-07-22 14:40:06 +10:00
parent 99015ec7bd
commit 1e58dcda58
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
2 changed files with 21 additions and 57 deletions

View File

@ -5,15 +5,9 @@ import {
NotifIndex,
readNote,
Timebox,
Unreads,
setMentions,
listenGroup,
ignoreGroup,
listenGraph,
ignoreGraph,
setWatchOnSelf,
setDoNotDisturb
Unreads
} from '@urbit/api';
import { Poke } from '@urbit/http-api';
import { patp2dec } from 'urbit-ob';
import _ from 'lodash';
import BigIntOrderedMap from '@urbit/api/lib/BigIntOrderedMap';
@ -29,6 +23,7 @@ export const HARK_FETCH_MORE_COUNT = 3;
export interface HarkState {
archivedNotifications: BigIntOrderedMap<Timebox>;
doNotDisturb: boolean;
poke: (poke: Poke<any>) => Promise<void>;
getMore: () => Promise<boolean>;
getSubset: (offset: number, count: number, isArchive: boolean) => Promise<void>;
// getTimeSubset: (start?: Date, end?: Date) => Promise<void>;
@ -41,13 +36,6 @@ export interface HarkState {
archive: (index: NotifIndex, time?: BigInteger) => Promise<void>;
readNote: (index: NotifIndex) => Promise<void>;
readCount: (resource: string, index?: string) => Promise<void>;
setMentions: (mentions: boolean) => Promise<void>;
listenGraph: (graph: string, index: string) => Promise<void>;
ignoreGraph: (graph: string, index: string) => Promise<void>;
listenGroup: (group: string) => Promise<void>;
ignoreGroup: (group: string) => Promise<void>;
watchOnSelf: (watch: boolean) => Promise<void>;
setDoNotDisturb: (dnd: boolean) => Promise<void>;
}
const useHarkState = createState<HarkState>(
@ -56,6 +44,9 @@ const useHarkState = createState<HarkState>(
archivedNotifications: new BigIntOrderedMap<Timebox>(),
doNotDisturb: false,
unreadNotes: [],
poke: async (poke: Poke<any>) => {
await pokeOptimisticallyN(useHarkState, poke, [reduce]);
},
readCount: async (resource: string, index?: string) => {
const poke = markCountAsRead(resource, index);
await pokeOptimisticallyN(useHarkState, poke, [reduce]);
@ -82,34 +73,6 @@ const useHarkState = createState<HarkState>(
});
reduceState(useHarkState, harkUpdate, [reduce]);
},
setMentions: async (mentions) => {
const poke = setMentions(mentions);
pokeOptimisticallyN(useHarkState, poke, [reduce]);
},
listenGroup: async (group) => {
const poke = listenGroup(group);
pokeOptimisticallyN(useHarkState, poke, [reduce]);
},
ignoreGroup: async (group) => {
const poke = ignoreGroup(group);
pokeOptimisticallyN(useHarkState, poke, [reduce]);
},
listenGraph: async (graph, index) => {
const poke = listenGraph(graph, index);
pokeOptimisticallyN(useHarkState, poke, [reduce]);
},
ignoreGraph: async (graph, index) => {
const poke = ignoreGraph(graph, index);
pokeOptimisticallyN(useHarkState, poke, [reduce]);
},
watchOnSelf: async (watch) => {
const poke = setWatchOnSelf(watch);
pokeOptimisticallyN(useHarkState, poke, [reduce]);
},
setDoNotDisturb: async (dnd) => {
const poke = setDoNotDisturb(dnd);
pokeOptimisticallyN(useHarkState, poke, [reduce]);
},
notifications: new BigIntOrderedMap<Timebox>(),
notificationsCount: 0,
notificationsGraphConfig: {

View File

@ -11,6 +11,15 @@ import useHarkState from '~/logic/state/hark';
import { FormikOnBlur } from '~/views/components/FormikOnBlur';
import { BackButton } from './BackButton';
import { GroupChannelPicker } from './GroupChannelPicker';
import {
setMentions,
setWatchOnSelf,
setDoNotDisturb,
listenGraph,
listenGroup,
ignoreGraph,
ignoreGroup
} from '@urbit/api';
interface FormSchema {
mentions: boolean;
@ -36,32 +45,24 @@ export function NotificationPreferences() {
const onSubmit = useCallback(async (values: FormSchema, actions: FormikHelpers<FormSchema>) => {
try {
const {
setMentions,
listenGraph,
listenGroup,
ignoreGraph,
ignoreGroup,
setDoNotDisturb,
watchOnSelf
} = useHarkState.getState();
const { poke } = useHarkState.getState();
if (values.mentions !== graphConfig.mentions) {
setMentions(values.mentions);
poke(setMentions(values.mentions));
}
if (values.watchOnSelf !== graphConfig.watchOnSelf) {
watchOnSelf(values.watchOnSelf);
poke(setWatchOnSelf(values.watchOnSelf));
}
if (values.dnd !== dnd && !_.isUndefined(values.dnd)) {
setDoNotDisturb(values.dnd);
poke(setDoNotDisturb(values.dnd));
}
_.forEach(values.graph, (listen: boolean, graph: string) => {
if(listen !== isWatching(graphConfig, graph)) {
(listen ? listenGraph : ignoreGraph)(graph, '/');
poke((listen ? listenGraph : ignoreGraph)(graph, '/'));
}
});
_.forEach(values.groups, (listen: boolean, group: string) => {
if(listen !== groupConfig.includes(group)) {
(listen ? listenGroup : ignoreGroup)(group);
poke((listen ? listenGroup : ignoreGroup)(group));
}
});
actions.setStatus({ success: null });