Skip to content

Commit

Permalink
feat: fetch packageInfo when the Indicator component created (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
baruchiro authored Mar 9, 2023
1 parent f747d65 commit 70e9927
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module.exports = {
es2021: true,
webextensions: true,
},
globals: {
process: 'readonly',
},
extends: ['eslint:recommended', 'prettier'],
overrides: [
{
Expand Down
5 changes: 4 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ async function buildCustomElements(outputDirPath) {
},
},
define: {
'process.env': {},
'process.env': {
// TODO: Need to run this for each extension (chrome/firefox)
EXTENSION_ID: process.env.EXTENSION_ID,
},
},
plugins: [vue({ customElement: true }), svgLoader()],
});
Expand Down
7 changes: 5 additions & 2 deletions src/background/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ const listeners = {
};

export const listen = () => {
chrome.runtime.onMessage.addListener(({ action, data }, _sender, sendResponse) => {
const listener = ({ action, data }, _sender, sendResponse) => {
Promise.resolve(listeners[action](data)).then(sendResponse);
return true;
});
};

chrome.runtime.onMessage.addListener(listener);
chrome.runtime.onMessageExternal.addListener(listener);
};
1 change: 1 addition & 0 deletions src/content/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PACKAGE_INFO_ACTION } from '../bridge-sync';
export const getPackageInfo = async (packageID) => {
return new Promise((resolve) =>
chrome.runtime.sendMessage(
process.env.EXTENSION_ID,
{
action: PACKAGE_INFO_ACTION,
data: packageID,
Expand Down
33 changes: 20 additions & 13 deletions src/custom-elements/indicator.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div
class="overlay-indicator"
:class="{ 'overlay-indicator--issues': package.issues ?? length }"
:class="{ 'overlay-indicator--issues': issues }"
@mouseenter="
initTooltipPosition();
this.overIndicator = true;
Expand All @@ -13,7 +13,7 @@
"
ref="overlay"
>
<div class="overlay-indicator__icon">{{ package.issues }}</div>
<div class="overlay-indicator__icon">{{ issues }}</div>
<div class="overlay-indicator__text">
<slot></slot>
</div>
Expand All @@ -29,21 +29,24 @@
ref="overlayTooltip"
>
<!-- test to open url in new page svg-->
<a target="_blank" :href="`https://www.npmjs.com/package/${package.name}`">{{ package.name }}</a>
<a target="_blank" :href="`https://www.npmjs.com/package/${packageInfo?.name}`">{{ packageInfo?.name }}</a>

<!-- test to load svg-->
<component :is="`${package.type}_logo`"></component>
<component :is="`${packageInfo?.type}_logo`"></component>

<!-- just show all package json at the moment-->
<div>{{ package }}</div>
<div>{{ packageInfo }}</div>
</div>
</teleport>
</div>
</template>

<script>
import { getPackageInfo } from '../content/bridge';
import npm_logo from './assets/npm_logo.svg?component';
const sum = (arr) => arr.reduce((a, b) => a + b, 0);
const TOOLTIP_POSITION = {
TOP_START: 'top_start',
TOP_END: 'top_end',
Expand Down Expand Up @@ -74,17 +77,15 @@ export default {
tooltipOpen: false,
overTooltip: false,
overIndicator: false,
packageInfo: null,
};
},
computed: {
store() {
return window.__overlay_global_store || {};
},
packageId() {
return `${this.overlayIndicatorPackageType}/${this.overlayIndicatorPackageName}`;
},
package() {
return this.store.packages[this.packageId] || {};
issues() {
if (!this.packageInfo?.sources) return 0;
const sources = Object.values(this.packageInfo.sources).filter((s) => s);
return sum(sources.map(({ issues }) => issues));
},
},
methods: {
Expand Down Expand Up @@ -263,6 +264,12 @@ export default {
}, 300);
},
},
created() {
getPackageInfo({ type: this.overlayIndicatorPackageType, name: this.overlayIndicatorPackageName }).then((res) => {
console.log('res', res);
this.packageInfo = res;
});
},
mounted() {
this.initTooltipPosition();
},
Expand Down
3 changes: 3 additions & 0 deletions src/manifest.chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@
"matches": ["<all_urls>"]
}
],
"externally_connectable": {
"matches": ["*://stackoverflow.com/*"]
},
"manifest_version": 3
}

0 comments on commit 70e9927

Please sign in to comment.