2022-03-03 15:19:51 +00:00
|
|
|
import { DEFAULT_REDIRECT_URL } from '../constants';
|
2019-08-25 14:29:28 +00:00
|
|
|
|
|
|
|
export const frontendURL = (path, params) => {
|
2022-08-03 11:38:21 +00:00
|
|
|
const stringifiedParams = params ? `?${new URLSearchParams(params)}` : '';
|
2019-08-25 14:29:28 +00:00
|
|
|
return `/app/${path}${stringifiedParams}`;
|
|
|
|
};
|
2020-02-22 08:54:51 +00:00
|
|
|
|
2022-08-08 18:43:06 +00:00
|
|
|
const getSSOAccountPath = ({ ssoAccountId, user }) => {
|
2022-10-06 00:31:12 +00:00
|
|
|
const { accounts = [], account_id = null } = user || {};
|
2022-03-03 15:19:51 +00:00
|
|
|
const ssoAccount = accounts.find(
|
|
|
|
account => account.id === Number(ssoAccountId)
|
|
|
|
);
|
2022-08-08 18:43:06 +00:00
|
|
|
let accountPath = '';
|
2022-03-03 15:19:51 +00:00
|
|
|
if (ssoAccount) {
|
2022-08-08 18:43:06 +00:00
|
|
|
accountPath = `accounts/${ssoAccountId}`;
|
|
|
|
} else if (accounts.length) {
|
2022-10-06 00:31:12 +00:00
|
|
|
// If the account id is not found, redirect to the first account
|
|
|
|
const accountId = account_id || accounts[0].id;
|
|
|
|
accountPath = `accounts/${accountId}`;
|
2022-03-03 15:19:51 +00:00
|
|
|
}
|
2022-08-08 18:43:06 +00:00
|
|
|
return accountPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getLoginRedirectURL = ({
|
|
|
|
ssoAccountId,
|
|
|
|
ssoConversationId,
|
|
|
|
user,
|
|
|
|
}) => {
|
|
|
|
const accountPath = getSSOAccountPath({ ssoAccountId, user });
|
|
|
|
if (accountPath) {
|
|
|
|
if (ssoConversationId) {
|
|
|
|
return frontendURL(`${accountPath}/conversations/${ssoConversationId}`);
|
|
|
|
}
|
|
|
|
return frontendURL(`${accountPath}/dashboard`);
|
2022-04-14 15:24:26 +00:00
|
|
|
}
|
2022-03-03 15:19:51 +00:00
|
|
|
return DEFAULT_REDIRECT_URL;
|
|
|
|
};
|
|
|
|
|
2021-03-16 14:14:50 +00:00
|
|
|
export const conversationUrl = ({
|
|
|
|
accountId,
|
|
|
|
activeInbox,
|
|
|
|
id,
|
|
|
|
label,
|
|
|
|
teamId,
|
2021-12-09 05:50:14 +00:00
|
|
|
conversationType = '',
|
2022-01-27 02:56:12 +00:00
|
|
|
foldersId,
|
2021-03-16 14:14:50 +00:00
|
|
|
}) => {
|
2021-12-09 05:50:14 +00:00
|
|
|
let url = `accounts/${accountId}/conversations/${id}`;
|
2020-06-25 15:34:03 +00:00
|
|
|
if (activeInbox) {
|
2021-12-09 05:50:14 +00:00
|
|
|
url = `accounts/${accountId}/inbox/${activeInbox}/conversations/${id}`;
|
|
|
|
} else if (label) {
|
|
|
|
url = `accounts/${accountId}/label/${label}/conversations/${id}`;
|
|
|
|
} else if (teamId) {
|
|
|
|
url = `accounts/${accountId}/team/${teamId}/conversations/${id}`;
|
2022-01-27 02:56:12 +00:00
|
|
|
} else if (foldersId && foldersId !== 0) {
|
|
|
|
url = `accounts/${accountId}/custom_view/${foldersId}/conversations/${id}`;
|
2021-12-09 05:50:14 +00:00
|
|
|
} else if (conversationType === 'mention') {
|
|
|
|
url = `accounts/${accountId}/mentions/conversations/${id}`;
|
2022-11-29 16:18:00 +00:00
|
|
|
} else if (conversationType === 'unattended') {
|
|
|
|
url = `accounts/${accountId}/unattended/conversations/${id}`;
|
2020-06-25 15:34:03 +00:00
|
|
|
}
|
2021-12-09 05:50:14 +00:00
|
|
|
return url;
|
2020-02-22 08:54:51 +00:00
|
|
|
};
|
2020-04-06 16:47:07 +00:00
|
|
|
|
2022-08-01 05:23:50 +00:00
|
|
|
export const conversationListPageURL = ({
|
|
|
|
accountId,
|
|
|
|
conversationType = '',
|
|
|
|
inboxId,
|
|
|
|
label,
|
|
|
|
teamId,
|
2022-11-24 07:55:45 +00:00
|
|
|
customViewId,
|
2022-08-01 05:23:50 +00:00
|
|
|
}) => {
|
|
|
|
let url = `accounts/${accountId}/dashboard`;
|
|
|
|
if (label) {
|
|
|
|
url = `accounts/${accountId}/label/${label}`;
|
|
|
|
} else if (teamId) {
|
|
|
|
url = `accounts/${accountId}/team/${teamId}`;
|
|
|
|
} else if (inboxId) {
|
|
|
|
url = `accounts/${accountId}/inbox/${inboxId}`;
|
2022-11-24 07:55:45 +00:00
|
|
|
} else if (customViewId) {
|
|
|
|
url = `accounts/${accountId}/custom_view/${customViewId}`;
|
2022-12-02 23:07:38 +00:00
|
|
|
} else if (conversationType) {
|
|
|
|
const urlMap = {
|
|
|
|
mention: 'mentions/conversations',
|
|
|
|
unattended: 'unattended/conversations',
|
|
|
|
};
|
|
|
|
url = `accounts/${accountId}/${urlMap[conversationType]}`;
|
2022-08-01 05:23:50 +00:00
|
|
|
}
|
|
|
|
return frontendURL(url);
|
|
|
|
};
|
|
|
|
|
2022-02-07 07:34:27 +00:00
|
|
|
export const isValidURL = value => {
|
|
|
|
/* eslint-disable no-useless-escape */
|
|
|
|
const URL_REGEX = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/gm;
|
|
|
|
return URL_REGEX.test(value);
|
|
|
|
};
|