Added redirect function to search bar

This commit is contained in:
unknown 2021-10-06 12:01:07 +02:00
parent a162450568
commit da928f20a2
6 changed files with 35 additions and 15 deletions

View file

@ -1,6 +1,8 @@
### v1.7.0 (TBA) ### v1.7.0 (TBA)
- Search bar will now redirect if valid URL or IP is provided ([#67](https://github.com/pawelmalak/flame/issues/67))
- Fixed bug related to creating new apps/bookmarks with custom icon ([#83](https://github.com/pawelmalak/flame/issues/83)) - Fixed bug related to creating new apps/bookmarks with custom icon ([#83](https://github.com/pawelmalak/flame/issues/83))
- URL can now be assigned to notifications. Clicking on "New version is available" popup will now redirect to changelog ([#86](https://github.com/pawelmalak/flame/issues/86)) - URL can now be assigned to notifications. Clicking on "New version is available" popup will now redirect to changelog ([#86](https://github.com/pawelmalak/flame/issues/86))
- Added static fonts ([#94](https://github.com/pawelmalak/flame/issues/94))
### v1.6.7 (2021-10-04) ### v1.6.7 (2021-10-04)
- Add multiple labels to Docker Compose ([#90](https://github.com/pawelmalak/flame/issues/90)) - Add multiple labels to Docker Compose ([#90](https://github.com/pawelmalak/flame/issues/90))

View file

@ -11,7 +11,7 @@ import { NewNotification } from '../../interfaces';
import classes from './SearchBar.module.css'; import classes from './SearchBar.module.css';
// Utils // Utils
import { searchParser } from '../../utility'; import { searchParser, urlParser, redirectUrl } from '../../utility';
interface ComponentProps { interface ComponentProps {
createNotification: (notification: NewNotification) => void; createNotification: (notification: NewNotification) => void;
@ -28,28 +28,28 @@ const SearchBar = (props: ComponentProps): JSX.Element => {
}, []); }, []);
const searchHandler = (e: KeyboardEvent<HTMLInputElement>) => { const searchHandler = (e: KeyboardEvent<HTMLInputElement>) => {
const searchResult = searchParser(inputRef.current.value); const { isLocal, search, query, isURL, sameTab } = searchParser(
inputRef.current.value
);
if (searchResult.isLocal) { if (isLocal) {
setLocalSearch(searchResult.search); setLocalSearch(search);
} }
if (e.code === 'Enter') { if (e.code === 'Enter') {
if (!searchResult.query.prefix) { if (!query.prefix) {
createNotification({ createNotification({
title: 'Error', title: 'Error',
message: 'Prefix not found', message: 'Prefix not found',
}); });
} else if (searchResult.isLocal) { } else if (isURL) {
setLocalSearch(searchResult.search); const url = urlParser(inputRef.current.value)[1];
redirectUrl(url, sameTab);
} else if (isLocal) {
setLocalSearch(search);
} else { } else {
if (searchResult.sameTab) { const url = `${query.template}${search}`;
document.location.replace( redirectUrl(url, sameTab);
`${searchResult.query.template}${searchResult.search}`
);
} else {
window.open(`${searchResult.query.template}${searchResult.search}`);
}
} }
} }
}; };

View file

@ -2,6 +2,7 @@ import { Query } from './Query';
export interface SearchResult { export interface SearchResult {
isLocal: boolean; isLocal: boolean;
isURL: boolean;
sameTab: boolean; sameTab: boolean;
search: string; search: string;
query: Query; query: Query;

View file

@ -3,4 +3,5 @@ export * from './urlParser';
export * from './searchConfig'; export * from './searchConfig';
export * from './checkVersion'; export * from './checkVersion';
export * from './sortData'; export * from './sortData';
export * from './searchParser'; export * from './searchParser';
export * from './redirectUrl';

View file

@ -0,0 +1,7 @@
export const redirectUrl = (url: string, sameTab: boolean) => {
if (sameTab) {
document.location.replace(url);
} else {
window.open(url);
}
};

View file

@ -6,6 +6,7 @@ import { searchConfig } from '.';
export const searchParser = (searchQuery: string): SearchResult => { export const searchParser = (searchQuery: string): SearchResult => {
const result: SearchResult = { const result: SearchResult = {
isLocal: false, isLocal: false,
isURL: false,
sameTab: false, sameTab: false,
search: '', search: '',
query: { query: {
@ -15,6 +16,13 @@ export const searchParser = (searchQuery: string): SearchResult => {
}, },
}; };
// Check if url or ip was passed
const urlRegex =
/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?|^((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
result.isURL = urlRegex.test(searchQuery);
// Match prefix and query
const splitQuery = searchQuery.match(/^\/([a-z]+)[ ](.+)$/i); const splitQuery = searchQuery.match(/^\/([a-z]+)[ ](.+)$/i);
const prefix = splitQuery const prefix = splitQuery
@ -27,6 +35,7 @@ export const searchParser = (searchQuery: string): SearchResult => {
const query = queries.find((q: Query) => q.prefix === prefix); const query = queries.find((q: Query) => q.prefix === prefix);
// If search provider was found
if (query) { if (query) {
result.query = query; result.query = query;
result.search = search; result.search = search;