Skip to content

Commit 4e7d3fe

Browse files
committed
add null checking to tsconfig
The reason this wasn't the case already is because Plasmo's apis and the Notion SDK don't support it yet However, the appreance of bugs that could've been prevented had it been enabled made me change my mind here
1 parent e082902 commit 4e7d3fe

File tree

13 files changed

+66
-46
lines changed

13 files changed

+66
-46
lines changed

src/api/saveAnswer.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const saveAnswer = async ({
4444
return response
4545
}
4646

47+
// @ts-ignore
4748
const response = await notion.pages.create({
4849
parent: {
4950
database_id: database.id

src/api/saveChat.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const saveChat = async ({
1313
try {
1414
const notion = await getNotion()
1515
const { propertiesIds, tags, tagIndex, tagPropertyIndex } = database
16-
const blocks = []
16+
const blocks: any[] = []
1717
for (let i = 0; i < prompts.length; i++) {
1818
const { answerBlocks, promptBlocks } = generateBlocks(
1919
prompts[i],
@@ -43,6 +43,7 @@ export const saveChat = async ({
4343
})
4444
}
4545

46+
// @ts-ignore
4647
const response = await notion.pages.create({
4748
parent: {
4849
database_id: database.id

src/background/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const refreshIcons = async () => {
9393
if (new Date(expiryTime).getTime() < Date.now()) {
9494
console.log("refreshing icon for", databases[i].title)
9595
const db = await getDatabase(databases[i].id)
96+
if (!db) continue
9697
databases[i].icon = db.icon
9798
await storage.set("databases", databases)
9899
}

src/contents/fetchFullPage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
1313
const rawAnswers = chat.filter((el, index) => index % 2 === 1)
1414

1515
const prompts = rawPrompts.map(
16-
(el) => el.querySelector(".whitespace-pre-wrap").textContent
16+
(el) => el.querySelector(".whitespace-pre-wrap")?.textContent
1717
)
1818
const answers = rawAnswers.map(
1919
(el) =>
2020
(
2121
el.querySelector(".markdown") ??
2222
el.querySelector(".dark.text-orange-500")
23-
).innerHTML
23+
)?.innerHTML
2424
)
2525

2626
const url = window.location.href

src/contents/pin.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ export const render: PlasmoRender = async ({
2525
anchor, // the observed anchor, OR document.body.
2626
createRootContainer // This creates the default root container
2727
}) => {
28+
// @ts-ignore
2829
const rootContainer = await createRootContainer(anchor)
2930

3031
const root = createRoot(rootContainer) // Any root
3132
root.render(
3233
<>
3334
<Content
35+
// @ts-ignore
3436
parent={anchor.element.parentElement.parentElement.parentElement}
3537
/>
3638
</>
@@ -48,12 +50,14 @@ const Content = ({ parent }: Props) => {
4850
return
4951
}
5052
const answer = await compress(
53+
// @ts-ignore
5154
(
5255
parent.querySelector(".markdown") ??
5356
parent.querySelector(".dark.text-orange-500")
5457
).innerHTML
5558
)
5659
const prompt = await compress(
60+
// @ts-ignore
5761
parent.previousElementSibling.querySelector(".whitespace-pre-wrap")
5862
.textContent
5963
)

src/hooks/useTags.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ const useTags = () => {
1212
[]
1313
)
1414

15-
const [db, setCurrentDB] = useState<StoredDatabase>(null)
15+
const [db, setCurrentDB] = useState<StoredDatabase | null>(null)
1616
const [tagProp, setCurrentTagProp] = useState<{
1717
options: SelectPropertyResponse[]
1818
name: string
1919
id: string
2020
type: "select" | "multi_select"
21-
}>(null)
22-
const [tag, setCurrentTag] = useState<SelectPropertyResponse>(null)
21+
} | null>(null)
22+
const [tag, setCurrentTag] = useState<SelectPropertyResponse | null>(null)
2323

2424
useEffect(() => {
2525
if (!databases || databases.length == 0) return
@@ -39,15 +39,17 @@ const useTags = () => {
3939

4040
const selectTagProp = (index: number) => {
4141
setDatabases((prev) => {
42+
if (!prev) return []
4243
const newDbs = [...prev]
4344
newDbs[selectedDB].tagPropertyIndex = index
44-
newDbs[selectedDB].tagIndex = 0
45+
newDbs[selectedDB].tagIndex = -1
4546
return newDbs
4647
})
4748
}
4849

4950
const selectTag = (index: number) => {
5051
setDatabases((prev) => {
52+
if (!prev) return []
5153
const newDatabases = [...prev]
5254
newDatabases[selectedDB].tagIndex = index
5355
return newDatabases

src/popup/DatabaseSettings.tsx

+14-9
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ function DatabaseSettingsPopup() {
1919
[]
2020
)
2121
const [selectedDB, setSelectedDB] = useStorage<number>("selectedDB", 0)
22-
const [db, setCurrentDB] = useState<StoredDatabase>(null)
22+
const [db, setCurrentDB] = useState<StoredDatabase | null>(null)
2323
const [currentProp, setCurrentProp] = useState<{
2424
options: SelectPropertyResponse[]
2525
name: string
2626
id: string
2727
type: "select" | "multi_select"
28-
}>(null)
28+
} | null>(null)
2929

3030
const [refreshing, setRefreshing] = useState(false)
3131

@@ -46,19 +46,24 @@ function DatabaseSettingsPopup() {
4646
id: databases[selectedDB].id
4747
}
4848
})
49-
await setDatabases((prev) => [
50-
...prev.slice(0, selectedDB),
51-
formatDB(db),
52-
...prev.slice(selectedDB + 1)
53-
])
49+
await setDatabases((prev) =>
50+
prev
51+
? [
52+
...prev.slice(0, selectedDB),
53+
formatDB(db)!,
54+
...prev.slice(selectedDB + 1)
55+
]
56+
: [formatDB(db)!]
57+
)
5458
setRefreshing(false)
5559
}
5660

5761
const selectTagProp = (index: number) => {
5862
setDatabases((prev) => {
63+
if (!prev) return []
5964
const newDbs = [...prev]
6065
newDbs[selectedDB].tagPropertyIndex = index
61-
newDbs[selectedDB].tagIndex = 0
66+
newDbs[selectedDB].tagIndex = -1
6267
return newDbs
6368
})
6469
}
@@ -112,7 +117,7 @@ function DatabaseSettingsPopup() {
112117
{tag.name}
113118
</button>
114119
))}>
115-
{currentProp.name}
120+
{currentProp?.name}
116121
</DropdownPopup>
117122
<h3 className="font-semibold my-2">{i18n("dbsettings_tags")}</h3>
118123
{currentProp && (

src/popup/IndexPopup.tsx

+18-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useState } from "react"
88

99
import { saveChat } from "~api/saveChat"
1010
import DropdownPopup from "~common/components/Dropdown"
11+
import NoTagButton from "~common/components/NoTagButton"
1112
import Spinner from "~common/components/Spinner"
1213
import useTags from "~hooks/useTags"
1314
import { i18n } from "~utils/functions"
@@ -29,6 +30,10 @@ function IndexPopup() {
2930
currentWindow: true
3031
})
3132
const currentTab = tabs[0]
33+
if (!currentTab.id) {
34+
setLoading(false)
35+
return
36+
}
3237
const chat = await chrome.tabs.sendMessage(currentTab.id, {
3338
type: "fetchFullChat"
3439
})
@@ -70,26 +75,25 @@ function IndexPopup() {
7075
{tag.name}
7176
</button>
7277
))}>
73-
{tagProp.name}
78+
{tagProp?.name}
7479
</DropdownPopup>
7580
<DropdownPopup
7681
className={`px-2 py-0.5 border border-main rounded ${
7782
tag === null ? "italic font-bold" : ""
7883
}`}
7984
position="up"
80-
items={[
81-
...tagProp.options.map((tag, index) => (
82-
<button key={tag.id} onClick={() => selectTag(index)}>
83-
{tag.name}
84-
</button>
85-
)),
86-
<button
87-
key={"notag"}
88-
className="italic font-bold"
89-
onClick={() => selectTag(-1)}>
90-
{i18n("save_noTag")}
91-
</button>
92-
]}>
85+
items={
86+
tagProp
87+
? [
88+
...tagProp.options.map((tag, index) => (
89+
<button key={tag.id} onClick={() => selectTag(index)}>
90+
{tag.name}
91+
</button>
92+
)),
93+
<NoTagButton selectTag={selectTag} />
94+
]
95+
: [<NoTagButton selectTag={selectTag} />]
96+
}>
9397
{tag === null ? i18n("save_noTag") : tag?.name}
9498
</DropdownPopup>
9599
</div>

src/popup/SavePopup.tsx

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { StoredDatabase, ToBeSaved } from "~utils/types"
88
import "~styles.css"
99

1010
import DropdownPopup from "~common/components/Dropdown"
11+
import NoTagButton from "~common/components/NoTagButton"
1112
import Spinner from "~common/components/Spinner"
1213
import useTags from "~hooks/useTags"
1314
import { i18n } from "~utils/functions"
@@ -104,19 +105,18 @@ export default function SavePopup() {
104105
tag === null ? "italic font-bold" : ""
105106
}`}
106107
position="up"
107-
items={[
108-
...tagProp?.options.map((tag, index) => (
109-
<button key={tag.id} onClick={() => selectTag(index)}>
110-
{tag.name}
111-
</button>
112-
)),
113-
<button
114-
key={"notag"}
115-
className="font-bold"
116-
onClick={() => selectTag(-1)}>
117-
{i18n("save_noTag")}
118-
</button>
119-
]}>
108+
items={
109+
tagProp
110+
? [
111+
...tagProp.options.map((tag, index) => (
112+
<button key={tag.id} onClick={() => selectTag(index)}>
113+
{tag.name}
114+
</button>
115+
)),
116+
<NoTagButton selectTag={selectTag} />
117+
]
118+
: [<NoTagButton selectTag={selectTag} />]
119+
}>
120120
{tag === null ? i18n("save_noTag") : tag?.name}
121121
</DropdownPopup>
122122
</div>
@@ -126,7 +126,7 @@ export default function SavePopup() {
126126
<button
127127
disabled={loading || success || !authenticated}
128128
className="button w-full disabled:bg-main"
129-
onClick={() => save(db)}>
129+
onClick={() => save(db!)}>
130130
{!authenticated && (
131131
<>
132132
<span>{i18n("authenticating")}</span>

src/popup/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function Wrapper() {
4242
active: true,
4343
currentWindow: true
4444
})
45-
if (!tabs[0].url.match(/^(https:\/\/chat.openai.com).*/)) {
45+
if (!tabs[0].url?.match(/^(https:\/\/chat.openai.com).*/)) {
4646
if (!popup || popup === "index" || popup === "save")
4747
await setPopup("wrongpage")
4848
} else if (popup === "wrongpage") {

src/utils/functions/notion.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export const formatDB = (db: DatabaseObjectResponse): StoredDatabase | null => {
209209
const formattedDB = {
210210
id: db.id,
211211
title: db.title[0].plain_text,
212-
icon: db.icon ?? null,
212+
icon: db.icon,
213213
propertiesIds: {
214214
title: titleID,
215215
url: urlID

src/utils/types/notion.ts

+1
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ export type IconResponse =
131131
expiry_time: string
132132
}
133133
}
134+
| null

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"./**/*.tsx"
1010
],
1111
"compilerOptions": {
12+
"strictNullChecks": true,
1213
"paths": {
1314
"~*": [
1415
"./src/*"

0 commit comments

Comments
 (0)