feat(phone/browser): Added a browser app

This utilizes an iframe, meaning sites that are blocking cross-origin iframes will ultimately fail.

With this feature, a config option is added to the nui config allowing for users to specify a home page URL to default to.
This commit is contained in:
Taso committed 2021-07-28 21:21:11 -04:00
1 parent b433c2bda0
commit a06ef07902
11 files changed
+248 -5

No files matched your search

@@ -0,0 +1,106 @@
import { AppWrapper } from '../../../ui/components';
import React, { Reducer, useReducer, useRef } from 'react';
import { AppContent } from '../../../ui/components/AppContent';
import { Box, makeStyles } from '@material-ui/core';
import { BrowserURLBar } from './BrowserURLBar';
import { promiseTimeout } from '../../../utils/promiseTimeout';
import { usePhoneConfig } from '../../../config/hooks/usePhoneConfig';
const useStyles = makeStyles(() => ({
iframe: {
height: '100%',
width: '100%',
zoom: 0.5,
border: 'none',
},
root: {
height: '100%',
display: 'flex',
flexDirection: 'column',
},
}));
interface BrowserState {
browserUrl: string;
browserHistory: string[];
}
interface ReducerAction {
payload: any;
type: ReducerActionsType;
}
enum ReducerActionsType {
SET_URL,
ADD_HISTORY,
RELOAD,
}
const browserReducer: Reducer<BrowserState, ReducerAction> = (state, action) => {
switch (action.type) {
case ReducerActionsType.ADD_HISTORY:
if (action.payload === 'about:blank') return state;
return { ...state, browserHistory: state.browserHistory.concat(action.payload) };
case ReducerActionsType.SET_URL:
return { ...state, browserUrl: action.payload };
default:
throw new Error('Invalid reducer action type');
}
};
export const BrowserApp: React.FC = () => {
const classes = useStyles();
const [{ appSettings }] = usePhoneConfig();
const [browserState, dispatch] = useReducer(browserReducer, {
browserUrl: appSettings.browserHomePage,
browserHistory: [appSettings.browserHomePage],
});
const { browserHistory, browserUrl } = browserState;
const iframeRef = useRef<HTMLIFrameElement | null>(null);
const _setBrowserUrl = (newUrl: string) => {
const formattedUrl = newUrl.match(/^(http|https):\/\//) ? newUrl : 'https://' + newUrl;
dispatch({ payload: browserUrl, type: ReducerActionsType.ADD_HISTORY });
dispatch({ payload: formattedUrl, type: ReducerActionsType.SET_URL });
};
const handleGoBack = () => {
if (browserHistory.length <= 1) return;
// Get last page from history
const lastPage = browserHistory[browserHistory.length - 1];
// Bail if last page is same as current
if (lastPage === browserUrl) return;
_setBrowserUrl(lastPage);
};
const reloadPage = async () => {
const strCopy = browserUrl.slice();
dispatch({ payload: 'about:blank', type: ReducerActionsType.SET_URL });
await promiseTimeout(100);
dispatch({ payload: strCopy, type: ReducerActionsType.SET_URL });
};
return (
<AppWrapper id="browser">
<AppContent className={classes.root}>
<BrowserURLBar
browserUrl={browserUrl}
browserHasHistory={browserHistory.length > 1}
setBrowser={_setBrowserUrl}
goBack={handleGoBack}
reloadPage={reloadPage}
/>
<Box flexGrow={1}>
<iframe
src={browserUrl}
className={classes.iframe}
title="npwd-browser"
ref={iframeRef}
/>
</Box>
</AppContent>
</AppWrapper>
);
};
@@ -0,0 +1,91 @@
import React, { FocusEventHandler, useRef, useState } from 'react';
import { makeStyles, Theme, Box, InputBase, IconButton } from '@material-ui/core';
import { Cached, KeyboardArrowLeft } from '@material-ui/icons';
const useStyles = makeStyles((theme: Theme) => ({
urlInput: {
borderRadius: 18,
borderWidth: 1,
borderStyle: 'solid',
paddingRight: 10,
paddingLeft: 10,
marginLeft: 5,
borderColor: '#787878FF',
color: '#909090',
transition: 'color 0.1s ease',
},
urlInputFocused: {
color: '#fff',
},
root: {},
}));
interface BrowserControlsProps {
setBrowser: (url: string) => void;
browserUrl: string;
reloadPage: () => void;
browserHasHistory: boolean;
goBack: () => void;
}
export const BrowserURLBar: React.FC<BrowserControlsProps> = ({
goBack,
reloadPage,
browserHasHistory,
browserUrl,
setBrowser,
}) => {
const classes = useStyles();
const inputRef = useRef<HTMLInputElement>(null);
const [inputState, setInputState] = useState('');
const [isFocused, setFocus] = useState(false);
const handleFocus: FocusEventHandler = (e) => {
setInputState(browserUrl);
setFocus(true);
};
const handleFocusOut: FocusEventHandler = (e) => {
setFocus(false);
setInputState(browserUrl);
};
const handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
setInputState(e.target.value);
};
const onEnter: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
if ((e as any).code !== 'Enter') return;
setBrowser(inputState);
e.currentTarget.blur();
};
const styledUrl = browserUrl.replace(/(^\w+:|^)\/\//, '');
const transformedUrl = isFocused ? inputState : styledUrl;
return (
<Box display="flex" py={1} className={classes.root} px={1}>
<IconButton size="small" onClick={() => reloadPage()}>
<Cached />
</IconButton>
{browserHasHistory && (
<IconButton size="small" onClick={() => goBack()}>
<KeyboardArrowLeft />
</IconButton>
)}
<InputBase
className={classes.urlInput}
ref={inputRef}
fullWidth
onChange={handleChange}
onKeyDown={onEnter}
value={transformedUrl}
classes={{ focused: classes.urlInputFocused }}
onFocus={handleFocus}
spellCheck={false}
onBlur={handleFocusOut}
/>
</Box>
);
};
+3
View File
@@ -194,6 +194,9 @@
"TWITTER_notiSoundVol": 50,
"TWITTER_notifyNewFeedTweet": true
},
"appSettings": {
"browserHomePage": "https://docs.projecterror.dev"
},
"iconSet": [
{
"label": "NPWD Icons",
+3
View File
@@ -18,6 +18,9 @@
"ERROR": "Error occurred while completing this call"
}
},
"BROWSER": {
"NAME": "Browser"
},
"SETTINGS": {
"CATEGORY": {
"APPEARANCE": "Appearance",
+11
View File
@@ -34,6 +34,7 @@ import {
import { MATCH_APP_PRIMARY_COLOR, MATCH_APP_TEXT_COLOR } from '../../../apps/match/match.theme';
import { SvgIconProps } from '@material-ui/core';
import { INotificationIcon } from '../../notifications/providers/NotificationsProvider';
import { BrowserApp } from '../../../apps/browser/components/BrowserApp';
export interface IAppConfig {
id: string;
@@ -61,6 +62,16 @@ export const APPS: IAppConfig[] = [
path: '/phone',
Route: () => <AppRoute id="DIALER" path="/phone" component={DialerApp} emitOnOpen={false} />,
},
{
id: 'BROWSER',
nameLocale: 'BROWSER.NAME',
backgroundColor: blue['300'],
path: '/browser',
color: common.white,
Route: () => (
<AppRoute id="BROWSER" path="/browser" component={BrowserApp} emitOnOpen={false} />
),
},
{
id: 'MESSAGES',
nameLocale: 'APPS_MESSAGES',
+8 -4
View File
@@ -18,11 +18,15 @@ export const useApps = () => {
const apps: IApp[] = useMemo(() => {
return APPS.map((app) => {
const SvgIcon = React.lazy<SvgIconComponent>(
() => import(`${__dirname}/../icons/${curIconSet.name}/svg/${app.id}`),
const SvgIcon = React.lazy<SvgIconComponent>(() =>
import(`${__dirname}/../icons/${curIconSet.name}/svg/${app.id}`).catch(
() => 'Was not able to find a dynamic import for icon from this icon set',
),
);
const AppIcon = React.lazy<SvgIconComponent>(
() => import(`${__dirname}/../icons/${curIconSet.name}/app/${app.id}`),
const AppIcon = React.lazy<SvgIconComponent>(() =>
import(`${__dirname}/../icons/${curIconSet.name}/app/${app.id}`).catch(
() => 'Was not able to find a dynamic import for icon from this icon set',
),
);
const NotificationIcon = createLazyAppIcon(SvgIcon);
@@ -0,0 +1,5 @@
import { Language } from '@material-ui/icons';
const BrowserIcon = () => <Language fontSize="large" />;
export default BrowserIcon;
@@ -0,0 +1,5 @@
import { Language } from '@material-ui/icons';
const BrowserIcon = () => <Language fontSize="small" />;
export default BrowserIcon;
@@ -0,0 +1,5 @@
import { Language } from '@material-ui/icons';
const BrowserIcon = () => <Language fontSize="large" />;
export default BrowserIcon;
@@ -0,0 +1,5 @@
import { Language } from '@material-ui/icons';
const BrowserIcon = () => <Language fontSize="small" />;
export default BrowserIcon;
+6 -1
View File
@@ -38,7 +38,12 @@ export const AppContent = ({
>
<Backdrop className={classes.backdrop} open={backdrop || false} onClick={onClickBackdrop} />
<Box flexGrow={1} className={classes.box} {...props}>
<Paper square elevation={0} className={classes.paper} style={paperStyle}>
<Paper
square
elevation={0}
className={`${classes.paper} ${props.className}`}
style={paperStyle}
>
{children}
</Paper>
</Box>