Files
talk/src/core/client/stream/local/initLocalState.ts
T
Kiwi 106a5d36ed [next] Embed plus (#1877)
* Implement StreamEmbed instance and rename library to coral

* Add article & articleButton.html, only show embed htmls in production

* Respect assetURL

* Add tests

* Add parseHashQuery

* Fix permalink query and integration tests

* Fix permalink URL

* Remove optionalparams from pym

* Scroll when showing permalink view

* Implement autoRender

* AutoRender immediately when render permalink

* Add test for `showPermalink` event

* Add comment
2018-09-21 22:43:28 +00:00

79 lines
2.0 KiB
TypeScript

import qs from "query-string";
import { commitLocalUpdate, Environment } from "relay-runtime";
import { TalkContext } from "talk-framework/lib/bootstrap";
import {
createAndRetain,
LOCAL_ID,
LOCAL_TYPE,
} from "talk-framework/lib/relay";
import {
AUTH_POPUP_ID,
AUTH_POPUP_TYPE,
NETWORK_ID,
NETWORK_TYPE,
} from "./constants";
/**
* Initializes the local state, before we start the App.
*/
export default async function initLocalState(
environment: Environment,
{ localStorage }: TalkContext
) {
const authToken = await localStorage!.getItem("authToken");
commitLocalUpdate(environment, s => {
// TODO: (cvle) move local, auth token and network initialization to framework.
const root = s.getRoot();
// Create the Local Record which is the Root for the client states.
const localRecord = createAndRetain(environment, s, LOCAL_ID, LOCAL_TYPE);
root.setLinkedRecord(localRecord, "local");
// Set auth token
localRecord.setValue(authToken || "", "authToken");
// Parse query params
const query = qs.parse(location.search);
if (query.assetID) {
localRecord.setValue(query.assetID, "assetID");
}
if (query.assetURL) {
localRecord.setValue(query.assetURL, "assetURL");
}
if (query.commentID) {
localRecord.setValue(query.commentID, "commentID");
}
// Create network Record
const networkRecord = createAndRetain(
environment,
s,
NETWORK_ID,
NETWORK_TYPE
);
networkRecord.setValue(false, "isOffline");
localRecord.setLinkedRecord(networkRecord, "network");
// Create authPopup Record
const authPopupRecord = createAndRetain(
environment,
s,
AUTH_POPUP_ID,
AUTH_POPUP_TYPE
);
authPopupRecord.setValue(false, "open");
authPopupRecord.setValue(false, "focus");
authPopupRecord.setValue("", "href");
localRecord.setLinkedRecord(authPopupRecord, "authPopup");
// Set active tab
localRecord.setValue("COMMENTS", "activeTab");
});
}