Skip to content

Commit

Permalink
feat: add cards
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaHuhn committed Jun 23, 2023
1 parent 103b920 commit 63cf695
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 33 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"compression": "^1.7.4",
"cors": "^2.8.5",
"deta": "^1.0.1",
"deta-space-actions": "^0.1.4",
"deta-space-actions": "^0.1.8",
"dotenv": "^10.0.0",
"ejs": "^3.1.6",
"express": "^4.18.2",
Expand Down
94 changes: 73 additions & 21 deletions server/router/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createActions, Inputs } from 'deta-space-actions'
import { CardType, createActions, DetailCard, Inputs, ListCard } from 'deta-space-actions'
import { createLink } from '../service/link.js'
import { Link } from '../models/link.js'
import { domain } from '../utils/variables.js'
Expand All @@ -13,10 +13,17 @@ actions.add<{ title?: string, url: string }>({
Inputs('url').String(),
Inputs('title').String().Optional()
],
handler: async (event) => {
card: CardType.DETAIL,
handler: async (event): Promise<DetailCard> => {
const { url, title } = event
const link = await createLink(url, title)
return `https://${ domain }/link/${ link.id }`
return {
title: link?.meta?.title,
text: link?.meta?.description || link.url,
image_url: link?.meta?.image,
url: link.url,
ref: `https://${ domain }/link/${ link.id }`
}
}
})

Expand All @@ -26,39 +33,84 @@ actions.add<{ crate?: string }>({
input: [
Inputs('crate').String().Optional()
],
handler: async (event) => {
card: CardType.LIST,
handler: async (event): Promise<ListCard> => {
const { crate: crateName } = event
if (!crateName) {
const links = await Link.find({}, 20, undefined)
return links.items.map(link => ({
title: link?.meta?.title,
url: link.url
}))
return {
title: `Recent Links`,
items: links.items.map(link => ({
title: link?.meta?.title || link.url,
description: link?.meta?.description,
url: link.url,
card: {
type: CardType.DETAIL,
data: {
title: link?.meta?.title,
text: link?.meta?.description,
image_url: link?.meta?.image,
url: link.url,
ref: `https://${ domain }/link/${ link.id }`
}
}
})) as any
}
}
const crate = await Crate.findOne({ name: crateName })
if (!crate) return 'Crate not found'
if (!crate) return {
title: `Crate not found`,
items: []
}

const links = await Link.find({ crate: crate.id }, 20, undefined)
return links.items.map(link => ({
title: link?.meta?.title,
url: link.url,
crate: crate.name
}))

return {
title: crate.name,
description: crate.description,
items: links.items.map(link => ({
title: link?.meta?.title || link.url,
description: crate.name,
url: link.url,
card: {
type: CardType.DETAIL,
data: {
title: link?.meta?.title,
text: link?.meta?.description,
image_url: link?.meta?.image,
url: link.url,
ref: `https://${ domain }/link/${ link.id }`
}
}
})) as any
}
}
})

actions.add({
name: 'crates',
title: 'List Crates',
input: [],
handler: async () => {
card: CardType.LIST,
handler: async (): Promise<ListCard> => {
const crates = await Crate.find({}, 20, undefined)
return crates.items.map(crate => ({
name: crate.name,
description: crate.description,
icon: crate.icon,
public: crate.public
}))
return {
title: `Crates`,
items: crates.items.map(crate => ({
title: crate.name,
description: crate.description,
url: `https://${ domain }/crate/${ crate.name }`,
card: {
type: CardType.DETAIL,
data: {
title: crate.name,
text: crate.description,
url: `https://${ domain }/crate/${ crate.name }`,
ref: `https://${ domain }/crate/${ crate.name }`,
}
}
})) as any
}
}
})

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4998,10 +4998,10 @@
"resolved" "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz"
"version" "1.2.0"

"deta-space-actions@^0.1.4":
"integrity" "sha512-pHxozRTLO/WfjN+XpIfppVDrc8dYpnxRI0fZQDmEx8ZE752/4Vo6BVVNpEhf5sWQMFS49Sn51nYtKpjr+4Mbjw=="
"resolved" "https://registry.npmjs.org/deta-space-actions/-/deta-space-actions-0.1.4.tgz"
"version" "0.1.4"
"deta-space-actions@^0.1.8":
"integrity" "sha512-GZVFQ7Z+bAeUtkhDBNA1mmGWpJm4uIf4+M+EH+xaPs/Ic7xBCMMJbAYcnhmrtEZve4mDEHnmqFKnP4De5wayBg=="
"resolved" "https://registry.npmjs.org/deta-space-actions/-/deta-space-actions-0.1.8.tgz"
"version" "0.1.8"
dependencies:
"node-fetch" "^3.3.1"

Expand Down

0 comments on commit 63cf695

Please sign in to comment.