refactor: extract `getNoteSlug` logic into `lib/helpers`

This commit is contained in:
Runjuu 2023-01-19 10:48:52 -08:00
parent cbf2f3d1ee
commit a30eaef9ac
3 changed files with 20 additions and 24 deletions

View File

@ -1,3 +1,5 @@
import { NoteEntity } from "crossbell.js"
import { IS_PROD } from "./constants"
import { OUR_DOMAIN } from "./env"
@ -18,3 +20,13 @@ export const getSiteLink = ({
}
return `${IS_PROD ? "https" : "http"}://${subdomain}.${OUR_DOMAIN}`
}
export const getNoteSlug = (note: NoteEntity) => {
return (
note.metadata?.content?.attributes?.find(
(a) => a?.trait_type === "xlog_slug",
)?.value ||
(note.metadata?.content as any)?._xlog_slug ||
(note.metadata?.content as any)?._crosslog_slug
)?.toLowerCase?.()
}

View File

@ -14,19 +14,17 @@ import {
} from "@crossbell/connect-kit"
import { UrlComposer } from "@crossbell/ui"
import { NotificationModal } from "@crossbell/notification"
import { InitContractProvider } from "@crossbell/contract"
import { useRefCallback } from "@crossbell/util-hooks"
import NextNProgress from "nextjs-progressbar"
import { CharacterEntity, Network } from "crossbell.js"
import { Network } from "crossbell.js"
import { IPFS_GATEWAY, CSB_IO } from "~/lib/env"
import { toGateway } from "~/lib/ipfs-parser"
import { connectors, provider } from "~/lib/wallet-config"
import { createIDBPersister } from "~/lib/persister.client"
import { NotificationModal } from "@crossbell/notification"
import { getSiteLink } from "~/lib/helpers"
import type { NoteEntity } from "crossbell.js"
import { getNoteSlug, getSiteLink } from "~/lib/helpers"
Network.setIpfsGateway(IPFS_GATEWAY)
@ -75,13 +73,7 @@ const urlComposer: Partial<UrlComposer> = {
subdomain: character.handle,
}) +
"/" +
(
originalNote.metadata?.content?.attributes?.find(
(a: any) => a?.trait_type === "xlog_slug",
)?.value ||
(originalNote as any).metadata?.content?._xlog_slug ||
(originalNote as any).metadata?.content?._crosslog_slug
)?.toLowerCase?.() +
getNoteSlug(originalNote) +
(originalNote !== note ? `#comments` : "")
)
} else {

View File

@ -1,15 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next"
import { cacheGet, cacheDelete } from "~/lib/redis.server"
const getSlug = (note: any) => {
return (
note.metadata?.content?.attributes?.find(
(a: any) => a?.trait_type === "xlog_slug",
)?.value ||
note.metadata?.content?._xlog_slug ||
note.metadata?.content?._crosslog_slug
)?.toLowerCase?.()
}
import { cacheGet, cacheDelete } from "~/lib/redis.server"
import { getNoteSlug } from "~/lib/helpers"
export async function getIdBySlug(slug: string, handle: string) {
slug = (slug as string)?.toLowerCase?.()
@ -44,7 +36,7 @@ export async function getIdBySlug(slug: string, handle: string) {
cursor = response.cursor
note = response?.list?.find(
(item: any) =>
slug === getSlug(item) || slug === `${cid}-${item.noteId}`,
slug === getNoteSlug(item) || slug === `${cid}-${item.noteId}`,
)
} while (!note && cursor)
@ -67,7 +59,7 @@ export async function getIdBySlug(slug: string, handle: string) {
)
.then((res) => res.json())
.then((note) => {
if ((note && getSlug(note) !== slug) || note.deleted) {
if ((note && getNoteSlug(note) !== slug) || note.deleted) {
cacheDelete(["slug2id", handle, slug])
}
})