Skip to content

Commit

Permalink
Fix page switching breaking chart
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed Nov 7, 2023
1 parent d33844a commit 6036fca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
9 changes: 5 additions & 4 deletions src/content/add-billability-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 8 additions & 6 deletions src/content/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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.');
}
}

0 comments on commit 6036fca

Please sign in to comment.