Skip to content

Commit 7722bb2

Browse files
committed
refactor: refactor preview code
1 parent a994496 commit 7722bb2

21 files changed

+135
-103
lines changed

bin/todoctor.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ try {
2828
process.exit(1)
2929
}
3030

31-
let distPath = path.join(dirname, '../dist')
32-
let args = process.argv.slice(2)
33-
let env = { ...process.env, DIST_PATH: distPath }
34-
let child = spawn(binaryPath, args, { stdio: 'inherit', env })
31+
let distributionPath = path.join(dirname, '../dist')
32+
let arguments_ = process.argv.slice(2)
33+
let environment = { ...process.env, DIST_PATH: distributionPath }
34+
let child = spawn(binaryPath, arguments_, {
35+
stdio: 'inherit',
36+
env: environment,
37+
})
3538

3639
child.on('exit', code => {
3740
process.exit(code)
File renamed without changes.

preview/blocks/footer.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
<Typography size="m">Released under the MIT License</Typography>
1313
<Typography size="m">Copyright © {currentYear} Azat S.</Typography>
1414
</div>
15-
<a href="https://github.com/azat-io/todoctor" target="_blank">
15+
<a
16+
href="https://github.com/azat-io/todoctor"
17+
rel="noopener noreferrer"
18+
target="_blank"
19+
>
1620
<Typography size="m">Source code on GitHub</Typography>
1721
</a>
1822
</div>

preview/blocks/graph.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
$data.history?.sort(
99
(a, b) => new Date(a.date).getTime() - new Date(b.date).getTime(),
1010
) ?? []
11-
$: labels = history?.map(({ date }) => date)
12-
$: values = history?.map(({ count }) => count)
11+
$: labels = history.map(({ date }) => date)
12+
$: values = history.map(({ count }) => count)
1313
</script>
1414

1515
<div class="graph-wrapper">

preview/blocks/header.svelte

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
88
// eslint-disable-next-line no-unused-expressions
99
$theme
10+
11+
let { version, name } = $data
1012
</script>
1113

1214
<header class="header">
@@ -18,21 +20,25 @@
1820
<div>
1921
<div class="title-wrapper">
2022
<Typography size="2xl" tag="h1" bold>Todoctor</Typography>
21-
{#if $data.version}
22-
<sub class="version">v{$data.version}</sub>
23+
{#if version}
24+
<sub class="version">v{version}</sub>
2325
{/if}
2426
</div>
25-
{#if $data.name}
26-
<Typography size="m">{$data.name}</Typography>
27+
{#if name}
28+
<Typography size="m">{name}</Typography>
2729
{/if}
2830
</div>
2931
</div>
3032
</div>
3133
<div class="links">
32-
<button on:click={toggleTheme} class="button">
34+
<button on:click={toggleTheme} class="button" type="button">
3335
<Typography color="brand" size="m">Toggle Theme</Typography>
3436
</button>
35-
<a href="https://github.com/azat-io/todoctor" target="_blank">
37+
<a
38+
href="https://github.com/azat-io/todoctor"
39+
rel="noopener noreferrer"
40+
target="_blank"
41+
>
3642
<Typography color="brand" size="m">GitHub</Typography>
3743
</a>
3844
</div>

preview/blocks/info.svelte

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
$: todosByKind =
1111
$data.data?.reduce((accumulator: Record<string, number>, { kind }) => {
1212
if (kind in accumulator) {
13-
accumulator[kind] = accumulator[kind] + 1
13+
accumulator[kind] += 1
1414
} else {
1515
accumulator[kind] = 1
1616
}
1717
return accumulator
18-
}, {}) || {}
18+
}, {}) ?? {}
1919
2020
$: todosEntries = Object.entries(todosByKind).sort(([, a], [, b]) => b - a)
2121
@@ -42,7 +42,7 @@
4242
'senary',
4343
]
4444
45-
$: notes = [] as {
45+
let notes = [] as {
4646
content: string[]
4747
title: string
4848
}[]
@@ -69,7 +69,7 @@
6969
let todosByAuthors = $data.data.reduce(
7070
(accumulator: Record<string, number>, { blame }) => {
7171
if (blame.author in accumulator) {
72-
accumulator[blame.author] = accumulator[blame.author] + 1
72+
accumulator[blame.author] += 1
7373
} else {
7474
accumulator[blame.author] = 1
7575
}
@@ -128,7 +128,7 @@
128128
<div class="notes-wrapper">
129129
<Typography size="xl" tag="h2" mbe="l">Did You Know That?</Typography>
130130
<div class="notes">
131-
{#each notes as { content, title }}
131+
{#each notes as { content, title } (title)}
132132
<Note {title} {content} />
133133
{/each}
134134
</div>
@@ -143,12 +143,12 @@
143143
</div>
144144
</div>
145145
<div class="legend">
146-
{#each finalTodosEntries as [kind], index}
146+
{#each finalTodosEntries as [kind], index (kind)}
147147
<div class="legend-element">
148148
<div
149149
style={`--color: var(--color-additional-${colors[index]});`}
150150
class="legend-color"
151-
></div>
151+
/>
152152
<Typography size="s" tag="span">{kind.toUpperCase()}</Typography>
153153
</div>
154154
{/each}

preview/elements/avatar.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
let transformToCode = (string: string, length: number): number => {
1010
let hash = 0
1111
12-
for (let len = string.length, i = 0; i < len; i++) {
12+
for (let stringLength = string.length, i = 0; i < stringLength; i++) {
1313
let chr = string.charCodeAt(i)
1414
hash = (hash << 5) - hash + chr
1515
hash |= 0

preview/elements/chart-doughnut.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<script lang="ts">
22
import type { ChartOptions, ChartData } from 'chart.js'
33
4+
import { onMount } from 'svelte'
5+
46
import Chart from '~/elements/chart.svelte'
57
import { theme } from '~/stores/theme'
68
79
export let values: number[]
810
9-
$: computedStyles = getComputedStyle(document.body)
11+
let computedStyles = getComputedStyle(document.body)
1012
1113
$: data = {
1214
datasets: [
@@ -27,7 +29,7 @@
2729
],
2830
} as ChartData<'doughnut'>
2931
30-
theme.subscribe(() => {
32+
let unsubscribe = theme.subscribe(() => {
3133
computedStyles = getComputedStyle(document.body)
3234
})
3335
@@ -70,6 +72,8 @@
7072
},
7173
},
7274
} as ChartOptions<'doughnut'>
75+
76+
onMount(() => () => unsubscribe)
7377
</script>
7478

7579
<Chart type="doughnut" height={null} {options} {data} />

preview/elements/chart.svelte

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
export let options: ChartOptions<T>
1515
export let height: number | null = null
1616
17-
let canvasRef: HTMLCanvasElement
17+
let canvasReference: HTMLCanvasElement
1818
let chart: Chart<T> | null = null
1919
20-
let resizeObserver: ResizeObserver
20+
let resizeObserver: ResizeObserver | null = null
2121
22-
let createChart = () => {
22+
let createChart = (): void => {
2323
if (chart) {
2424
chart.destroy()
2525
}
2626
27-
chart = new Chart(canvasRef, {
27+
chart = new Chart(canvasReference, {
2828
options: {
2929
...options,
3030
maintainAspectRatio: false,
@@ -42,8 +42,8 @@
4242
chart?.resize()
4343
})
4444
45-
if (canvasRef.parentElement) {
46-
resizeObserver.observe(canvasRef.parentElement)
45+
if (canvasReference.parentElement) {
46+
resizeObserver.observe(canvasReference.parentElement)
4747
}
4848
})
4949
@@ -63,7 +63,7 @@
6363
})
6464
</script>
6565

66-
<canvas class="canvas" bind:this={canvasRef} {height}></canvas>
66+
<canvas class="canvas" bind:this={canvasReference} {height} />
6767

6868
<style>
6969
.canvas {

preview/elements/note.svelte

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
export let title = ''
55
export let content: string[] = []
66
7-
let processLine = (line: string) => {
8-
let parts: Array<{ bold: boolean; text: string }> = []
7+
interface Part {
8+
bold: boolean
9+
text: string
10+
}
11+
12+
let processLine = (line: string): Part[] => {
13+
let parts: Part[] = []
914
10-
let regex = /<b>(.*?)<\/b>/gi
15+
let regex = /<b>(?<content>.*?)<\/b>/giu
1116
1217
let lastIndex = 0
1318
let match
@@ -31,9 +36,9 @@
3136
<div class="note">
3237
<Typography size="l" tag="h3" mbe="s">{title}</Typography>
3338

34-
{#each content as line}
39+
{#each content as line (line)}
3540
<Typography size="m" tag="p" mbe="2xs">
36-
{#each processLine(line) as part}
41+
{#each processLine(line) as part (part.text)}
3742
{#if part.bold}
3843
<b>{part.text}</b>
3944
{:else}

0 commit comments

Comments
 (0)