session-desktop/ts/components/session/SessionInboxView.tsx

145 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-11-13 04:29:59 +01:00
import React from 'react';
import { Provider } from 'react-redux';
import { bindActionCreators } from 'redux';
2021-03-04 23:24:25 +01:00
import { ConversationModel } from '../../models/conversation';
import { getConversationController } from '../../session/conversations';
2021-01-29 01:29:24 +01:00
import { UserUtils } from '../../session/utils';
2020-11-13 04:29:59 +01:00
import { createStore } from '../../state/createStore';
2020-11-17 03:30:24 +01:00
import { actions as conversationActions } from '../../state/ducks/conversations';
import { initialDefaultRoomState } from '../../state/ducks/defaultRooms';
import { initialModalState } from '../../state/ducks/modalDialog';
import { initialOnionPathState } from '../../state/ducks/onion';
import { initialSearchState } from '../../state/ducks/search';
import { initialSectionState } from '../../state/ducks/section';
import { initialThemeState } from '../../state/ducks/theme';
import { initialUserConfigState } from '../../state/ducks/userConfig';
import { StateType } from '../../state/reducer';
2021-02-08 06:18:36 +01:00
import { makeLookup } from '../../util';
2021-03-16 07:22:46 +01:00
import { LeftPane } from '../LeftPane';
import { SessionMainPanel } from '../SessionMainPanel';
2020-11-13 04:29:59 +01:00
2021-06-24 09:06:21 +02:00
// tslint:disable-next-line: no-submodule-imports
import { PersistGate } from 'redux-persist/integration/react';
import { persistStore } from 'redux-persist';
2021-07-06 06:40:45 +02:00
import { TimerOptionsArray, TimerOptionsState } from '../../state/ducks/timerOptions';
2020-11-13 04:29:59 +01:00
// Workaround: A react component's required properties are filtering up through connect()
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31363
type State = {
isInitialLoadComplete: boolean;
isExpired: boolean;
2020-11-13 04:29:59 +01:00
};
export class SessionInboxView extends React.Component<any, State> {
private store: any;
constructor(props: any) {
super(props);
this.state = {
isInitialLoadComplete: false,
isExpired: false,
};
void this.setupLeftPane();
// not reactified yet. this is a callback called once we were able to check for expiration of this Session version
window.extension.expired((expired: boolean) => {
if (expired) {
this.setState({
isExpired: true,
});
}
});
}
2020-11-13 04:29:59 +01:00
public render() {
if (!this.state.isInitialLoadComplete) {
return <></>;
}
2020-11-13 04:29:59 +01:00
2021-06-24 09:06:21 +02:00
const persistor = persistStore(this.store);
return (
<Provider store={this.store}>
<PersistGate loading={null} persistor={persistor}>
<div className="gutter">
<div className="network-status-container" />
{this.renderLeftPane()}
</div>
<SessionMainPanel />
</PersistGate>
</Provider>
);
}
2020-11-13 04:29:59 +01:00
private renderLeftPane() {
2021-03-16 07:22:46 +01:00
return <LeftPane isExpired={this.state.isExpired} />;
}
private async setupLeftPane() {
// Here we set up a full redux store with initial state for our LeftPane Root
const convoCollection = getConversationController().getConversations();
2021-04-22 10:03:58 +02:00
const conversations = convoCollection.map((conversation: ConversationModel) =>
conversation.getProps()
);
2021-02-08 06:18:36 +01:00
const filledConversations = conversations.map((conv: any) => {
return { ...conv, messages: [] };
});
const fullFilledConversations = await Promise.all(filledConversations);
2021-07-06 06:40:45 +02:00
const timerOptions: TimerOptionsArray = window.Whisper.ExpirationTimerOptions.map(
(item: any) => ({
name: item.getName(),
value: item.get('seconds'),
})
);
const initialState: StateType = {
conversations: {
2021-02-08 06:18:36 +01:00
conversationLookup: makeLookup(fullFilledConversations, 'id'),
messages: [],
showRightPanel: false,
messageDetailProps: undefined,
selectedMessageIds: [],
selectedConversation: undefined,
2021-07-20 08:58:51 +02:00
areMoreMessagesBeingFetched: false,
showScrollButton: false,
animateQuotedMessageId: undefined,
lightBox: undefined,
nextMessageToPlay: undefined,
quotedMessage: undefined,
mentionMembers: [],
firstUnreadMessageId: undefined,
},
user: {
2021-01-29 01:29:24 +01:00
ourNumber: UserUtils.getOurPubKeyStrFromCache(),
},
section: initialSectionState,
defaultRooms: initialDefaultRoomState,
search: initialSearchState,
theme: initialThemeState,
onionPaths: initialOnionPathState,
modals: initialModalState,
userConfig: initialUserConfigState,
2021-07-06 06:40:45 +02:00
timerOptions: {
timerOptions,
},
};
this.store = createStore(initialState);
window.inboxStore = this.store;
// Enables our redux store to be updated by backbone events in the outside world
2021-04-22 10:03:58 +02:00
const { messageExpired } = bindActionCreators(conversationActions, this.store.dispatch);
window.actionsCreators = conversationActions;
// messageExpired is currently inboked fropm js. So we link it to Redux that way
window.Whisper.events.on('messageExpired', messageExpired);
this.setState({ isInitialLoadComplete: true });
}
2020-11-13 04:29:59 +01:00
}