Skip to content

Commit 655303c

Browse files
abhiraj75Nihal4777
andauthored
refactor: convert tutorials.js to TypeScript (#746)
* refactor: convert tutorial.js to TypeScript * docs: add doc comments for tutorial exports * refactor: safely handle tutorial DOM access and align storage flag * refactor: align tutorials_tour_done storage with existing convention * refactor: safely handle tutorial positioning and unify storage key * refactor: remove unused driver instance and finalize tutorial lifecycle * refactor: update tutorials tour logic and typings * refactor: move className and showButtons to popover object * refactor: tutorials.js(v1) to tutorials.ts --------- Co-authored-by: Nihal <65150640+Nihal4777@users.noreply.github.com>
1 parent afe78de commit 655303c

File tree

2 files changed

+80
-38
lines changed

2 files changed

+80
-38
lines changed
Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import Driver from 'driver.js'
2+
/**
3+
* Defines the steps used by the Driver.js tutorial tour.
4+
* Each step highlights a UI element and displays contextual guidance.
5+
*/
26

3-
export const tour = [
7+
export const tour: Driver.Step [] = [
48
{
59
element: '#guide_1',
6-
className: 'guide_1',
710
popover: {
8-
className: '',
11+
className: 'guide_1',
912
title: 'Circuit Elements panel',
1013
description:
1114
'This is where you can find all the circuit elements that are offered to build amazing circuits.',
@@ -87,46 +90,64 @@ export const tour = [
8790
]
8891

8992
// Not used currently
90-
export const tutorialWrapper = () => {
93+
94+
/**
95+
* Initializes a one-time tutorial highlight for the circuit elements panel.
96+
* Currently unused but retained for future onboarding flows.
97+
*/
98+
export const tutorialWrapper = (): void => {
9199
const panelHighlight = new Driver()
92-
document.querySelector('.panelHeader').addEventListener('click', (e) => {
93-
if (localStorage.tutorials === 'next') {
94-
panelHighlight.highlight({
100+
const panelHeaderEl = document.querySelector('.panelHeader')
101+
if (!panelHeaderEl) return
102+
panelHeaderEl.addEventListener('click', (e: Event) => {
103+
if (localStorage.getItem('tutorials') === 'next') {
104+
const target = e.target
105+
if (!(target instanceof HTMLElement)) return
106+
const sibling = target.nextElementSibling as HTMLElement | null
107+
const siblingHeight = sibling ? sibling.offsetHeight : 0
108+
const step: Driver.Step = {
95109
element: '#guide_1',
96-
showButtons: false,
97110
popover: {
111+
showButtons: false,
98112
title: 'Here are the elements',
99113
description:
100114
'Select any element by clicking on it & then click anywhere on the grid to place the element.',
101115
position: 'right',
102116
offset:
103-
e.target.nextElementSibling.offsetHeight +
104-
e.target.offsetTop -
105-
45,
117+
siblingHeight + target.offsetTop - 45,
106118
},
107-
})
119+
}
120+
panelHighlight.highlight(step)
108121
localStorage.setItem('tutorials', 'done')
109122
}
110123
}, {
111124
once: true,
112125
})
113-
document.querySelector('.icon').addEventListener('click', () => {
114-
panelHighlight.reset(true)
115-
})
126+
const iconEl = document.querySelector('.icon')
127+
if (iconEl) {
128+
iconEl.addEventListener('click', () => {
129+
panelHighlight.reset(true)
130+
})
131+
}
116132
}
117-
118133
const animatedTourDriver = new Driver({
119134
animate: true,
120135
opacity: 0.8,
121136
padding: 5,
122137
showButtons: true,
123138
})
124139

125-
export function showTourGuide() {
126-
document.querySelector('.draggable-panel .maximize').click();
140+
/**
141+
* Launches the interactive tutorial tour for the simulator UI.
142+
*/
143+
export function showTourGuide(): void {
144+
const maximizeButton = document.querySelector('.draggable-panel .maximize') as HTMLElement | null
145+
if (maximizeButton) {
146+
maximizeButton.click()
147+
}
127148
animatedTourDriver.defineSteps(tour)
128149
animatedTourDriver.start()
129-
localStorage.setItem('tutorials_tour_done', true)
150+
localStorage.setItem('tutorials_tour_done', 'true')
130151
}
131152

132153
export default showTourGuide
Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import Driver from 'driver.js'
2+
/**
3+
* Defines the steps used by the Driver.js tutorial tour.
4+
* Each step highlights a UI element and displays contextual guidance.
5+
*/
26

3-
export const tour = [
7+
export const tour: Driver.Step [] = [
48
{
59
element: '#guide_1',
6-
className: 'guide_1',
710
popover: {
8-
className: '',
11+
className: 'guide_1',
912
title: 'Circuit Elements panel',
1013
description:
1114
'This is where you can find all the circuit elements that are offered to build amazing circuits.',
@@ -87,46 +90,64 @@ export const tour = [
8790
]
8891

8992
// Not used currently
90-
export const tutorialWrapper = () => {
93+
94+
/**
95+
* Initializes a one-time tutorial highlight for the circuit elements panel.
96+
* Currently unused but retained for future onboarding flows.
97+
*/
98+
export const tutorialWrapper = (): void => {
9199
const panelHighlight = new Driver()
92-
document.querySelector('.panelHeader').addEventListener('click', (e) => {
93-
if (localStorage.tutorials === 'next') {
94-
panelHighlight.highlight({
100+
const panelHeaderEl = document.querySelector('.panelHeader')
101+
if (!panelHeaderEl) return
102+
panelHeaderEl.addEventListener('click', (e: Event) => {
103+
if (localStorage.getItem('tutorials') === 'next') {
104+
const target = e.target
105+
if (!(target instanceof HTMLElement)) return
106+
const sibling = target.nextElementSibling as HTMLElement | null
107+
const siblingHeight = sibling ? sibling.offsetHeight : 0
108+
const step: Driver.Step = {
95109
element: '#guide_1',
96-
showButtons: false,
97110
popover: {
111+
showButtons: false,
98112
title: 'Here are the elements',
99113
description:
100114
'Select any element by clicking on it & then click anywhere on the grid to place the element.',
101115
position: 'right',
102116
offset:
103-
e.target.nextElementSibling.offsetHeight +
104-
e.target.offsetTop -
105-
45,
117+
siblingHeight + target.offsetTop - 45,
106118
},
107-
})
119+
}
120+
panelHighlight.highlight(step)
108121
localStorage.setItem('tutorials', 'done')
109122
}
110123
}, {
111124
once: true,
112125
})
113-
document.querySelector('.icon').addEventListener('click', () => {
114-
panelHighlight.reset(true)
115-
})
126+
const iconEl = document.querySelector('.icon')
127+
if (iconEl) {
128+
iconEl.addEventListener('click', () => {
129+
panelHighlight.reset(true)
130+
})
131+
}
116132
}
117-
118133
const animatedTourDriver = new Driver({
119134
animate: true,
120135
opacity: 0.8,
121136
padding: 5,
122137
showButtons: true,
123138
})
124139

125-
export function showTourGuide() {
126-
document.querySelector('.draggable-panel .maximize').click();
140+
/**
141+
* Launches the interactive tutorial tour for the simulator UI.
142+
*/
143+
export function showTourGuide(): void {
144+
const maximizeButton = document.querySelector('.draggable-panel .maximize') as HTMLElement | null
145+
if (maximizeButton) {
146+
maximizeButton.click()
147+
}
127148
animatedTourDriver.defineSteps(tour)
128149
animatedTourDriver.start()
129-
localStorage.setItem('tutorials_tour_done', true)
150+
localStorage.setItem('tutorials_tour_done', 'true')
130151
}
131152

132153
export default showTourGuide

0 commit comments

Comments
 (0)