mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-11-30 06:12:37 +03:00
Added post parser util
This commit is contained in:
parent
5c5c7dcf8a
commit
c9edaa01e3
@ -8,10 +8,11 @@ import { search } from '../../../providers/esteem/esteem';
|
|||||||
import { lookupAccounts, getTrendingTags } from '../../../providers/steem/dsteem';
|
import { lookupAccounts, getTrendingTags } from '../../../providers/steem/dsteem';
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import { default as ROUTES } from '../../../constants/routeNames';
|
import ROUTES from '../../../constants/routeNames';
|
||||||
|
|
||||||
// Utilities
|
// Utilities
|
||||||
import { getResizedAvatar } from '../../../utils/image';
|
import { getResizedAvatar } from '../../../utils/image';
|
||||||
|
import postUrlParser from '../../../utils/postUrlParser';
|
||||||
|
|
||||||
// Component
|
// Component
|
||||||
import SearchModalView from '../view/searchModalView';
|
import SearchModalView from '../view/searchModalView';
|
||||||
@ -40,13 +41,13 @@ class SearchModalContainer extends PureComponent {
|
|||||||
};
|
};
|
||||||
|
|
||||||
_handleOnChangeSearchInput = text => {
|
_handleOnChangeSearchInput = text => {
|
||||||
const { isConnected } = this.props;
|
const { isConnected, navigation, handleOnClose } = this.props;
|
||||||
|
|
||||||
if (text && text.length < 2) return;
|
if (text && text.length < 2) return;
|
||||||
if (this.timer) {
|
if (this.timer) {
|
||||||
clearTimeout(this.timer);
|
clearTimeout(this.timer);
|
||||||
}
|
}
|
||||||
if (!isConnected) return;
|
if (!isConnected) return;
|
||||||
this.timer = setTimeout(() => {
|
|
||||||
if (text && text !== '@' && text !== '#') {
|
if (text && text !== '@' && text !== '#') {
|
||||||
if (text[0] === '@') {
|
if (text[0] === '@') {
|
||||||
lookupAccounts(text.substr(1)).then(res => {
|
lookupAccounts(text.substr(1)).then(res => {
|
||||||
@ -66,6 +67,22 @@ class SearchModalContainer extends PureComponent {
|
|||||||
|
|
||||||
this.setState({ searchResults: { type: 'tag', data: tags } });
|
this.setState({ searchResults: { type: 'tag', data: tags } });
|
||||||
});
|
});
|
||||||
|
} else if (text.includes('https')) {
|
||||||
|
console.log('test :');
|
||||||
|
console.log('postUrlParser(text) :', postUrlParser(text));
|
||||||
|
const { author, permlink } = postUrlParser(text);
|
||||||
|
console.log('author, permlink :', author, permlink);
|
||||||
|
if (author && permlink) {
|
||||||
|
handleOnClose();
|
||||||
|
navigation.navigate({
|
||||||
|
routeName: ROUTES.SCREENS.POST,
|
||||||
|
params: {
|
||||||
|
author,
|
||||||
|
permlink,
|
||||||
|
},
|
||||||
|
key: permlink,
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
search({ q: text }).then(res => {
|
search({ q: text }).then(res => {
|
||||||
res.results = res.results
|
res.results = res.results
|
||||||
@ -79,7 +96,6 @@ class SearchModalContainer extends PureComponent {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 500);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
_handleOnPressListItem = (type, item) => {
|
_handleOnPressListItem = (type, item) => {
|
||||||
|
60
src/utils/postUrlParser.js
Normal file
60
src/utils/postUrlParser.js
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
export default url => {
|
||||||
|
const parseCatAuthorPermlink = u => {
|
||||||
|
const r = /^https?:\/\/(.*)\/(.*)\/(@[\w.\d-]+)\/(.*)/i;
|
||||||
|
const match = u.match(r);
|
||||||
|
if (match && match.length === 5) {
|
||||||
|
return {
|
||||||
|
author: match[3].replace('@', ''),
|
||||||
|
permlink: match[4],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const parseAuthorPermlink = u => {
|
||||||
|
const r = /^https?:\/\/(.*)\/(@[\w.\d-]+)\/(.*)/i;
|
||||||
|
const match = u.match(r);
|
||||||
|
if (match && match.length === 4) {
|
||||||
|
return {
|
||||||
|
author: match[2].replace('@', ''),
|
||||||
|
permlink: match[3],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (
|
||||||
|
['https://esteem.app', 'https://steemit.com', 'https://steempeak.com'].some(x =>
|
||||||
|
url.startsWith(x),
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return parseCatAuthorPermlink(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (['https://busy.org', 'https://steemhunt.com'].some(x => url.startsWith(x))) {
|
||||||
|
return parseAuthorPermlink(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
// For non urls like @good-karma/esteem-london-presentation-e3105ba6637ed
|
||||||
|
let match = url.match(/^[/]?(@[\w.\d-]+)\/(.*)/);
|
||||||
|
if (match && match.length === 3) {
|
||||||
|
return {
|
||||||
|
author: match[1].replace('@', ''),
|
||||||
|
permlink: match[2],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// For non urls with category like esteem/@good-karma/esteem-london-presentation-e3105ba6637ed
|
||||||
|
match = url.match(/^[/]?([\w.\d-]+)\/(@[\w.\d-]+)\/(.*)/);
|
||||||
|
if (match && match.length === 4) {
|
||||||
|
return {
|
||||||
|
category: match[1],
|
||||||
|
author: match[2].replace('@', ''),
|
||||||
|
permlink: match[3],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user