Skip to content

Commit

Permalink
Send mobile configuration from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
RomaricMourgues committed Mar 7, 2022
1 parent 28cd040 commit 6d2057f
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
5 changes: 5 additions & 0 deletions twake/backend/node/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"general": {
"help_url": false,
"pricing_plan_url": "",
"mobile": {
"mobile_redirect": "mobile.twake.app",
"mobile_appstore": "https://apps.apple.com/fr/app/twake/id1588764852?l=en",
"mobile_googleplay": "https://play.google.com/store/apps/details?id=com.twake.twake&gl=FR"
},
"accounts": {
"_type": "console",
"type": "internal",
Expand Down
5 changes: 5 additions & 0 deletions twake/backend/node/src/services/general/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export type ServerConfiguration = {
configuration: {
help_url: string | null;
pricing_plan_url: string | null;
mobile: {
mobile_redirect: string;
mobile_appstore: string;
mobile_googleplay: string;
};
accounts: {
type: "console" | "internal";
console: null | {
Expand Down
2 changes: 1 addition & 1 deletion twake/backend/node/src/services/general/web/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const routes: FastifyPluginCallback<{ configuration: ServerConfiguration["config
status: "ready",
version: version,
configuration: {
..._.pick(options.configuration, "help_url", "pricing_plan_url"),
..._.pick(options.configuration, "help_url", "pricing_plan_url", "mobile"),
accounts: {
type: accounts.type,
console: _.pick(
Expand Down
7 changes: 2 additions & 5 deletions twake/frontend/src/app/environment/environment.ts.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ export default {
env_dev: false,
front_root_url: 'https://web.twake.app',
api_root_url: 'https://api.twake.app',
websocket_url: 'wss://api.twake.app',
mobile_redirect: 'mobile.twake.app',
mobile_appstore: 'https://apps.apple.com/fr/app/twake/id1588764852?l=en',
mobile_googleplay: 'https://play.google.com/store/apps/details?id=com.twake.twake&gl=FR'
websocket_url: 'wss://api.twake.app'
};

In this form:
"export default { env_dev: false, front_root_url: 'https://web.twake.app', api_root_url: 'https://web.twake.app', websocket_url: 'wss://web.twake.app', mobile_redirect: 'mobile.twake.app', mobile_appstore: 'https://apps.apple.com/fr/app/twake/id1588764852?l=en', mobile_googleplay: 'https://play.google.com/store/apps/details?id=com.twake.twake&gl=FR' };"
"export default { env_dev: false, front_root_url: 'https://web.twake.app', api_root_url: 'https://web.twake.app', websocket_url: 'wss://web.twake.app' };"
*/
3 changes: 0 additions & 3 deletions twake/frontend/src/app/environment/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export type EnvironmentType = {
front_root_url: string;
websocket_url: string;

mobile_redirect?: string;
mobile_appstore?: string;
mobile_googleplay?: string;
sentry_dsn?: string | boolean;
usetiful_token?: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export type ServerInfoType = null | {
branding: any;
help_url: string | null;
pricing_plan_url: string | null;
mobile: {
mobile_redirect: string;
mobile_appstore: string;
mobile_googleplay: string;
};
accounts: {
type: 'console' | 'internal';
console?: ConsoleConfiguration;
Expand Down
17 changes: 10 additions & 7 deletions twake/frontend/src/app/views/mobile-redirect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import configuration from '../../environment/environment';
import { Smartphone, X } from 'react-feather';
import './style.scss';
import { Button } from 'antd';
import InitService from '../../features/global/services/init-service';

const environment = configuration as any;

export default function MobileRedirect(props: { children: ReactNode }) {
const os = getDevice();
const searchParams = Object.fromEntries(new URLSearchParams(window.location.search)) as any;

const parameters = InitService.server_infos?.configuration.mobile;

const getapp = searchParams.getapp;
const forceUseWeb = searchParams.useweb;
const originInUrl = searchParams.origin;
Expand All @@ -20,29 +23,29 @@ export default function MobileRedirect(props: { children: ReactNode }) {
delete searchParams.origin;

//If requested in url: redirect to stores
if (getapp && environment.mobile_appstore && environment.mobile_googleplay) {
if (getapp && parameters?.mobile_appstore && parameters?.mobile_googleplay) {
if (os === 'android') {
document.location.replace(environment.mobile_googleplay);
document.location.replace(parameters?.mobile_googleplay);
} else if (os === 'ios') {
document.location.replace(environment.mobile_appstore);
document.location.replace(parameters?.mobile_appstore);
}
}

//For desktop we don't show the open on app popup
if (
forceUseWeb ||
os === 'other' ||
!environment.mobile_redirect ||
(!environment.front_root_url && !originInUrl) ||
!parameters?.mobile_redirect ||
(!environment?.front_root_url && !originInUrl) ||
typeof window === 'undefined'
) {
return <>{props.children}</>;
}

const origin = (originInUrl || environment.front_root_url)
const origin = (originInUrl || environment?.front_root_url)
.replace(/https?:\/\//g, '')
.replace(/\//g, '');
const redirectOrigin = environment.mobile_redirect.replace(/https?:\/\//g, '').replace(/\//g, '');
const redirectOrigin = parameters?.mobile_redirect.replace(/https?:\/\//g, '').replace(/\//g, '');

//For mobile first we ensure to be on the m.domain.com url
if (window.location.origin.replace(/https?:\/\//g, '').replace(/\//g, '') !== redirectOrigin) {
Expand Down

0 comments on commit 6d2057f

Please sign in to comment.