Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit e7b9881

Browse files
authored
smuggler-api/types.ts: split types & helpers (#390)
1 parent 119bca4 commit e7b9881

33 files changed

+259
-346
lines changed

archaeologist/src/background.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import { log, isAbortError, genOriginId, unixtime } from 'armoury'
2525
import {
2626
TNode,
27+
NodeUtil,
2728
TotalUserActivity,
2829
ResourceVisit,
2930
smuggler,
@@ -102,7 +103,7 @@ async function requestPageSavedStatus(url: string | undefined) {
102103
}
103104
let bookmark: TNode | undefined = undefined
104105
for (const node of nodes) {
105-
if (node.isWebBookmark()) {
106+
if (NodeUtil.isWebBookmark(node)) {
106107
bookmark = node
107108
break
108109
}
@@ -419,7 +420,7 @@ async function handleMessageFromContent(
419420
const suggested = await suggestAssociations(message.phrase, message.limit)
420421
return {
421422
type: 'SUGGESTED_CONTENT_ASSOCIATIONS',
422-
suggested: suggested.map((node: TNode) => node.toJson()),
423+
suggested: suggested.map((node: TNode) => NodeUtil.toJson(node)),
423424
}
424425
}
425426
default:
@@ -608,7 +609,11 @@ async function handleMessageFromPopup(
608609
content,
609610
tabId
610611
)
611-
return { type: 'PAGE_SAVED', bookmark: node?.toJson(), unmemorable }
612+
return {
613+
type: 'PAGE_SAVED',
614+
bookmark: node ? NodeUtil.toJson(node) : undefined,
615+
unmemorable,
616+
}
612617
case 'REQUEST_PAGE_IN_ACTIVE_TAB_STATUS': {
613618
const { bookmark, unmemorable, fromNodes, toNodes } =
614619
await requestPageSavedStatus(activeTab?.url)
@@ -619,9 +624,9 @@ async function handleMessageFromPopup(
619624
return {
620625
type: 'UPDATE_POPUP_CARDS',
621626
mode: 'reset',
622-
bookmark: bookmark?.toJson(),
623-
fromNodes: fromNodes?.map((node) => node.toJson()) ?? [],
624-
toNodes: toNodes?.map((node) => node.toJson()) ?? [],
627+
bookmark: bookmark ? NodeUtil.toJson(bookmark) : undefined,
628+
fromNodes: fromNodes?.map((node) => NodeUtil.toJson(node)) ?? [],
629+
toNodes: toNodes?.map((node) => NodeUtil.toJson(node)) ?? [],
625630
unmemorable,
626631
}
627632
}
@@ -785,9 +790,9 @@ async function initMazedPartsOfTab(
785790
type: 'INIT_CONTENT_AUGMENTATION_REQUEST',
786791
nodeEnv: process.env.NODE_ENV,
787792
userUid: auth.account().getUid(),
788-
bookmark: bookmark?.toJson(),
789-
fromNodes: fromNodes?.map((node) => node.toJson()) ?? [],
790-
toNodes: toNodes?.map((node) => node.toJson()) ?? [],
793+
bookmark: bookmark ? NodeUtil.toJson(bookmark) : undefined,
794+
fromNodes: fromNodes?.map((node) => NodeUtil.toJson(node)) ?? [],
795+
toNodes: toNodes?.map((node) => NodeUtil.toJson(node)) ?? [],
791796
mode,
792797
})
793798
} catch (err) {

archaeologist/src/background/savePage.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
NodeType,
1313
OriginHash,
1414
TNode,
15-
makeNodeTextData,
15+
NodeUtil,
16+
makeEmptyNodeTextData,
1617
smuggler,
1718
} from 'smuggler-api'
1819
import { ToContent } from '../message/types'
@@ -33,16 +34,16 @@ async function updateContent(
3334
bookmark?: TNode,
3435
tabId?: number
3536
): Promise<void> {
36-
const bookmarkJson = bookmark?.toJson()
37+
const bookmarkJson = bookmark ? NodeUtil.toJson(bookmark) : undefined
3738
// Update content augmentation
3839
if (tabId == null) {
3940
return
4041
}
4142
try {
4243
await ToContent.sendMessage(tabId, {
4344
type: 'REQUEST_UPDATE_CONTENT_AUGMENTATION',
44-
fromNodes: fromNodes.map((node) => node.toJson()),
45-
toNodes: toNodes.map((node) => node.toJson()),
45+
fromNodes: fromNodes.map((node) => NodeUtil.toJson(node)),
46+
toNodes: toNodes.map((node) => NodeUtil.toJson(node)),
4647
bookmark: bookmarkJson,
4748
mode: 'append',
4849
})
@@ -136,7 +137,7 @@ export async function saveWebPage(
136137
await updateContent([], [], undefined, tabId)
137138
return { unmemorable: true }
138139
}
139-
const text = makeNodeTextData()
140+
const text = makeEmptyNodeTextData()
140141
const index_text: NodeIndexText = {
141142
plaintext: content.text || undefined,
142143
labels: [],
@@ -222,7 +223,7 @@ export async function savePageQuote(
222223
web_quote: { url, path, text },
223224
}
224225
const resp = await smuggler.node.create({
225-
text: makeNodeTextData(),
226+
text: makeEmptyNodeTextData(),
226227
ntype: NodeType.WebQuote,
227228
from_nid: fromNid ? [fromNid] : undefined,
228229
extattrs,
@@ -248,7 +249,7 @@ async function _saveWebSearchQuery(
248249
if (originId == null) {
249250
originId = genOriginId(url).id
250251
}
251-
const text = makeNodeTextData()
252+
const text = makeEmptyNodeTextData()
252253
const index_text: NodeIndexText = {
253254
labels: [],
254255
brands: [],

archaeologist/src/background/suggestAssociations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { smuggler, TNode } from 'smuggler-api'
1+
import { smuggler } from 'smuggler-api'
2+
import type { TNode } from 'smuggler-api'
23
import { Beagle } from 'elementary'
34

45
export async function suggestAssociations(

archaeologist/src/content/App.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import browser from 'webextension-polyfill'
55
import { PostHog } from 'posthog-js'
66
import { v4 as uuidv4 } from 'uuid'
77

8-
import { TNode, TNodeJson, NodeType } from 'smuggler-api'
8+
import { NodeUtil, NodeType } from 'smuggler-api'
9+
import type { TNode, TNodeJson } from 'smuggler-api'
910
import { genOriginId, OriginIdentity, log, productanalytics } from 'armoury'
1011
import * as truthsayer_archaeologist_communication from 'truthsayer-archaeologist-communication'
1112

@@ -152,10 +153,12 @@ function updateState(state: State, action: Action): State {
152153
fromNodes: fromNodesJson,
153154
} = action.data
154155
const bookmark =
155-
bookmarkJson != null ? TNode.fromJson(bookmarkJson) : undefined
156-
const toNodes = toNodesJson.map((json: TNodeJson) => TNode.fromJson(json))
156+
bookmarkJson != null ? NodeUtil.fromJson(bookmarkJson) : undefined
157+
const toNodes = toNodesJson.map((json: TNodeJson) =>
158+
NodeUtil.fromJson(json)
159+
)
157160
const fromNodes = fromNodesJson.map((json: TNodeJson) =>
158-
TNode.fromJson(json)
161+
NodeUtil.fromJson(json)
159162
)
160163

161164
let analytics: PostHog | null = null
@@ -225,13 +228,13 @@ function updateState(state: State, action: Action): State {
225228
}
226229
const d = action.data
227230
const newToNodes = d.toNodes.map((json: TNodeJson) =>
228-
TNode.fromJson(json)
231+
NodeUtil.fromJson(json)
229232
)
230233
const newFromNodes = d.fromNodes.map((json: TNodeJson) =>
231-
TNode.fromJson(json)
234+
NodeUtil.fromJson(json)
232235
)
233236
const newBookmark =
234-
d.bookmark != null ? TNode.fromJson(d.bookmark) : undefined
237+
d.bookmark != null ? NodeUtil.fromJson(d.bookmark) : undefined
235238

236239
return {
237240
...state,

archaeologist/src/content/augmentation/SuggestionsToast.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import styled from '@emotion/styled'
55
import { mazed } from '../../util/mazed'
66

77
import { TDoc, ShrinkMinimalCard, NodeCardReadOnly } from 'elementary'
8-
import { TNode } from 'smuggler-api'
8+
import { NodeUtil, smuggler } from 'smuggler-api'
9+
import type { TNode } from 'smuggler-api'
910

1011
import { Toast, useOutsideToastClickHandler } from './../toaster/Toaster'
1112
import { LogoSmall, RefItem } from './../style'
@@ -91,21 +92,21 @@ const CopySuggestionButton = ({
9192

9293
function getTextToInsert(node: TNode): string {
9394
let toInsert: string
94-
if (node.isWebBookmark() && node.extattrs?.web != null) {
95+
if (NodeUtil.isWebBookmark(node) && node.extattrs?.web != null) {
9596
const { web, title, author } = node.extattrs
9697
const authorStr = author ? `\nby ${author}` : ''
9798
toInsert = `${title}${authorStr}\n🧵 ${web.url} `
98-
} else if (node.isWebQuote() && node.extattrs?.web_quote != null) {
99+
} else if (NodeUtil.isWebQuote(node) && node.extattrs?.web_quote != null) {
99100
const { text, url } = node.extattrs.web_quote
100101
const { author } = node.extattrs
101102
const authorStr = author ? `\nby ${author}` : ''
102103
toInsert = `“${text}${authorStr}\n🧵 ${url} `
103-
} else if (node.isImage()) {
104-
const url = node.getBlobSource()
104+
} else if (NodeUtil.isImage(node)) {
105+
const url = smuggler.blob.sourceUrl(node.nid)
105106
toInsert = ` 🧵 ${url} `
106107
} else {
107-
const doc = TDoc.fromNodeTextData(node.getText())
108-
toInsert = `${doc.genPlainText()}\n🧵 ${node.getDirectUrl()} `
108+
const doc = TDoc.fromNodeTextData(node.text)
109+
toInsert = `${doc.genPlainText()}\n🧵 ${smuggler.node.url(node.nid)} `
109110
}
110111
return toInsert
111112
}
@@ -118,11 +119,11 @@ function CardInsertButton({
118119
onClose: () => void
119120
}) {
120121
let copySubj: string
121-
if (node.isWebBookmark() && node.extattrs?.web != null) {
122+
if (NodeUtil.isWebBookmark(node) && node.extattrs?.web != null) {
122123
copySubj = 'Link'
123-
} else if (node.isWebQuote() && node.extattrs?.web_quote != null) {
124+
} else if (NodeUtil.isWebQuote(node) && node.extattrs?.web_quote != null) {
124125
copySubj = 'Quote'
125-
} else if (node.isImage()) {
126+
} else if (NodeUtil.isImage(node)) {
126127
copySubj = 'Image'
127128
} else {
128129
copySubj = 'Note'
@@ -156,7 +157,7 @@ const SuggestedCard = ({
156157
<SuggestedCardTools>
157158
<CardInsertButton node={node} onClose={onClose} />
158159
<SuggestionButton
159-
href={node.getDirectUrl()}
160+
href={smuggler.node.url(node.nid)}
160161
metricLabel={'Suggested Fragment Open in Mazed'}
161162
>
162163
Open Mazed
@@ -182,7 +183,7 @@ export const SuggestionsToast = ({
182183
onClose: () => void
183184
}) => {
184185
const suggestedEl = suggested.map((node: TNode) => {
185-
return <SuggestedCard key={node.getNid()} node={node} onClose={onClose} />
186+
return <SuggestedCard key={node.nid} node={node} onClose={onClose} />
186187
})
187188
useOutsideToastClickHandler(onClose)
188189
return (

archaeologist/src/content/augmentation/WriteAugmentation.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React from 'react'
22
import lodash from 'lodash'
33

44
import { log } from 'armoury'
5-
import { TNode, TNodeJson } from 'smuggler-api'
5+
import { NodeUtil } from 'smuggler-api'
6+
import type { TNode, TNodeJson } from 'smuggler-api'
67
import { Spinner } from 'elementary'
78

89
import { FromContent } from './../../message/types'
@@ -87,7 +88,9 @@ export function WriteAugmentation() {
8788
phrase,
8889
})
8990
setSuggestedNodes(
90-
response.suggested.map((value: TNodeJson) => TNode.fromJson(value))
91+
response.suggested.map((value: TNodeJson) =>
92+
NodeUtil.fromJson(value)
93+
)
9194
)
9295
setSuggestionsSearchIsActive(false)
9396
},

archaeologist/src/content/quote/Quotes.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
2-
import { TNode } from 'smuggler-api'
2+
import { NodeUtil } from 'smuggler-api'
3+
import type { TNode } from 'smuggler-api'
34

45
import { QuoteHighlight } from './QuoteHighlight'
56

@@ -11,7 +12,7 @@ export const Quotes = ({ quotes }: { quotes: TNode[] }) => {
1112
return null
1213
}
1314
const { path, text } = web_quote
14-
if (!node.isWebQuote() || path == null) {
15+
if (!NodeUtil.isWebQuote(node) || path == null) {
1516
return null
1617
}
1718
return (

archaeologist/src/message/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { WebPageContent } from './../content/extractor/webPageContent'
22

33
import browser from 'webextension-polyfill'
4-
import { OriginHash, TNodeJson } from 'smuggler-api'
4+
import type { OriginHash, TNodeJson } from 'smuggler-api'
55
import { OriginIdentity } from 'armoury'
66

77
/**

archaeologist/src/omnibox/omnibox.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { smuggler, TNode } from 'smuggler-api'
1+
import { smuggler, NodeUtil } from 'smuggler-api'
2+
import type { TNode } from 'smuggler-api'
23
import { Beagle, TDoc } from 'elementary'
34
import { log, unicodeText } from 'armoury'
45
import { mazed } from '../util/mazed'
@@ -12,7 +13,7 @@ function nodeToSuggestion(node: TNode): browser.Omnibox.SuggestResult {
1213
const { nid } = node
1314
const url = node.extattrs?.web?.url || node.extattrs?.web_quote?.url
1415
if (url != null) {
15-
if (node.isWebQuote()) {
16+
if (NodeUtil.isWebQuote(node)) {
1617
const title = _truncate(
1718
node.extattrs?.web_quote?.text || '',
1819
kTitleLengthMax
@@ -23,7 +24,7 @@ function nodeToSuggestion(node: TNode): browser.Omnibox.SuggestResult {
2324
description: formatDescription(title, shortUrl),
2425
}
2526
}
26-
if (node.isWebBookmark()) {
27+
if (NodeUtil.isWebBookmark(node)) {
2728
const title = _truncate(
2829
node.extattrs?.title ?? node.extattrs?.description ?? '',
2930
kTitleLengthMax
@@ -35,7 +36,7 @@ function nodeToSuggestion(node: TNode): browser.Omnibox.SuggestResult {
3536
}
3637
}
3738
}
38-
const doc = TDoc.fromNodeTextData(node.getText())
39+
const doc = TDoc.fromNodeTextData(node.text)
3940
const title = doc.genTitle(kTitleLengthMax)
4041
return {
4142
content: mazed.makeNodeUrl(nid).toString(),

archaeologist/src/popup/NodeCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
NodeCardReadOnly,
1111
} from 'elementary'
1212

13-
import { TNode } from 'smuggler-api'
13+
import type { TNode } from 'smuggler-api'
1414

1515
const Box = styled(SmallCard)`
1616
width: 100%;

0 commit comments

Comments
 (0)