import { connect } from 'react-redux';
import { PAGE_SIZE } from 'constants/claim';
import {
  doResolveUri,
  makeSelectClaimForUri,
  makeSelectIsUriResolving,
  makeSelectTotalPagesForChannel,
  makeSelectTitleForUri,
  normalizeURI,
  makeSelectClaimIsMine,
  makeSelectClaimIsPending,
} from 'lbry-redux';
import { makeSelectChannelInSubscriptions } from 'redux/selectors/subscriptions';
import { selectBlackListedOutpoints } from 'lbryinc';
import ShowPage from './view';

const select = (state, props) => {
  const { pathname, hash } = props.location;
  const urlPath = pathname + hash;
  // Remove the leading "/" added by the browser
  const path = urlPath.slice(1).replace(/:/g, '#');

  let uri;
  try {
    uri = normalizeURI(path);
  } catch (e) {
    const match = path.match(/[#/:]/);

    if (path === '$/') {
      props.history.replace(`/`);
    } else if (!path.startsWith('$/') && match && match.index) {
      uri = `lbry://${path.slice(0, match.index)}`;
      props.history.replace(`/${path.slice(0, match.index)}`);
    }
  }

  return {
    claim: makeSelectClaimForUri(uri)(state),
    isResolvingUri: makeSelectIsUriResolving(uri)(state),
    blackListedOutpoints: selectBlackListedOutpoints(state),
    totalPages: makeSelectTotalPagesForChannel(uri, PAGE_SIZE)(state),
    isSubscribed: makeSelectChannelInSubscriptions(uri)(state),
    uri,
    title: makeSelectTitleForUri(uri)(state),
    claimIsMine: makeSelectClaimIsMine(uri)(state),
    claimIsPending: makeSelectClaimIsPending(uri)(state),
  };
};

const perform = dispatch => ({
  resolveUri: uri => dispatch(doResolveUri(uri)),
});

export default connect(select, perform)(ShowPage);

@@ -1,16 +1,49 @@
+import %7B DOMAIN %7D from 'config';%0A
 import %7B connect
@@ -57,32 +57,32 @@
  'react-redux';%0A
-
 import %7B PAGE_SI
@@ -564,16 +564,24 @@
 me, hash
+, search
  %7D = pro
@@ -679,20 +679,18 @@
 owser%0A  
-cons
+le
 t path =
@@ -728,16 +728,647 @@
  '#');%0A%0A
+  // Google cache url%0A  // ex: webcache.googleusercontent.com/search?q=cache:MLwN3a8fCbYJ:https://lbry.tv/%2540Bombards_Body_Language:f+&cd=12&hl=en&ct=clnk&gl=us%0A  // Extract the lbry url and use that instead%0A  // Without this it will try to render lbry://search%0A  if (search && search.startsWith('?q=cache:')) %7B%0A    const googleCacheRegex = new RegExp(%60(https://$%7BDOMAIN%7D/)(%5B%5E+%5D*)%60);%0A    const %5Bx, y, googleCachedUrl%5D = search.match(googleCacheRegex); // eslint-disable-line%0A    if (googleCachedUrl) %7B%0A      const actualUrl = decodeURIComponent(googleCachedUrl);%0A      if (actualUrl) %7B%0A        path = actualUrl;%0A      %7D%0A    %7D%0A  %7D%0A%0A
   let ur

handle google webcache urls