From b3fa5e1e7325294c520e06f7578cdb892af7a409 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Fri, 3 Aug 2018 18:19:37 +0200 Subject: [PATCH] Integrate with embed --- src/core/client/embed/Stream.ts | 7 +++- src/core/client/embed/decorators/index.ts | 2 +- .../client/embed/decorators/withCommentID.ts | 36 ----------------- ...entID.spec.ts => withSetCommentID.spec.ts} | 10 ++--- .../embed/decorators/withSetCommentID.ts | 22 ++++++++++ src/core/client/embed/index.ts | 2 + .../lib/relay/createMutationContainer.tsx | 16 ++++++-- src/core/client/stream/components/Logo.tsx | 22 ---------- .../stream/components/PermalinkView.css | 5 +-- .../stream/components/PermalinkView.tsx | 40 +++++-------------- src/core/client/stream/components/Stream.tsx | 2 - .../stream/mutations/SetCommentIDMutation.ts | 12 +++++- .../components/ClickOutside/ClickOutside.tsx | 4 +- .../ui/components/ClickOutside/index.ts | 2 +- 14 files changed, 74 insertions(+), 108 deletions(-) delete mode 100644 src/core/client/embed/decorators/withCommentID.ts rename src/core/client/embed/decorators/{withCommentID.spec.ts => withSetCommentID.spec.ts} (82%) create mode 100644 src/core/client/embed/decorators/withSetCommentID.ts delete mode 100644 src/core/client/stream/components/Logo.tsx diff --git a/src/core/client/embed/Stream.ts b/src/core/client/embed/Stream.ts index 61db1863d..ae47e450e 100644 --- a/src/core/client/embed/Stream.ts +++ b/src/core/client/embed/Stream.ts @@ -5,9 +5,9 @@ import { Decorator, withAutoHeight, withClickEvent, - withCommentID, withEventEmitter, withIOSSafariWidthWorkaround, + withSetCommentID, } from "./decorators"; import PymControl from "./PymControl"; import { ensureEndSlash } from "./utils"; @@ -15,6 +15,7 @@ import { ensureEndSlash } from "./utils"; interface CreatePymControlConfig { assetID?: string; assetURL?: string; + commentID?: string; title?: string; eventEmitter: EventEmitter2; id: string; @@ -26,13 +27,14 @@ export function createPymControl(config: CreatePymControlConfig) { withIOSSafariWidthWorkaround, withAutoHeight, withClickEvent, - withCommentID, + withSetCommentID, withEventEmitter(config.eventEmitter), ]; const query = qs.stringify({ assetID: config.assetID, assetURL: config.assetURL, + commentID: config.commentID, }); const url = `${ensureEndSlash(config.rootURL)}stream.html?${query}`; return new PymControl({ @@ -73,6 +75,7 @@ export type StreamInterface = ReturnType; export interface CreateConfig { assetID?: string; assetURL?: string; + commentID?: string; title?: string; eventEmitter: EventEmitter2; id: string; diff --git a/src/core/client/embed/decorators/index.ts b/src/core/client/embed/decorators/index.ts index 59dbac12c..6e2d45195 100644 --- a/src/core/client/embed/decorators/index.ts +++ b/src/core/client/embed/decorators/index.ts @@ -4,7 +4,7 @@ export type CleanupCallback = () => void; export type Decorator = (pym: pym.Parent) => CleanupCallback | void; export { default as withAutoHeight } from "./withAutoHeight"; export { default as withClickEvent } from "./withClickEvent"; -export { default as withCommentID } from "./withCommentID"; +export { default as withSetCommentID } from "./withSetCommentID"; export { default as withEventEmitter } from "./withEventEmitter"; export { default as withIOSSafariWidthWorkaround, diff --git a/src/core/client/embed/decorators/withCommentID.ts b/src/core/client/embed/decorators/withCommentID.ts deleted file mode 100644 index 3fa968374..000000000 --- a/src/core/client/embed/decorators/withCommentID.ts +++ /dev/null @@ -1,36 +0,0 @@ -import qs from "query-string"; - -import { buildURL } from "../utils"; -import { Decorator } from "./"; - -const withCommentID: Decorator = pym => { - // Remove the comment id from the query. - pym.onMessage("view-all-comments", () => { - const search = qs.stringify({ - ...qs.parse(location.search), - commentId: undefined, - }); - - // Remove the commentId url param. - const url = buildURL({ search }); - - // Change the url. - window.history.replaceState({}, document.title, url); - }); - - // Add the permalink comment id to the query. - pym.onMessage("view-comment", (id: string) => { - const search = qs.stringify({ - ...qs.parse(location.search), - commentId: id, - }); - - // Remove the commentId url param. - const url = buildURL({ search }); - - // Change the url. - window.history.replaceState({}, document.title, url); - }); -}; - -export default withCommentID; diff --git a/src/core/client/embed/decorators/withCommentID.spec.ts b/src/core/client/embed/decorators/withSetCommentID.spec.ts similarity index 82% rename from src/core/client/embed/decorators/withCommentID.spec.ts rename to src/core/client/embed/decorators/withSetCommentID.spec.ts index 2c5802aa0..1c6b92281 100644 --- a/src/core/client/embed/decorators/withCommentID.spec.ts +++ b/src/core/client/embed/decorators/withSetCommentID.spec.ts @@ -1,16 +1,16 @@ -import withCommentID from "./withCommentID"; +import withSetCommentID from "./withSetCommentID"; it("should add commentID", () => { const previousLocation = location.toString(); const previousState = window.history.state; const fakePym = { onMessage: (type: string, callback: (id: string) => void) => { - if (type === "view-comment") { + if (type === "setCommentID") { callback("comment-id"); } }, }; - withCommentID(fakePym as any); + withSetCommentID(fakePym as any); expect(location.toString()).toBe("http://localhost/?commentId=comment-id"); window.history.replaceState(previousState, document.title, previousLocation); }); @@ -25,12 +25,12 @@ it("should remove commentID", () => { ); const fakePym = { onMessage: (type: string, callback: () => void) => { - if (type === "view-all-comments") { + if (type === "setCommentID") { callback(); } }, }; - withCommentID(fakePym as any); + withSetCommentID(fakePym as any); expect(location.toString()).toBe("http://localhost/"); window.history.replaceState(previousState, document.title, previousLocation); }); diff --git a/src/core/client/embed/decorators/withSetCommentID.ts b/src/core/client/embed/decorators/withSetCommentID.ts new file mode 100644 index 000000000..b2b6812a3 --- /dev/null +++ b/src/core/client/embed/decorators/withSetCommentID.ts @@ -0,0 +1,22 @@ +import qs from "query-string"; + +import { buildURL } from "../utils"; +import { Decorator } from "./"; + +const withSetCommentID: Decorator = pym => { + // Add the permalink comment id to the query. + pym.onMessage("setCommentID", (id: string) => { + const search = qs.stringify({ + ...qs.parse(location.search), + commentID: id || undefined, + }); + + // Remove the commentId url param. + const url = buildURL({ search }); + + // Change the url. + window.history.replaceState({}, document.title, url); + }); +}; + +export default withSetCommentID; diff --git a/src/core/client/embed/index.ts b/src/core/client/embed/index.ts index 6c6ebc36d..c440f72a8 100644 --- a/src/core/client/embed/index.ts +++ b/src/core/client/embed/index.ts @@ -6,6 +6,7 @@ import createStreamInterface from "./Stream"; export interface Config { assetID?: string; assetURL?: string; + commentID?: string; rootURL?: string; id?: string; events?: (eventEmitter: EventEmitter2) => void; @@ -23,6 +24,7 @@ export function render(config: Config = {}) { return createStreamInterface({ assetID: config.assetID || query.assetID, assetURL: config.assetURL || query.assetURL, + commentID: config.commentID || query.commentID, id: config.id || "talk-embed-stream", rootURL: config.rootURL || location.origin, eventEmitter, diff --git a/src/core/client/framework/lib/relay/createMutationContainer.tsx b/src/core/client/framework/lib/relay/createMutationContainer.tsx index 0446e7d17..b8c74be18 100644 --- a/src/core/client/framework/lib/relay/createMutationContainer.tsx +++ b/src/core/client/framework/lib/relay/createMutationContainer.tsx @@ -7,7 +7,7 @@ import { } from "recompose"; import { Environment } from "relay-runtime"; -import { withContext } from "../bootstrap"; +import { TalkContext, withContext } from "../bootstrap"; /** * createMutationContainer creates a HOC that @@ -18,10 +18,14 @@ import { withContext } from "../bootstrap"; */ function createMutationContainer( propName: T, - commit: (environment: Environment, input: I) => Promise + commit: ( + environment: Environment, + input: I, + context: TalkContext + ) => Promise ): InferableComponentEnhancer<{ [P in T]: (input: I) => Promise }> { return compose( - withContext(({ relayEnvironment }) => ({ relayEnvironment })), + withContext(context => ({ context })), hoistStatics((BaseComponent: React.ComponentType) => { class CreateMutationContainer extends React.Component { public static displayName = wrapDisplayName( @@ -30,7 +34,11 @@ function createMutationContainer( ); private commit = (input: I) => { - return commit(this.props.relayEnvironment, input); + return commit( + this.props.context.relayEnvironment, + input, + this.props.context + ); }; public render() { diff --git a/src/core/client/stream/components/Logo.tsx b/src/core/client/stream/components/Logo.tsx deleted file mode 100644 index 2fe4dcac0..000000000 --- a/src/core/client/stream/components/Logo.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import { Localized } from "fluent-react/compat"; -import * as React from "react"; -import { StatelessComponent } from "react"; - -import { Typography } from "talk-ui/components"; - -export interface LogoProps { - className?: string; - gutterBottom?: boolean; -} - -const Logo: StatelessComponent = props => { - return ( - - ); -}; - -export default Logo; diff --git a/src/core/client/stream/components/PermalinkView.css b/src/core/client/stream/components/PermalinkView.css index a0c8f8cb8..1e9f3ca45 100644 --- a/src/core/client/stream/components/PermalinkView.css +++ b/src/core/client/stream/components/PermalinkView.css @@ -4,7 +4,6 @@ margin: calc(0.5 * var(--spacing-unit)) auto; } -.comment, -.commentNotFound { - margin: calc(5 * var(--spacing-unit)) auto; +.button { + margin-bottom: calc(2 * var(--spacing-unit)); } diff --git a/src/core/client/stream/components/PermalinkView.tsx b/src/core/client/stream/components/PermalinkView.tsx index 10c2f8470..68396ac74 100644 --- a/src/core/client/stream/components/PermalinkView.tsx +++ b/src/core/client/stream/components/PermalinkView.tsx @@ -1,6 +1,5 @@ import React, { StatelessComponent } from "react"; -import Logo from "talk-stream/components/Logo"; import { Button, Flex, Typography } from "talk-ui/components"; import CommentContainer from "../containers/CommentContainer"; @@ -17,44 +16,25 @@ const PermalinkView: StatelessComponent = ({ comment, onShowAllComments, }) => { - if (comment) { - return ( -
- - {assetURL && ( - - )} - - - -
- ); - } - return (
- - - Comment not found - {assetURL && ( )} + {!comment && Comment not found} + {comment && ( + + + + )}
); }; diff --git a/src/core/client/stream/components/Stream.tsx b/src/core/client/stream/components/Stream.tsx index 6601f4341..ef1ed5bda 100644 --- a/src/core/client/stream/components/Stream.tsx +++ b/src/core/client/stream/components/Stream.tsx @@ -7,7 +7,6 @@ import { Button, Flex } from "talk-ui/components"; import CommentContainer from "../containers/CommentContainer"; import PostCommentFormContainer from "../containers/PostCommentFormContainer"; import ReplyListContainer from "../containers/ReplyListContainer"; -import Logo from "./Logo"; import * as styles from "./Stream.css"; export interface StreamProps { @@ -22,7 +21,6 @@ export interface StreamProps { const Stream: StatelessComponent = props => { return (
- Promise; -async function commit(environment: Environment, input: SetCommentIDInput) { +async function commit( + environment: Environment, + input: SetCommentIDInput, + { pym }: TalkContext +) { return commitLocalUpdate(environment, store => { const record = store.get(LOCAL_ID)!; record.setValue(input.id, "commentID"); + if (pym) { + // This sets the comment id on the parent url. + pym.sendMessage("setCommentID", input.id || ""); + } }); } diff --git a/src/core/client/ui/components/ClickOutside/ClickOutside.tsx b/src/core/client/ui/components/ClickOutside/ClickOutside.tsx index afcda2695..275b5fdb0 100644 --- a/src/core/client/ui/components/ClickOutside/ClickOutside.tsx +++ b/src/core/client/ui/components/ClickOutside/ClickOutside.tsx @@ -65,7 +65,9 @@ export class ClickOutside extends React.Component { } } -const ClickOutsideWithContext: StatelessComponent = props => ( +const ClickOutsideWithContext: StatelessComponent< + ClickOutsideProps +> = props => ( {({ registerClickFarAway }) => ( diff --git a/src/core/client/ui/components/ClickOutside/index.ts b/src/core/client/ui/components/ClickOutside/index.ts index b70132125..91803ea08 100644 --- a/src/core/client/ui/components/ClickOutside/index.ts +++ b/src/core/client/ui/components/ClickOutside/index.ts @@ -1 +1 @@ -export { default as ClickOutside, ClickFarAwayRegister } from "./ClickOutside"; +export { default, ClickFarAwayRegister } from "./ClickOutside";