|
| 1 | +import { fakeString } from '../../lib/fake-strings.js'; |
| 2 | +import { notify, notifyError } from '../../lib/notifications.js'; |
| 3 | +import { defineSmartComponent } from '../../lib/smart/define-smart-component.js'; |
| 4 | +import { generateDocsHref } from '../../lib/utils.js'; |
| 5 | +import { i18n } from '../../translations/translation.js'; |
| 6 | +import '../cc-smart-container/cc-smart-container.js'; |
| 7 | +import { CcAddonHeaderClient } from './cc-addon-header.client.js'; |
| 8 | +import './cc-addon-header.js'; |
| 9 | + |
| 10 | +const DOCS_URL = generateDocsHref(`/addons/metabase`); |
| 11 | +const PROVIDER_ID = 'metabase'; |
| 12 | + |
| 13 | +/** |
| 14 | + * @typedef {import('./cc-addon-header.js').CcAddonHeader} CcAddonHeader |
| 15 | + * @typedef {import('../../lib/smart/smart-component.types.js').OnContextUpdateArgs<CcAddonHeader>} OnContextUpdateArgs |
| 16 | + */ |
| 17 | + |
| 18 | +defineSmartComponent({ |
| 19 | + selector: 'cc-addon-header[smart-mode=metabase]', |
| 20 | + params: { |
| 21 | + apiConfig: { type: Object }, |
| 22 | + ownerId: { type: String }, |
| 23 | + addonId: { type: String }, |
| 24 | + logsUrlPattern: { type: String }, |
| 25 | + productStatus: { type: String, optional: true }, |
| 26 | + }, |
| 27 | + |
| 28 | + /** @param {OnContextUpdateArgs} args */ |
| 29 | + onContextUpdate({ context, updateComponent, onEvent, signal }) { |
| 30 | + const { apiConfig, ownerId, addonId, productStatus } = context; |
| 31 | + const api = new CcAddonHeaderClient({ apiConfig, ownerId, addonId, providerId: PROVIDER_ID, signal }); |
| 32 | + let logsUrl = ''; |
| 33 | + |
| 34 | + updateComponent('state', { |
| 35 | + type: 'loading', |
| 36 | + logsUrl: fakeString(15), |
| 37 | + openLinks: [ |
| 38 | + { |
| 39 | + url: fakeString(15), |
| 40 | + name: fakeString(5), |
| 41 | + }, |
| 42 | + ], |
| 43 | + actions: { |
| 44 | + restart: true, |
| 45 | + rebuildAndRestart: true, |
| 46 | + }, |
| 47 | + productStatus: fakeString(4), |
| 48 | + }); |
| 49 | + |
| 50 | + api |
| 51 | + .getAddonWithOperatorAndZone() |
| 52 | + .then(({ rawAddon, operator, zone }) => { |
| 53 | + const javaAppId = operator.resources.entrypoint; |
| 54 | + logsUrl = context.logsUrlPattern.replace(':id', javaAppId); |
| 55 | + |
| 56 | + updateComponent('state', { |
| 57 | + type: 'loaded', |
| 58 | + providerId: rawAddon.provider.name, |
| 59 | + providerLogoUrl: rawAddon.provider.logoUrl, |
| 60 | + name: rawAddon.name, |
| 61 | + id: rawAddon.realId, |
| 62 | + zone, |
| 63 | + logsUrl, |
| 64 | + openLinks: [ |
| 65 | + { |
| 66 | + name: 'METABASE', |
| 67 | + url: operator.accessUrl, |
| 68 | + }, |
| 69 | + ], |
| 70 | + actions: { |
| 71 | + restart: true, |
| 72 | + rebuildAndRestart: true, |
| 73 | + }, |
| 74 | + productStatus, |
| 75 | + }); |
| 76 | + }) |
| 77 | + .catch((error) => { |
| 78 | + console.error(error); |
| 79 | + updateComponent('state', { |
| 80 | + type: 'error', |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + onEvent('cc-addon-restart', () => { |
| 85 | + updateComponent('state', (state) => { |
| 86 | + state.type = 'restarting'; |
| 87 | + }); |
| 88 | + |
| 89 | + api |
| 90 | + .restartAddon() |
| 91 | + .then(() => { |
| 92 | + notify({ |
| 93 | + intent: 'success', |
| 94 | + message: i18n('cc-addon-header.restart.success.message', { logsUrl, docsUrl: DOCS_URL }), |
| 95 | + title: i18n('cc-addon-header.restart.success.title'), |
| 96 | + options: { |
| 97 | + timeout: 0, |
| 98 | + closeable: true, |
| 99 | + }, |
| 100 | + }); |
| 101 | + }) |
| 102 | + .catch((error) => { |
| 103 | + console.error(error); |
| 104 | + notifyError(i18n('cc-addon-header.restart.error')); |
| 105 | + }) |
| 106 | + .finally(() => { |
| 107 | + updateComponent('state', (state) => { |
| 108 | + state.type = 'loaded'; |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + onEvent('cc-addon-rebuild', () => { |
| 114 | + updateComponent('state', (state) => { |
| 115 | + state.type = 'rebuilding'; |
| 116 | + }); |
| 117 | + |
| 118 | + api |
| 119 | + .rebuildAndRestartAddon() |
| 120 | + .then(() => { |
| 121 | + notify({ |
| 122 | + intent: 'success', |
| 123 | + message: i18n('cc-addon-header.rebuild.success.message', { logsUrl, docsUrl: DOCS_URL }), |
| 124 | + title: i18n('cc-addon-header.rebuild.success.title'), |
| 125 | + options: { |
| 126 | + timeout: 0, |
| 127 | + closeable: true, |
| 128 | + }, |
| 129 | + }); |
| 130 | + }) |
| 131 | + .catch((error) => { |
| 132 | + console.error(error); |
| 133 | + notifyError(i18n('cc-addon-header.rebuild.error')); |
| 134 | + }) |
| 135 | + .finally(() => { |
| 136 | + updateComponent('state', (state) => { |
| 137 | + state.type = 'loaded'; |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | + }, |
| 142 | +}); |
0 commit comments