Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: MMPD-1528 - View recents and favorites in browser URL modal #12938

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 54 additions & 32 deletions app/components/UI/UrlAutocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import { fontStyles } from '../../../styles/common';
import { getHost } from '../../../util/browser';
import { ThemeContext, mockTheme } from '../../../util/theme';
import { strings } from '../../../../locales/i18n';

const MAX_RECENTS = 5;
const ORDERED_CATEGORIES = ['recents', 'favorites', 'sites'];

const createStyles = (colors) =>
StyleSheet.create({
Expand All @@ -36,6 +40,12 @@
color: colors.text.default,
...fontStyles.normal,
},
category: {
fontSize: 16,
color: colors.text.default,
...fontStyles.bold,
margin: 10,
},
url: {
fontSize: 12,
color: colors.text.alternative,
Expand Down Expand Up @@ -83,27 +93,22 @@
* An array of visited urls and names
*/
browserHistory: PropTypes.array,
/**
* An array of bookmarks
*/
bookmarks: PropTypes.array,
};

state = {
results: [],
};

componentDidMount() {
const allUrls = [...this.props.browserHistory, ...dappUrlList];
const singleUrlList = [];
const singleUrls = [];
for (let i = 0; i < allUrls.length; i++) {
const el = allUrls[i];
if (!singleUrlList.includes(el.url)) {
singleUrlList.push(el.url);
singleUrls.push(el);
}
}
const allUrls = dappUrlList;

this.fuse = new Fuse(singleUrls, {
this.fuse = new Fuse(allUrls, {
shouldSort: true,
threshold: 0.45,
threshold: 0.4,
location: 0,
distance: 100,
maxPatternLength: 32,
Expand Down Expand Up @@ -131,7 +136,7 @@
} else {
this.updateResults([]);
}
}, 500);
}, 50);
}
}

Expand Down Expand Up @@ -180,18 +185,22 @@
const colors = this.context.colors || mockTheme.colors;
const styles = createStyles(colors);

if (!this.props.input || this.props.input.length < 2)
return (
<View style={styles.wrapper}>
<TouchableWithoutFeedback
style={styles.bg}
onPress={this.props.onDismiss}
>
<View style={styles.bg} />
</TouchableWithoutFeedback>
</View>
);
if (this.state.results.length === 0) {
let results = [];
if (!this.props.input || this.props.input.length === 0) {
results = [
...this.props.browserHistory.slice(-MAX_RECENTS).reverse().map(i => ({...i, type: 'recents'})),
...this.props.bookmarks.map(i => ({...i, type: 'favorites'})),
]

Check warning on line 193 in app/components/UI/UrlAutocomplete/index.js

View workflow job for this annotation

GitHub Actions / scripts (lint)

Missing semicolon
} else {
results = this.state.results.map(i => ({...i, type: 'sites'}));
}

const resultsByCategory = results.reduce((acc, i) => {
acc[i.type] = [...acc[i.type] || [], i];
return acc;
}, {});

if (results.length === 0) {
return (
<View style={styles.wrapper}>
<TouchableOpacity
Expand All @@ -215,15 +224,27 @@
</View>
);
}

const categoriesWithResults = ORDERED_CATEGORIES.filter(category => resultsByCategory[category]?.length > 0);

return (
<View style={styles.wrapper}>
{this.state.results.slice(0, 3).map((r) => {
const { url, name } = r;
const onPress = () => {
this.props.onSubmit(url);
};
return this.renderUrlOption(url, name, onPress);
})}
{
categoriesWithResults.map(category => {

Check failure on line 233 in app/components/UI/UrlAutocomplete/index.js

View workflow job for this annotation

GitHub Actions / scripts (lint)

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return (
<View key={category}>
<Text style={styles.category}>{strings(`autocomplete.${category}`)}</Text>
{resultsByCategory[category].map((r) => {
const { url, name } = r;
const onPress = () => {
this.props.onSubmit(url);
};
return this.renderUrlOption(url, name, onPress);
})}
</View>
)

Check warning on line 245 in app/components/UI/UrlAutocomplete/index.js

View workflow job for this annotation

GitHub Actions / scripts (lint)

Missing semicolon
})
}
<TouchableWithoutFeedback
style={styles.bg}
onPress={this.props.onDismiss}
Expand All @@ -237,6 +258,7 @@

const mapStateToProps = (state) => ({
browserHistory: state.browser.history,
bookmarks: state.bookmarks,
});

UrlAutocomplete.contextType = ThemeContext;
Expand Down
9 changes: 5 additions & 4 deletions app/components/Views/BrowserUrlModal/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const createStyles = (colors: any) =>
flexDirection: 'row',
paddingTop: Device.isAndroid() ? 10 : Device.isIphoneX() ? 50 : 27,
paddingHorizontal: 10,
height: Device.isAndroid() ? 59 : Device.isIphoneX() ? 87 : 65,
height: Device.isAndroid() ? 69 : Device.isIphoneX() ? 97 : 75,
backgroundColor: colors.background.default,
},
clearButton: { paddingHorizontal: 12, justifyContent: 'center' },
Expand All @@ -46,9 +46,10 @@ export const createStyles = (colors: any) =>
} as any,
searchWrapper: {
flexDirection: 'row',
borderRadius: 30,
backgroundColor: colors.background.alternative,
height: Device.isAndroid() ? 40 : 30,
borderRadius: 8,
borderWidth: 1,
borderColor: colors.border.default,
height: Device.isAndroid() ? 50 : 40,
flex: 1,
},
});
5 changes: 4 additions & 1 deletion locales/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
"connector": "at"
},
"autocomplete": {
"placeholder": "Search or Type URL"
"placeholder": "Search by site or address",
"recents": "Recents",
"favorites": "Favorites",
"sites": "Sites"
},
"navigation": {
"back": "Back",
Expand Down
Loading