Skip to content

Commit eb40d34

Browse files
committed
feat(extension): add loader for note adding
1 parent d017764 commit eb40d34

File tree

9 files changed

+51
-13
lines changed

9 files changed

+51
-13
lines changed

clients/extension/action/app.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { ExtItem, ExtMessage } from '../types'
88
export function App() {
99
const [isOpen, setIsOpen] = useState(false)
1010
const [saveStatus, setSaveStatus] = useState('saving')
11+
const [noteStatus, setNoteStatus] = useState<string | undefined>(undefined)
1112
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined)
1213
const [item, setItem] = useState<ExtItem | undefined>(undefined)
1314

@@ -38,6 +39,7 @@ export function App() {
3839
case EXT_ACTIONS.SAVE_TO_POCKET_SUCCESS: {
3940
const item = message?.item as ExtItem
4041
setItem(item)
42+
setNoteStatus(undefined)
4143
setSaveStatus('saved')
4244
return
4345
}
@@ -51,6 +53,7 @@ export function App() {
5153

5254
case EXT_ACTIONS.ADD_NOTE_FAILURE: {
5355
const { error } = message ?? 'Unknown error'
56+
setNoteStatus(undefined)
5457
setErrorMessage(error)
5558
return
5659
}
@@ -114,6 +117,8 @@ export function App() {
114117
<ActionContainer
115118
errorMessage={errorMessage}
116119
saveStatus={saveStatus}
120+
noteStatus={noteStatus}
121+
setNoteStatus={setNoteStatus}
117122
isOpen={isOpen}
118123
item={item}
119124
actionUnSave={actionUnSave}

clients/extension/background/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async function messageHandler(message: ExtMessage, sender: chrome.runtime.Messag
7979

8080
case EXT_ACTIONS.ADD_NOTE_REQUEST: {
8181
const { noteData } = message
82-
if (noteData && tab.url) await saveNote(noteData, tab.url)
82+
if (noteData && tab?.url) await saveNote(noteData, tab.url)
8383
return true
8484
}
8585

clients/extension/components/action-container/index.tsx

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { useState } from 'react'
22
import { ExtensionError } from '../action-error'
33
import { ExtensionHeader } from '../action-header'
4+
import { ActionLoader } from '../action-loader'
45
import { Notes } from '../notes'
56
import { SavedScreen } from '../saved'
6-
import { SavedLoader } from '../saved-loader'
77

88
// Types
99
import type { ExtItem } from '../../types'
10+
import type { Dispatch, SetStateAction } from 'react'
1011

1112
export function ActionContainer({
1213
isOpen = false,
1314
errorMessage,
15+
noteStatus,
16+
setNoteStatus,
1417
item
1518
}: {
1619
isOpen: boolean
1720
errorMessage?: string
1821
saveStatus?: string
22+
noteStatus?: string
1923
item?: ExtItem
24+
setNoteStatus: Dispatch<SetStateAction<string | undefined>>
2025
actionLogOut: () => void
2126
actionUnSave: () => void
2227
}) {
@@ -34,7 +39,13 @@ export function ActionContainer({
3439
{item?.preview ? (
3540
<>
3641
{showNotes ? (
37-
<Notes notes={notes?.edges} setShowNotes={setShowNotes} setErrorText={setErrorText} />
42+
<Notes
43+
noteStatus={noteStatus}
44+
notes={notes?.edges}
45+
setShowNotes={setShowNotes}
46+
setNoteStatus={setNoteStatus}
47+
setErrorText={setErrorText}
48+
/>
3849
) : (
3950
<SavedScreen
4051
preview={preview!}
@@ -45,7 +56,7 @@ export function ActionContainer({
4556
)}
4657
</>
4758
) : (
48-
<SavedLoader />
59+
<ActionLoader />
4960
)}
5061
{errorText ? <ExtensionError errorMessage={errorText} /> : null}
5162
</div>

clients/extension/components/saved-loader/component.story.tsx clients/extension/components/action-loader/component.story.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Components
2-
import { SavedLoader as Component } from '.'
2+
import { ActionLoader as Component } from '.'
33

44
// Types
55
import type { Meta, StoryObj } from '@storybook/react'
66

77
// Storybook Meta
88
const meta: Meta<typeof Component> = {
9-
title: 'Saved - Loader ',
9+
title: 'Action - Loader ',
1010
component: Component
1111
}
1212
export default meta
1313

1414
// Stories
15-
export const SavedLoader: StoryObj<typeof Component> = {
15+
export const ActionLoader: StoryObj<typeof Component> = {
1616
render: () => {
1717
return <Component />
1818
},

clients/extension/components/saved-loader/index.tsx clients/extension/components/action-loader/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import style from './style.module.css'
22

3-
export function SavedLoader() {
3+
export function ActionLoader() {
44
return (
55
<div className={style.container}>
66
<div className={style.loader}></div>
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import style from './style.module.css'
22

3+
import { ActionLoader } from '../action-loader'
34
import type { RefObject } from 'react'
45
import type { Dispatch, SetStateAction } from 'react'
56

67
export function NotesAdd({
78
textRef,
9+
noteStatus,
810
setErrorText
911
}: {
1012
textRef?: RefObject<HTMLTextAreaElement | null>
13+
noteStatus?: string
1114
setErrorText: Dispatch<SetStateAction<string | undefined>>
1215
}) {
1316
const resetError = () => setErrorText(undefined)
17+
1418
return (
1519
<div className={style.container}>
16-
<textarea onFocus={resetError} name="note" id="note" ref={textRef}></textarea>
20+
{noteStatus ? (
21+
<ActionLoader />
22+
) : (
23+
<textarea onFocus={resetError} name="note" id="note" ref={textRef}></textarea>
24+
)}
1725
</div>
1826
)
1927
}

clients/extension/components/notes-footer/index.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ import type { Dispatch, SetStateAction } from 'react'
77

88
export function NotesFooter({
99
setShowNotes,
10+
noteStatus,
1011
handleAddNote = () => {}
1112
}: {
1213
setShowNotes: Dispatch<SetStateAction<boolean>>
14+
noteStatus: string | undefined
1315
handleAddNote: () => void
1416
}) {
1517
const handleBackClick = () => setShowNotes(false)
1618
const handleAddClick = () => handleAddNote()
19+
const isLoading = Boolean(noteStatus)
20+
1721
return (
1822
<footer className={style.footer}>
1923
<button onClick={handleBackClick}>
2024
<ChevronLeftIcon className={style.backIcon} /> Back
2125
</button>
22-
<button onClick={handleAddClick}>
26+
<button onClick={handleAddClick} disabled={isLoading}>
2327
<AddNoteIcon /> Add Note
2428
</button>
2529
</footer>

clients/extension/components/notes/index.tsx

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ import type { Dispatch, SetStateAction } from 'react'
1212

1313
export function Notes({
1414
notes,
15+
noteStatus,
16+
setNoteStatus,
1517
setShowNotes,
1618
setErrorText
1719
}: {
1820
notes?: NoteEdge[]
21+
noteStatus?: string | undefined
22+
setNoteStatus: Dispatch<SetStateAction<string | undefined>>
1923
setErrorText: Dispatch<SetStateAction<string | undefined>>
2024
setShowNotes: Dispatch<SetStateAction<boolean>>
2125
}) {
2226
const textRef = useRef<HTMLTextAreaElement | null>(null)
27+
2328
const handleAddNote = () => {
2429
// Get the value of our textarea
2530
if (!textRef.current) return
@@ -31,17 +36,22 @@ export function Notes({
3136
return setErrorText('Text is require to create a note')
3237
}
3338

34-
void sendMessage({ action: EXT_ACTIONS.ADD_NOTE_REQUEST, noteData: { docMarkdown } })
3539
textRef.current.value = ''
40+
setNoteStatus('saving note')
41+
void sendMessage({ action: EXT_ACTIONS.ADD_NOTE_REQUEST, noteData: { docMarkdown } })
3642
}
3743

3844
return (
3945
<div>
4046
<div className={style.container}>
4147
<NotesList notes={notes} />
42-
<NotesAdd textRef={textRef} setErrorText={setErrorText} />
48+
<NotesAdd noteStatus={noteStatus} textRef={textRef} setErrorText={setErrorText} />
4349
</div>
44-
<NotesFooter setShowNotes={setShowNotes} handleAddNote={handleAddNote} />
50+
<NotesFooter
51+
noteStatus={noteStatus}
52+
setShowNotes={setShowNotes}
53+
handleAddNote={handleAddNote}
54+
/>
4555
</div>
4656
)
4757
}

0 commit comments

Comments
 (0)