diff --git a/manifest.json b/manifest.json index 16931f6..0181331 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "TimeChimp Billability Chart", "description": "Adds a billability chart on the TimeChimp hours page.", - "version": "1.3.0", + "version": "1.3.1", "manifest_version": 3, "permissions": [ "webRequest" diff --git a/src/content/add-billability-chart.ts b/src/content/add-billability-chart.ts index 5da525a..bf96c10 100644 --- a/src/content/add-billability-chart.ts +++ b/src/content/add-billability-chart.ts @@ -30,15 +30,16 @@ async function doAddBillabilityChart(date: Date, userId: number) { return; } - let chartContainer = addTimePanel.querySelector('#billability-card'); - if (!chartContainer) { + // Check if the chart container already exists. + // If not, create a new element which can be used as the chart parent. + let chartContainer: HTMLElement | undefined; + if (!addTimePanel.querySelector('#billability-card')) { chartContainer = addTimePanel.appendChild(createBillabilityCard()); } const times = await getTimes(userId, date, GET_TIMES_WEEKS); - const stats = calculateTimeStats(times, SHOW_WEEKS, ROLLING_AVG_WEEKS); - createOrUpdateChart(chartContainer as HTMLElement, stats); + createOrUpdateChart(stats, chartContainer); } function createBillabilityCard() { diff --git a/src/content/chart.ts b/src/content/chart.ts index 54ec1e4..1b13706 100644 --- a/src/content/chart.ts +++ b/src/content/chart.ts @@ -10,11 +10,11 @@ const textStyle = { fontSize: '12px', }; -let chart: Highcharts.Chart; +let chart: Highcharts.Chart | undefined; export function createOrUpdateChart( - element: HTMLElement, rollingStats: RollingStats[], + element?: HTMLElement, ) { const options: Highcharts.Options = { chart: { @@ -115,11 +115,13 @@ export function createOrUpdateChart( }, }; - if (chart) { - console.debug('Updating existing chart'); + if (element) { + console.debug('Creating new chart.'); + chart = Highcharts.chart(element, options); + } else if (chart) { + console.debug('Updating existing chart.'); chart.update(options); } else { - console.debug('Creating new chart'); - chart = Highcharts.chart(element, options); + console.error('No chart container given and no existing chart set.'); } }