mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-20 20:01:56 +03:00
Merge pull request #1223 from esteemapp/feature/link-search
[Feature] Open post and profile links on search bar
This commit is contained in:
commit
3cc7c3fe9a
@ -5,13 +5,14 @@ import get from 'lodash/get';
|
||||
|
||||
// Services and Actions
|
||||
import { search } from '../../../providers/esteem/esteem';
|
||||
import { lookupAccounts, getTrendingTags } from '../../../providers/steem/dsteem';
|
||||
import { lookupAccounts, getTrendingTags, getPurePost } from '../../../providers/steem/dsteem';
|
||||
|
||||
// Constants
|
||||
import { default as ROUTES } from '../../../constants/routeNames';
|
||||
import ROUTES from '../../../constants/routeNames';
|
||||
|
||||
// Utilities
|
||||
import { getResizedAvatar } from '../../../utils/image';
|
||||
import postUrlParser from '../../../utils/postUrlParser';
|
||||
|
||||
// Component
|
||||
import SearchModalView from '../view/searchModalView';
|
||||
@ -41,12 +42,12 @@ class SearchModalContainer extends PureComponent {
|
||||
|
||||
_handleOnChangeSearchInput = text => {
|
||||
const { isConnected } = this.props;
|
||||
|
||||
if (text && text.length < 2) return;
|
||||
if (this.timer) {
|
||||
clearTimeout(this.timer);
|
||||
}
|
||||
if (!isConnected) return;
|
||||
this.timer = setTimeout(() => {
|
||||
if (text && text !== '@' && text !== '#') {
|
||||
if (text[0] === '@') {
|
||||
lookupAccounts(text.substr(1)).then(res => {
|
||||
@ -66,6 +67,42 @@ class SearchModalContainer extends PureComponent {
|
||||
|
||||
this.setState({ searchResults: { type: 'tag', data: tags } });
|
||||
});
|
||||
} else if (text.includes('https')) {
|
||||
const postUrl = postUrlParser(text.replace(/\s/g, ''));
|
||||
|
||||
if (postUrl) {
|
||||
const { author, permlink } = postUrl;
|
||||
|
||||
if (author) {
|
||||
if (permlink) {
|
||||
getPurePost(author, permlink).then(post => {
|
||||
if (post.id !== 0) {
|
||||
const result = {};
|
||||
const metadata = JSON.parse(get(post, 'json_metadata', ''));
|
||||
if (get(metadata, 'image', false) && metadata.image.length > 0) {
|
||||
result.image = metadata.image[0];
|
||||
} else {
|
||||
result.image = getResizedAvatar(author);
|
||||
}
|
||||
result.author = author;
|
||||
result.text = post.title;
|
||||
result.permlink = permlink;
|
||||
this.setState({ searchResults: { type: 'content', data: [result] } });
|
||||
} else {
|
||||
this.setState({ searchResults: { type: 'content', data: [] } });
|
||||
}
|
||||
});
|
||||
} else {
|
||||
lookupAccounts(author).then(res => {
|
||||
const users = res.map(item => ({
|
||||
image: getResizedAvatar(item),
|
||||
text: item,
|
||||
...item,
|
||||
}));
|
||||
this.setState({ searchResults: { type: 'user', data: users } });
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
search({ q: text }).then(res => {
|
||||
res.results = res.results
|
||||
@ -79,7 +116,7 @@ class SearchModalContainer extends PureComponent {
|
||||
});
|
||||
}
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
};
|
||||
|
||||
_handleOnPressListItem = (type, item) => {
|
||||
|
75
src/utils/postUrlParser.js
Normal file
75
src/utils/postUrlParser.js
Normal file
@ -0,0 +1,75 @@
|
||||
export default url => {
|
||||
const parseCatAuthorPermlink = u => {
|
||||
const postRegex = /^https?:\/\/(.*)\/(.*)\/(@[\w.\d-]+)\/(.*)/i;
|
||||
const postMatch = u.match(postRegex);
|
||||
if (postMatch && postMatch.length === 5) {
|
||||
return {
|
||||
author: postMatch[3].replace('@', ''),
|
||||
permlink: postMatch[4],
|
||||
};
|
||||
}
|
||||
const authorRegex = /^https?:\/\/(.*)\/(.*)\/(@[\w.\d-]+)/i;
|
||||
const authorMatch = u.match(authorRegex);
|
||||
if (authorMatch && authorMatch.length === 4) {
|
||||
return {
|
||||
author: authorMatch[3].replace('@', ''),
|
||||
permlink: null,
|
||||
};
|
||||
}
|
||||
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],
|
||||
};
|
||||
}
|
||||
const authorRegex = /^https?:\/\/(.*)\/(@[\w.\d-]+)/i;
|
||||
const authorMatch = u.match(authorRegex);
|
||||
if (authorMatch && authorMatch.length === 3) {
|
||||
return {
|
||||
author: authorMatch[2].replace('@', ''),
|
||||
permlink: null,
|
||||
};
|
||||
}
|
||||
|
||||
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