Skip to content

Commit

Permalink
feat: centralized admin calls and error handling (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
rofe authored May 27, 2024
1 parent 282cf65 commit 57af9d6
Show file tree
Hide file tree
Showing 33 changed files with 1,322 additions and 885 deletions.
57 changes: 21 additions & 36 deletions src/extension/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@
"config_invalid_shareurl": {
"message": "Invalid sharing URL"
},
"config_manual_devMode": {
"message": "Test project configuration locally"
},
"config_manual_devOrigin_placeholder": {
"message": "Local Development URL: http://localhost:3000"
},
Expand Down Expand Up @@ -297,9 +294,6 @@
"delete": {
"message": "Delete"
},
"delete_failure": {
"message": "Deletion failed. Please try again later."
},
"delete_file_confirm": {
"message": "Are you sure you want to delete this file?"
},
Expand All @@ -322,7 +316,7 @@
"message": "This page still has a source document and cannot be deleted."
},
"delete_page_success": {
"message": "Page successfully deleted."
"message": "Page successfully deleted, reloading..."
},
"description": {
"message": "AEM Sidekick allows authors of AEM sites to easily preview and publish content."
Expand All @@ -345,21 +339,30 @@
"edit_date": {
"message": "Edited: "
},
"error_config_failure": {
"message": "Failed to activate configuration: "
"error_429": {
"message": "429 Too many requests: Your project is being throttled by $1. Please wait and try again later."
},
"error_delete": {
"message": "Deletion failed. Please try again later."
},
"error_fatal": {
"message": "Apologies, we seem to be having problems at the moment. Please try again later."
},
"error_login_aborted": {
"message": "Sign in aborted."
},
"error_login_timeout": {
"message": "Sign in timed out. Please try again later."
},
"error_logout_error": {
"error_logout": {
"message": "Sign out failed. Please try again later."
},
"error_preview_failure": {
"error_preview": {
"message": "Preview generation failed. Please try again later."
},
"error_preview_404": {
"message": "404 Not found: Source document does not exist, preview generation failed."
},
"error_preview_not_gdoc_generic": {
"message": "Preview generation failed. Must be Google document or spreadsheet."
},
Expand All @@ -369,6 +372,9 @@
"error_preview_not_gsheet_ms_excel": {
"message": "This is a Microsoft Excel document. Please convert it to Google Sheets: File > Save as Google Sheets"
},
"error_publish": {
"message": "Publication failed. Please try again later."
},
"error_status_404_content": {
"message": "404 Not found: Check your Sidekick configuration or URL."
},
Expand All @@ -384,14 +390,11 @@
"error_status_504": {
"message": "504 Gateway timeout: Failed to fetch the page status. Please try again later."
},
"error_status_fatal": {
"message": "Apologies, we seem to be having problems at the moment. Please try again later. Error: $1"
},
"error_status_invalid": {
"message": "Invalid server response. Check your Sidekick configuration or URL."
},
"error_status_preview_404": {
"message": "404 Not found: Source document does not exist, failed to preview."
"error_unpublish": {
"message": "Unpublication failed. Please try again later."
},
"extensions": {
"message": "Extensions"
Expand Down Expand Up @@ -489,9 +492,6 @@
"preview": {
"message": "Preview"
},
"preview_config_success": {
"message": "Configuration successfully activated."
},
"preview_date": {
"message": "Previewed: "
},
Expand All @@ -510,21 +510,9 @@
"publish_date": {
"message": "Published: "
},
"publish_failure": {
"message": "Publication failed. Please try again later."
},
"push_down_content": {
"message": "Push down content instead of overlaying"
},
"push_down_content_description": {
"message": "Note: Depending on the site's structure, this may require custom project configuration to work as expected."
},
"reload": {
"message": "Reload"
},
"reload_failure": {
"message": "Reloading failed. Please try again later."
},
"save": {
"message": "Save"
},
Expand All @@ -550,9 +538,6 @@
"unpublish": {
"message": "Unpublish"
},
"unpublish_failure": {
"message": "Unpublication failed. Please try again later."
},
"unpublish_page_confirm": {
"message": "Are you sure you want to unpublish this page?"
},
Expand All @@ -563,7 +548,7 @@
"message": "This page no longer has a source document, unpublishing it cannot be undone!\n\nAre you sure you want to unpublish it?"
},
"unpublish_page_success": {
"message": "Page successfully unpublished."
"message": "Page successfully unpublished, reloading..."
},
"user_login": {
"message": "Sign in"
Expand Down Expand Up @@ -646,7 +631,7 @@
"destructive_confirmation_invalid": {
"message": "Sorry, please enter the text exactly as displayed to confirm."
},
"reload_success": {
"reload_success": {
"message": "Preview successfully updated, reloading..."
},
"preview_success": {
Expand Down
13 changes: 7 additions & 6 deletions src/extension/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
isValidProject,
getProject,
} from './project.js';
import { ADMIN_ORIGIN } from './utils/admin.js';

/**
* Updates the auth token via external messaging API (admin only).
Expand All @@ -36,7 +37,7 @@ async function updateAuthToken({
const { url } = sender;
if (owner) {
try {
if (new URL(url).origin === 'https://admin.hlx.page'
if (new URL(url).origin === ADMIN_ORIGIN
&& authToken !== undefined) {
await setAuthToken(owner, authToken, exp);
return 'close';
Expand Down Expand Up @@ -95,19 +96,19 @@ async function openViewDocSource({ id }) {
* @param {number} id The tab ID
*/
export async function checkViewDocSource(id) {
const tab = await chrome.tabs.get(id);
if (!tab || !tab.url || !tab.active) {
return;
}
try {
const tab = await chrome.tabs.get(id);
if (!tab || !tab.url || !tab.active) {
return;
}
const u = new URL(tab.url);
const vds = u.searchParams.get('view-doc-source');
if (vds && vds === 'true') {
// @ts-ignore
await openViewDocSource({ id });
}
} catch (e) {
log.warn(`Error checking view document source for url: ${tab.url}`, e);
log.warn(`Error checking view document source for tab ${id}`, e);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/extension/app/components/action-bar/action-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export class ActionBar extends LitElement {
background-color: var(--spectrum2-color-positive);
border: 1px solid var(--spectrum2-color-positive);
}
:host(.warning) .action-bar {
transition: background-color 500ms;
background-color: var(--spectrum2-color-warning);
border: 1px solid var(--spectrum2-color-warning);
}
:host(.negative) .action-bar {
transition: background-color 500ms;
background-color: var(--spectrum2-color-negative);
Expand All @@ -44,6 +49,7 @@ export class ActionBar extends LitElement {
}
:host(.positive) slot::slotted(sp-action-group),
:host(.warning) slot::slotted(sp-action-group),
:host(.negative) slot::slotted(sp-action-group),
:host(.info) slot::slotted(sp-action-group) {
width: 100%;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class ActivityAction extends ConnectedElement {
case 'positive':
return ICONS.CHECKMARK;
case 'negative':
case 'warning':
return ICONS.ALERT_TRIANGLE;
case 'info':
return ICONS.INFO;
Expand Down
46 changes: 22 additions & 24 deletions src/extension/app/plugins/delete/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
* governing permissions and limitations under the License.
*/

import { log } from '../../../log.js';
import { Plugin } from '../../components/plugin/plugin.js';
import {
MODALS,
MODAL_EVENTS,
RESTRICTED_PATHS,
STATE,
} from '../../constants.js';
import { newTab } from '../../utils/browser.js';

/**
* @typedef {import('@AppStore').AppStore} AppStore
Expand All @@ -43,8 +43,8 @@ export function createDeletePlugin(appStore) {
pinned: false,
button: {
text: appStore.i18n('delete'),
action: async () => {
const { location, status } = appStore;
action: async (evt) => {
const { status } = appStore;
const isPage = status.webPath.split('/').pop().indexOf('.') === -1;
const hasSrc = status.edit?.status === 200;

Expand All @@ -63,28 +63,26 @@ export function createDeletePlugin(appStore) {
modal.addEventListener(MODAL_EVENTS.CONFIRM, async () => {
// perform delete
appStore.setState(STATE.DELETING);
try {
const resp = await appStore.delete();
if (resp.ok) {
// show success toast
appStore.showToast(
isPage
? appStore.i18n('delete_page_success')
: appStore.i18n('delete_file_success'),
'positive',
() => {
log.info(`redirecting to ${location.origin}/`);
appStore.loadPage(`${location.origin}/`);
},
);
} else {
throw new Error(resp.headers?.['x-error']);
}
} catch (e) {

const res = await appStore.delete();
if (res) {
const actionCallback = () => {
appStore.reloadPage(newTab(evt));
};

const closeCallback = () => {
appStore.closeToast();
};

// show success toast
appStore.showToast(
appStore.i18n('delete_failure'),
'negative',
() => appStore.closeToast(),
isPage
? appStore.i18n('delete_page_success')
: appStore.i18n('delete_file_success'),
'positive',
closeCallback,
actionCallback,
appStore.i18n('reload'),
);
}
});
Expand Down
20 changes: 9 additions & 11 deletions src/extension/app/plugins/publish/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export function createPublishPlugin(appStore) {
button: {
text: appStore.i18n('publish'),
action: async (evt) => {
const { location, siteStore } = appStore;
const path = location.pathname;
const { siteStore } = appStore;
appStore.setState(STATE.PUBLISHNG);
const res = await appStore.publish(path);
if (res.ok) {

const res = await appStore.publish();
if (res) {
const actionCallback = () => {
appStore.switchEnv('prod', newTab(evt));
};
Expand All @@ -49,14 +49,12 @@ export function createPublishPlugin(appStore) {

const { host } = siteStore;
const targetEnv = host ? 'production' : 'live';
appStore.showToast(appStore.i18n('publish_success').replace('$1', appStore.i18n(targetEnv).toLowerCase()), 'positive', closeCallback, actionCallback, appStore.i18n('open'));
} else {
// eslint-disable-next-line no-console
console.error(res);
appStore.showToast(
appStore.i18n('publish_failure'),
'negative',
() => appStore.closeToast(),
appStore.i18n('publish_success').replace('$1', appStore.i18n(targetEnv)),
'positive',
closeCallback,
actionCallback,
appStore.i18n('open'),
);
}
},
Expand Down
14 changes: 3 additions & 11 deletions src/extension/app/plugins/reload/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ export function createReloadPlugin(appStore) {
text: i18n(appStore.languageDict, 'reload'),
action: async (evt) => {
appStore.setState(STATE.PREVIEWING);
try {
const resp = await appStore.update();
if (!resp.ok && resp.status >= 400) {
if (resp.status === 404) {
appStore.showToast(appStore.i18n('error_status_preview_404'), 'negative');
return;
}
throw new Error();
}

const res = await appStore.update();
if (res) {
const closeHandler = () => {
appStore.closeToast();
};
Expand All @@ -54,8 +48,6 @@ export function createReloadPlugin(appStore) {
};

appStore.showToast(i18n(appStore.languageDict, 'reload_success'), 'positive', closeHandler, actionHandler, appStore.i18n('open'));
} catch (e) {
appStore.showToast(appStore.i18n('reload_failure'), 'negative');
}
},
isEnabled: (store) => store.isAuthorized('preview', 'write'),
Expand Down
Loading

0 comments on commit 57af9d6

Please sign in to comment.