mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-14 17:41:33 +03:00
interface: cleaned up unused store state and reducers for links
This commit is contained in:
parent
97e1fa12a9
commit
5352272be7
@ -1,212 +0,0 @@
|
||||
import _ from 'lodash';
|
||||
import { StoreState } from '../../store/type';
|
||||
import { LinkUpdate, Pagination } from '~/types/link-update';
|
||||
|
||||
// page size as expected from link-view.
|
||||
// must change in parallel with the +page-size in /app/link-view to
|
||||
// ensure sane behavior.
|
||||
const PAGE_SIZE = 25;
|
||||
|
||||
type LinkState = Pick<StoreState, 'linksSeen' | 'links' | 'linkListening' | 'linkComments'>;
|
||||
|
||||
export default class LinkUpdateReducer<S extends LinkState> {
|
||||
reduce(json: any, state: S) {
|
||||
const data = _.get(json, 'link-update', false);
|
||||
if(data) {
|
||||
this.submissionsPage(data, state);
|
||||
this.submissionsUpdate(data, state);
|
||||
this.discussionsPage(data, state);
|
||||
this.discussionsUpdate(data, state);
|
||||
this.observationUpdate(data, state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
submissionsPage(json: LinkUpdate, state: S) {
|
||||
const data = _.get(json, 'initial-submissions', false);
|
||||
if (data) {
|
||||
// { "initial-submissions": {
|
||||
// "/~ship/group": {
|
||||
// page: [{ship, timestamp, title, url}]
|
||||
// page-number: 0
|
||||
// total-items: 1
|
||||
// total-pages: 1
|
||||
// }
|
||||
// } }
|
||||
|
||||
for (var path of Object.keys(data)) {
|
||||
const here = data[path];
|
||||
const page = here.pageNumber;
|
||||
|
||||
// if we didn't have any state for this path yet, initialize.
|
||||
if (!state.links[path]) {
|
||||
state.links[path] = {
|
||||
local: {},
|
||||
totalItems: here.totalItems,
|
||||
totalPages: here.totalPages,
|
||||
unseenCount: here.unseenCount
|
||||
};
|
||||
}
|
||||
|
||||
// since data contains an up-to-date full version of the page,
|
||||
// we can safely overwrite the one in state.
|
||||
if (typeof page === 'number' && here.page) {
|
||||
state.links[path][page] = here.page;
|
||||
state.links[path].local[page] = false;
|
||||
}
|
||||
state.links[path].totalPages = here.totalPages;
|
||||
state.links[path].totalItems = here.totalItems;
|
||||
state.links[path].unseenCount = here.unseenCount;
|
||||
|
||||
// write seen status to a separate structure,
|
||||
// for easier modification later.
|
||||
if (!state.linksSeen[path]) {
|
||||
state.linksSeen[path] = {};
|
||||
}
|
||||
(here.page || []).map((submission) => {
|
||||
state.linksSeen[path][submission.url] = submission.seen;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
submissionsUpdate(json: LinkUpdate, state: S) {
|
||||
const data = _.get(json, 'submissions', false);
|
||||
if (data) {
|
||||
// { "submissions": {
|
||||
// path: /~ship/group
|
||||
// pages: [{ship, timestamp, title, url}]
|
||||
// } }
|
||||
|
||||
const path = data.path;
|
||||
|
||||
// stub in a comment count, which is more or less guaranteed to be 0
|
||||
data.pages = data.pages.map((submission) => {
|
||||
submission.commentCount = 0;
|
||||
state.linksSeen[path][submission.url] = false;
|
||||
return submission;
|
||||
});
|
||||
|
||||
// add the new submissions to state, update totals
|
||||
state.links[path] = this._addNewItems(
|
||||
data.pages, state.links[path]
|
||||
);
|
||||
state.links[path].unseenCount =
|
||||
(state.links[path].unseenCount || 0) + data.pages.length;
|
||||
}
|
||||
}
|
||||
|
||||
discussionsPage(json: LinkUpdate, state: S) {
|
||||
const data = _.get(json, 'initial-discussions', false);
|
||||
if (data) {
|
||||
// { "initial-discussions": {
|
||||
// path: "/~ship/group"
|
||||
// url: https://urbit.org/
|
||||
// page: [{ship, timestamp, title, url}]
|
||||
// page-number: 0
|
||||
// total-items: 1
|
||||
// total-pages: 1
|
||||
// } }
|
||||
|
||||
const path = data.path;
|
||||
const url = data.url;
|
||||
const page = data.pageNumber;
|
||||
|
||||
// if we didn't have any state for this path yet, initialize.
|
||||
if (!state.linkComments[path]) {
|
||||
state.linkComments[path] = {};
|
||||
}
|
||||
let comments = {...{
|
||||
local: {},
|
||||
totalPages: data.totalPages,
|
||||
totalItems: data.totalItems
|
||||
}, ...state.linkComments[path][url] };
|
||||
|
||||
state.linkComments[path][url] = comments;
|
||||
const here = state.linkComments[path][url];
|
||||
|
||||
// since data contains an up-to-date full version of the page,
|
||||
// we can safely overwrite the one in state.
|
||||
here[page] = data.page;
|
||||
here.local[page] = false;
|
||||
here.totalPages = data.totalPages;
|
||||
here.totalItems = data.totalItems;
|
||||
}
|
||||
}
|
||||
|
||||
discussionsUpdate(json: LinkUpdate, state: S) {
|
||||
const data = _.get(json, 'discussions', false);
|
||||
if (data) {
|
||||
// { "discussions": {
|
||||
// path: /~ship/path
|
||||
// url: 'https://urbit.org'
|
||||
// comments: [{ship, timestamp, udon}]
|
||||
// } }
|
||||
|
||||
const path = data.path;
|
||||
const url = data.url;
|
||||
|
||||
// add new comments to state, update totals
|
||||
state.linkComments[path][url] = this._addNewItems(
|
||||
data.comments || [], state.linkComments[path][url]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
observationUpdate(json: LinkUpdate, state: S) {
|
||||
const data = _.get(json, 'observation', false);
|
||||
if (data) {
|
||||
// { "observation": {
|
||||
// path: /~ship/path
|
||||
// urls: ['https://urbit.org']
|
||||
// } }
|
||||
|
||||
const path = data.path;
|
||||
if (!state.linksSeen[path]) {
|
||||
state.linksSeen[path] = {};
|
||||
}
|
||||
const seen = state.linksSeen[path];
|
||||
|
||||
// mark urls as seen
|
||||
data.urls.map((url) => {
|
||||
seen[url] = true;
|
||||
});
|
||||
if (state.links[path]) {
|
||||
state.links[path].unseenCount =
|
||||
state.links[path].unseenCount - data.urls.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
_addNewItems<S extends { time: number }>(items: S[], pages: Pagination<S>, page = 0) {
|
||||
if (!pages) {
|
||||
pages = {
|
||||
local: {},
|
||||
totalPages: 0,
|
||||
totalItems: 0
|
||||
};
|
||||
}
|
||||
const i = page;
|
||||
if (!pages[i]) {
|
||||
pages[i] = [];
|
||||
// if we know this page exists in the backend, flag it as "local",
|
||||
// so that we know to initiate a "fetch the rest" request when we want
|
||||
// to display the page.
|
||||
pages.local[i] = (page < pages.totalPages);
|
||||
}
|
||||
pages[i] = items.concat(pages[i]);
|
||||
pages[i].sort((a, b) => b.time - a.time);
|
||||
pages.totalItems = pages.totalItems + items.length;
|
||||
if (pages[i].length <= PAGE_SIZE) {
|
||||
pages.totalPages = Math.ceil(pages.totalItems / PAGE_SIZE);
|
||||
return pages;
|
||||
}
|
||||
// overflow into next page
|
||||
const tail = pages[i].slice(PAGE_SIZE);
|
||||
pages[i].length = PAGE_SIZE;
|
||||
pages.totalItems = pages.totalItems - tail.length;
|
||||
return this._addNewItems(tail, pages, page+1);
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
import _ from 'lodash';
|
||||
import { StoreState } from '../../store/type';
|
||||
import { Cage } from '~/types/cage';
|
||||
import { LinkListenUpdate } from '~/types/link-listen-update';
|
||||
|
||||
type LinkListenState = Pick<StoreState, 'linkListening'>;
|
||||
|
||||
export default class LinkListenReducer<S extends LinkListenState> {
|
||||
reduce(json: Cage, state: S) {
|
||||
const data = _.get(json, 'link-listen-update', false);
|
||||
if (data) {
|
||||
this.listening(data, state);
|
||||
this.watch(data, state);
|
||||
this.leave(data, state);
|
||||
}
|
||||
}
|
||||
|
||||
listening(json: LinkListenUpdate, state: S) {
|
||||
const data = _.get(json, 'listening', false);
|
||||
if (data) {
|
||||
state.linkListening = new Set(data);
|
||||
}
|
||||
}
|
||||
|
||||
watch(json: LinkListenUpdate, state: S) {
|
||||
const data = _.get(json, 'watch', false);
|
||||
if (data) {
|
||||
state.linkListening.add(data);
|
||||
}
|
||||
}
|
||||
|
||||
leave(json: LinkListenUpdate, state: S) {
|
||||
const data = _.get(json, 'leave', false);
|
||||
if (data) {
|
||||
state.linkListening.delete(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import ChatReducer from '../reducers/chat-update';
|
||||
import { StoreState } from './type';
|
||||
import { Cage } from '~/types/cage';
|
||||
import ContactReducer from '../reducers/contact-update';
|
||||
import LinkUpdateReducer from '../reducers/link-update';
|
||||
import S3Reducer from '../reducers/s3-update';
|
||||
import { GraphReducer } from '../reducers/graph-update';
|
||||
import GroupReducer from '../reducers/group-update';
|
||||
@ -15,7 +14,6 @@ import PermissionReducer from '../reducers/permission-update';
|
||||
import PublishUpdateReducer from '../reducers/publish-update';
|
||||
import PublishResponseReducer from '../reducers/publish-response';
|
||||
import LaunchReducer from '../reducers/launch-update';
|
||||
import LinkListenReducer from '../reducers/listen-update';
|
||||
import ConnectionReducer from '../reducers/connection';
|
||||
|
||||
export const homeAssociation = {
|
||||
@ -38,8 +36,6 @@ export default class GlobalStore extends BaseStore<StoreState> {
|
||||
localReducer = new LocalReducer();
|
||||
chatReducer = new ChatReducer();
|
||||
contactReducer = new ContactReducer();
|
||||
linkReducer = new LinkUpdateReducer();
|
||||
linkListenReducer = new LinkListenReducer();
|
||||
s3Reducer = new S3Reducer();
|
||||
groupReducer = new GroupReducer();
|
||||
permissionReducer = new PermissionReducer();
|
||||
@ -100,10 +96,6 @@ export default class GlobalStore extends BaseStore<StoreState> {
|
||||
},
|
||||
credentials: null
|
||||
},
|
||||
links: {},
|
||||
linksSeen: {},
|
||||
linkListening: new Set(),
|
||||
linkComments: {},
|
||||
notebooks: {},
|
||||
contacts: {},
|
||||
dark: false,
|
||||
@ -118,14 +110,12 @@ export default class GlobalStore extends BaseStore<StoreState> {
|
||||
this.localReducer.reduce(data, this.state);
|
||||
this.chatReducer.reduce(data, this.state);
|
||||
this.contactReducer.reduce(data, this.state);
|
||||
this.linkReducer.reduce(data, this.state);
|
||||
this.s3Reducer.reduce(data, this.state);
|
||||
this.groupReducer.reduce(data, this.state);
|
||||
this.permissionReducer.reduce(data, this.state);
|
||||
this.publishUpdateReducer.reduce(data, this.state);
|
||||
this.publishResponseReducer.reduce(data, this.state);
|
||||
this.launchReducer.reduce(data, this.state);
|
||||
this.linkListenReducer.reduce(data, this.state);
|
||||
this.connReducer.reduce(data, this.state);
|
||||
GraphReducer(data, this.state);
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ import { Groups } from '~/types/group-update';
|
||||
import { S3State } from '~/types/s3-update';
|
||||
import { Permissions } from '~/types/permission-update';
|
||||
import { LaunchState, WeatherState } from '~/types/launch-update';
|
||||
import { LinkComments, LinkCollections, LinkSeen } from '~/types/link-update';
|
||||
import { ConnectionStatus } from '~/types/connection';
|
||||
import { BackgroundConfig, LocalUpdateRemoteContentPolicy } from '~/types/local-update';
|
||||
import {Graphs} from '~/types/graph-update';
|
||||
@ -47,12 +46,6 @@ export interface StoreState {
|
||||
weather: WeatherState | {} | null;
|
||||
userLocation: string | null;
|
||||
|
||||
// links state
|
||||
linksSeen: LinkSeen;
|
||||
linkListening: Set<Path>;
|
||||
links: LinkCollections;
|
||||
linkComments: LinkComments;
|
||||
|
||||
// publish state
|
||||
notebooks: Notebooks;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user