1- import React , { useEffect , useState } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import WebView from 'react-native-webview' ;
3- import { FastCommentsCommentWidgetConfig } from 'fastcomments-typescript' ;
4- import { ActivityIndicator , ColorValue } from 'react-native' ;
5- import { WebViewErrorEvent , WebViewNavigationEvent } from 'react-native-webview/lib/WebViewTypes' ;
3+ import { FastCommentsCommentWidgetConfig } from 'fastcomments-typescript' ;
4+ import { ActivityIndicator , ColorValue } from 'react-native' ;
5+ import {
6+ WebViewErrorEvent ,
7+ WebViewNavigationEvent ,
8+ } from 'react-native-webview/lib/WebViewTypes' ;
69
710export interface FastCommentsWidgetParameters {
811 config : FastCommentsCommentWidgetConfig ;
@@ -11,8 +14,10 @@ export interface FastCommentsWidgetParameters {
1114 onError ?: ( error : WebViewErrorEvent ) => void ;
1215}
1316
14- export function FastCommentsEmbedCore ( props : FastCommentsWidgetParameters , widgetId : string ) {
15-
17+ export function FastCommentsEmbedCore (
18+ props : FastCommentsWidgetParameters ,
19+ widgetId : string
20+ ) {
1621 const [ uri , setURI ] = useState ( '' ) ;
1722
1823 const [ height , setHeight ] = useState ( 500 ) ;
@@ -45,11 +50,11 @@ export function FastCommentsEmbedCore(props: FastCommentsWidgetParameters, widge
4550 function updateHeight ( value : number ) {
4651 if (
4752 // Always handle the widget growing.
48- value > lastHeight
53+ value > lastHeight ||
4954 // Only handle the widget shrinking, if it's by more than 50px.
50- || Math . abs ( value - lastHeight ) > 100
55+ Math . abs ( value - lastHeight ) > 100 ||
5156 // Handle the widget hiding itself.
52- || value === 0
57+ value === 0
5358 ) {
5459 lastHeight = value ;
5560 setHeight ( value ) ;
@@ -68,40 +73,53 @@ export function FastCommentsEmbedCore(props: FastCommentsWidgetParameters, widge
6873 if ( data . type === 'update-height' ) {
6974 updateHeight ( data . height ) ;
7075 } else if ( data . type === 'update-comment-count' ) {
71- configFunctions . commentCountUpdated && configFunctions . commentCountUpdated ( data . count ) ;
76+ configFunctions . commentCountUpdated &&
77+ configFunctions . commentCountUpdated ( data . count ) ;
7278 } else if ( data . type === 'redirect' ) {
7379 configFunctions . openURL && configFunctions . openURL ( data . url ) ;
7480 } else if ( data . type === 'login' ) {
7581 // @ts -ignore
76- configFunctions . loginCallback && configFunctions . loginCallback ( config . instanceId ) ;
82+ // eslint-disable-next-line prettier/prettier
83+ configFunctions . loginCallback && configFunctions . loginCallback ( props . config . instanceId ) ;
7784 } else if ( data . type === 'logout' ) {
7885 // @ts -ignore
79- configFunctions . logoutCallback && configFunctions . logoutCallback ( config . instanceId ) ;
86+ // eslint-disable-next-line prettier/prettier
87+ configFunctions . logoutCallback && configFunctions . logoutCallback ( props . config . instanceId ) ;
8088 } else if ( data . type === 'reply-success' ) {
81- configFunctions . onReplySuccess && configFunctions . onReplySuccess ( data . comment ) ;
89+ configFunctions . onReplySuccess &&
90+ configFunctions . onReplySuccess ( data . comment ) ;
8291 } else if ( data . type === 'vote-success' ) {
83- configFunctions . onVoteSuccess && configFunctions . onVoteSuccess ( data . comment , data . voteId , data . direction , data . status ) ;
92+ configFunctions . onVoteSuccess &&
93+ configFunctions . onVoteSuccess (
94+ data . comment ,
95+ data . voteId ,
96+ data . direction ,
97+ data . status
98+ ) ;
8499 } else if ( data . type === 'on-init' ) {
85100 configFunctions . onInit && configFunctions . onInit ( ) ;
86101 } else if ( data . type === 'on-render' ) {
87102 configFunctions . onRender && configFunctions . onRender ( ) ;
88103 } else if ( data . type === 'on-image-clicked' ) {
89- configFunctions . onImageClicked && configFunctions . onImageClicked ( data . src ) ;
104+ configFunctions . onImageClicked &&
105+ configFunctions . onImageClicked ( data . src ) ;
90106 } else if ( data . type === 'on-authentication-change' ) {
91- configFunctions . onAuthenticationChange && configFunctions . onAuthenticationChange ( data . changeType , data . data ) ;
107+ configFunctions . onAuthenticationChange &&
108+ configFunctions . onAuthenticationChange ( data . changeType , data . data ) ;
92109 } else if ( data . type === 'on-comments-rendered' ) {
93- configFunctions . onCommentsRendered && configFunctions . onCommentsRendered ( data . comments ) ;
110+ configFunctions . onCommentsRendered &&
111+ configFunctions . onCommentsRendered ( data . comments ) ;
94112 } else if ( data . type === 'open-profile' ) {
95113 if ( configFunctions . onOpenProfile ) {
96114 if ( configFunctions . onOpenProfile ( data . userId ) && webview ) {
97115 const js = `
98116 (function () {
99117 window.dispatchEvent(new MessageEvent('message', {
100118 data: '${ JSON . stringify ( {
101- type : 'profile-loaded' ,
102- // @ts -ignore
103- instanceId : config . instanceId
104- } ) } '
119+ type : 'profile-loaded' ,
120+ // @ts -ignore
121+ instanceId : config . instanceId ,
122+ } ) } '
105123 }));
106124 })();
107125 ` ;
@@ -111,16 +129,19 @@ export function FastCommentsEmbedCore(props: FastCommentsWidgetParameters, widge
111129 }
112130 } catch ( err ) {
113131 // @ts -ignore
114- if ( props . config . apiHost ) { // only log errors during testing
132+ if ( props . config . apiHost ) {
133+ // only log errors during testing
115134 console . error ( e , err ) ;
116135 }
117136 }
118137 }
119138
120139 useEffect ( ( ) => {
121- let config = { ...props . config } ;
140+ let config = { ...props . config } ;
122141 if ( config . urlId === null || config . urlId === undefined ) {
123- throw new Error ( 'FastComments Error: A "urlId" is required! This should be a "urlId" property on the config object, that points to a bucket where comments will be stored and render from.' ) ;
142+ throw new Error (
143+ 'FastComments Error: A "urlId" is required! This should be a "urlId" property on the config object, that points to a bucket where comments will be stored and render from.'
144+ ) ;
124145 }
125146
126147 if ( typeof config . urlId === 'number' ) {
@@ -139,15 +160,20 @@ export function FastCommentsEmbedCore(props: FastCommentsWidgetParameters, widge
139160 // @ts -ignore
140161 config . instanceId = Math . random ( ) + '.' + Date . now ( ) ;
141162 // @ts -ignore
142- const host = config . apiHost ? config . apiHost : config . region === 'eu' ? 'https://eu.fastcomments.com' : 'https://fastcomments.com' ;
163+ const host = config . apiHost
164+ ? config . apiHost
165+ : config . region === 'eu'
166+ ? 'https://eu.fastcomments.com'
167+ : 'https://fastcomments.com' ;
143168
144169 for ( const key in config ) {
145170 // @ts -ignore
146171 const configValue = config [ key ] ;
147172 if ( configValue === undefined ) {
148173 // @ts -ignore
149174 delete config [ key ] ;
150- } else if ( typeof configValue === 'number' ) { // example: startingPage
175+ } else if ( typeof configValue === 'number' ) {
176+ // example: startingPage
151177 // @ts -ignore
152178 config [ key ] = configValue ;
153179 } else if ( typeof configValue !== 'object' ) {
@@ -158,23 +184,30 @@ export function FastCommentsEmbedCore(props: FastCommentsWidgetParameters, widge
158184 config [ key ] = configValue ;
159185 }
160186 }
161- setURI ( host + '/embed?config=' + encodeURIComponent ( JSON . stringify ( config ) ) + '&wId=' + widgetId ) ;
162- } , [ props . config ] ) ;
187+ setURI (
188+ host +
189+ '/embed?config=' +
190+ encodeURIComponent ( JSON . stringify ( config ) ) +
191+ '&wId=' +
192+ widgetId
193+ ) ;
194+ } , [ props . config , widgetId ] ) ;
163195
164196 return (
165197 < WebView
166- ref = { ref => {
198+ ref = { ( ref ) => {
167199 webview = ref ;
168200 } }
169- style = { { height, backgroundColor : props . backgroundColor } }
201+ style = { { height, backgroundColor : props . backgroundColor } }
170202 startInLoadingState = { true }
171- renderLoading = { ( ) => < ActivityIndicator size = "small" /> }
203+ renderLoading = { ( ) => < ActivityIndicator size = "small" /> }
172204 scalesPageToFit = { true }
173- source = { { uri} }
205+ source = { { uri } }
174206 domStorageEnabled = { true }
175207 javaScriptEnabled = { true }
176- onMessage = { event => eventHandler ( event ) }
208+ onMessage = { ( event ) => eventHandler ( event ) }
177209 onError = { props . onError }
178- onLoad = { props . onLoad } />
210+ onLoad = { props . onLoad }
211+ />
179212 ) ;
180213}
0 commit comments