feat: support pinning an article to the top (#720)

This commit is contained in:
runjuu
2023-07-04 20:31:52 +01:00
committed by GitHub
parent a102083a04
commit 5cc6bbe0ea
14 changed files with 214 additions and 434 deletions
+11 -1
View File
@@ -20,7 +20,7 @@ import { readFiles } from "~/lib/read-files"
import { setStorage } from "~/lib/storage"
import { ExpandedNote, PageVisibilityEnum } from "~/lib/types"
import { cn } from "~/lib/utils"
import { useGetPagesBySite } from "~/queries/page"
import { useGetPagesBySite, usePinnedPage } from "~/queries/page"
import { useGetSite } from "~/queries/site"
import { Button } from "../ui/Button"
@@ -39,6 +39,7 @@ export const PagesManager = ({ isPost }: { isPost: boolean }) => {
const searchParams = useSearchParams()!
const router = useRouter()
const pathname = usePathname()
const pinnedPage = usePinnedPage({ characterId: site.data?.characterId })
const visibility = useMemo<PageVisibilityEnum>(
() =>
@@ -314,6 +315,15 @@ export const PagesManager = ({ isPost }: { isPost: boolean }) => {
page.metadata?.content?.date_published || "",
)}
</span>
{pinnedPage.noteId === page.noteId && (
<>
<span className="mx-2">·</span>
<span>
<i className="icon-[mingcute--pin-2-fill] translate-y-[18%]" />{" "}
{siteT("Pinned")}
</span>
</>
)}
</div>
</div>
<div className="w-10 flex-shrink-0 flex self-center ml-auto">
@@ -10,7 +10,7 @@ import { getNoteSlugFromNote, getTwitterShareUrl } from "~/lib/helpers"
import { useTranslation } from "~/lib/i18n/client"
import { delStorage, getStorage, setStorage } from "~/lib/storage"
import { ExpandedNote } from "~/lib/types"
import { useDeletePage, useUpdatePage } from "~/queries/page"
import { useDeletePage, usePinPage, useUpdatePage } from "~/queries/page"
import { useGetSite } from "~/queries/site"
import { DeleteConfirmationModal } from "./DeleteConfirmationModal"
@@ -48,6 +48,7 @@ export const PagesManagerMenu = ({
const editLink = usePageEditLink(page, isPost)
const queryClient = useQueryClient()
const deletePage = useDeletePage()
const pinPage = usePinPage(page)
const [convertToastId, setConvertToastId] = useState("")
const [deleteToastId, setDeleteToastId] = useState("")
@@ -98,6 +99,11 @@ export const PagesManagerMenu = ({
router.push(editLink)
},
},
{
text: pinPage.isPinned ? "Unpin" : "Pin",
icon: <span className="icon-[mingcute--pin-2-line] inline-block"></span>,
onClick: pinPage.togglePin,
},
{
text: "Convert to " + (isPost ? "Page" : "Post"),
icon: (
+12
View File
@@ -19,6 +19,7 @@ import { ExpandedNote } from "~/lib/types"
export default function PostList({
posts,
pinnedNoteId,
keyword,
}: {
posts: UseInfiniteQueryResult<
@@ -29,6 +30,7 @@ export default function PostList({
},
unknown
>
pinnedNoteId?: number | null
keyword?: string
}) {
const { t } = useTranslation("site")
@@ -48,6 +50,7 @@ export default function PostList({
return (
<PostItem
post={post}
isPinned={post.noteId === pinnedNoteId}
keyword={keyword}
key={post.noteId}
priority={listIndex === 0 && postIndex < 3}
@@ -86,13 +89,16 @@ export default function PostList({
const PostItem = memo(
({
post,
isPinned,
keyword,
priority,
}: {
post: ExpandedNote
isPinned?: boolean
keyword?: string
priority?: boolean
}) => {
const { t } = useTranslation("site")
const isMounted = useIsClient()
const date = useDate()
const router = useRouter()
@@ -113,6 +119,12 @@ const PostItem = memo(
)}
suppressHydrationWarning
>
{isPinned && (
<span className="absolute top-2 right-5 text-xs text-zinc-400 flex items-center gap-1">
<i className="icon-[mingcute--pin-2-fill]" />
{t("Pinned")}
</span>
)}
<PostCover
cover={post.metadata?.content?.cover}
priority={priority}
+3 -2
View File
@@ -2,7 +2,7 @@
import PostList from "~/components/site/PostList"
import { PageVisibilityEnum } from "~/lib/types"
import { useGetPagesBySiteLite } from "~/queries/page"
import { useGetPagesBySiteLite, usePinnedPage } from "~/queries/page"
import { useGetSite } from "~/queries/site"
export default function SiteHome({ handle }: { handle: string }) {
@@ -13,12 +13,13 @@ export default function SiteHome({ handle }: { handle: string }) {
visibility: PageVisibilityEnum.Published,
useStat: true,
})
const pinnedPage = usePinnedPage({ characterId: site.data?.characterId })
if (!posts.data?.pages?.length) return null
return (
<>
<PostList posts={posts} />
<PostList posts={posts} pinnedNoteId={pinnedPage.noteId} />
</>
)
}
+3 -1
View File
@@ -124,5 +124,7 @@
"delete_confirmation_post": "この記事を削除してもよろしいですか?",
"delete_confirmation_page": "このページを削除してもよろしいですか?",
"Advanced Settings": "詳細設定",
"Disable AI-generated summary": "AI が生成した要約を無効化する"
"Disable AI-generated summary": "AI が生成した要約を無効化する",
"Pin": "固定する",
"Unpin": "固定を解除する"
}
+2 -1
View File
@@ -10,5 +10,6 @@
"Pull down to open the app": "ダウンロード済みですか?アプリを開くために引き下げてく",
"Write a comment on the blockchain": "ブロックチェーンでコメントする",
"Open in App": "アプリで開く",
"powered by": "<span>このサイトは </span><name/><span> によって作動します</span>"
"powered by": "<span>このサイトは </span><name/><span> によって作動します</span>",
"Pinned": "固定された"
}
+3 -1
View File
@@ -202,5 +202,7 @@
"Align Right": "靠右",
"Formula": "公式",
"Advanced Settings": "高級設置",
"Disable AI-generated summary": "禁用 AI 生成的摘要"
"Disable AI-generated summary": "禁用 AI 生成的摘要",
"Pin": "釘選",
"Unpin": "取消釘選"
}
+3 -2
View File
@@ -22,5 +22,6 @@
"Back to Home": "回到首頁",
"All Posts": "所有文章",
"Open in App": "在 App 中開啟",
"Search Posts": "搜尋文章"
}
"Search Posts": "搜尋文章",
"Pinned": "釘選"
}
+5 -3
View File
@@ -205,13 +205,15 @@
"Comments": "评论",
"You can subscribe to comments through an RSS reader to receive timely reminders.": "你可以通过 RSS 阅读器订阅评论,以便及时收到提醒。",
"Subscription address:": "订阅地址:",
"comment on your" : "评论了你的{{type}}「<tolink>{{toTitle}}</tolink>」",
"{{newCount}} new comments" : "{{newCount}} 条新评论",
"comment on your": "评论了你的{{type}}「<tolink>{{toTitle}}</tolink>」",
"{{newCount}} new comments": "{{newCount}} 条新评论",
"Upload All Images to IPFS": "上传所有图片到 IPFS",
"Help: Markdown syntax and components used by xLog": "帮助:xLog 使用的 Markdown 语法和组件",
"Align Center": "居中对齐",
"Align Right": "右对齐",
"Formula": "公式",
"Advanced Settings": "高级设置",
"Disable AI-generated summary": "禁用 AI 生成的摘要"
"Disable AI-generated summary": "禁用 AI 生成的摘要",
"Pin": "置顶",
"Unpin": "取消置顶"
}
+4 -4
View File
@@ -7,8 +7,7 @@
"signed and stored on the blockchain": "此{{name}}数据所有权由区块链加密技术和智能合约保障仅归创作者所有。",
"Write a comment on the blockchain": "在区块链上写下你的评论",
"powered by": "<span>由 </span><name/><span> 提供支持</span>",
"This address is in local editing preview mode and cannot be viewed by the public.":
"此地址处于本地编辑预览模式,无法被公众查看。",
"This address is in local editing preview mode and cannot be viewed by the public.": "此地址处于本地编辑预览模式,无法被公众查看。",
"View on xChar": "在 xChar 上查看",
"View on xFeed": "在 xFeed 上查看",
"View on Hoot It": "在 Hoot It 上查看",
@@ -23,5 +22,6 @@
"Back to Home": "返回主页",
"All Posts": "所有文章",
"Open in App": "在 App 中打开",
"Search Posts": "搜索文章"
}
"Search Posts": "搜索文章",
"Pinned": "置顶"
}
+67 -31
View File
@@ -10,6 +10,7 @@ import type { Address } from "viem"
import { GeneralAccount } from "@crossbell/connect-kit"
import { indexer } from "@crossbell/indexer"
import { extractCharacterAttribute } from "@crossbell/util-metadata"
import { expandCrossbellNote } from "~/lib/expand-unit"
import { filterCommentCharacter } from "~/lib/filter-character"
@@ -18,6 +19,8 @@ import { getKeys, getStorage } from "~/lib/storage"
import { ExpandedNote, PageVisibilityEnum } from "~/lib/types"
import { client } from "~/queries/graphql"
export const PINNED_PAGE_KEY = "xlog:pinned-page"
export async function checkPageSlug(input: {
slug: string
characterId?: number
@@ -150,37 +153,53 @@ export async function getPagesBySite(input: {
const visibility = input.visibility || PageVisibilityEnum.All
const notes = await indexer.note.getMany({
characterId: input.characterId,
limit: input.limit || 10,
cursor: input.cursor,
orderBy: "publishedAt",
tags: [...(input.tags || []), input.type],
sources: "xlog",
})
const list = await Promise.all(
notes?.list.map(async (note) => {
const expanded = await expandCrossbellNote({
note,
useStat: input.useStat,
useHTML: input.useHTML,
useScore: false,
})
if (!input.keepBody) {
delete expanded.metadata?.content?.content
}
return expanded
}),
)
const expandedNotes: {
const expandedNotes = await (async (): Promise<{
list: ExpandedNote[]
pinnedNoteId?: number
count: number
cursor: string | null
} = Object.assign(notes, {
list,
})
}> => {
const [pinnedNote, notes] = await Promise.all([
!input.cursor ? getPinnedPage(input) : null,
await indexer.note.getMany({
characterId: input.characterId,
limit: input.limit || 10,
cursor: input.cursor,
orderBy: "publishedAt",
tags: [...(input.tags || []), input.type],
sources: "xlog",
}),
])
const list: NoteEntity[] = pinnedNote
? [
pinnedNote,
...notes.list.filter((note) => note.noteId !== pinnedNote.noteId),
]
: notes.list
return {
...notes,
pinnedNoteId: pinnedNote?.noteId,
list: await Promise.all(
list.map(async (note) => {
const expanded = await expandCrossbellNote({
note,
useStat: input.useStat,
useHTML: input.useHTML,
useScore: false,
})
if (!input.keepBody) {
delete expanded.metadata?.content?.content
}
return expanded
}),
),
}
})()
const local = getLocalPages({
characterId: input.characterId,
@@ -233,10 +252,16 @@ export async function getPagesBySite(input: {
break
}
expandedNotes.list = expandedNotes.list.sort((a, b) => {
if (!a.metadata?.content?.date_published) {
expandedNotes.list.sort((a, b) => {
if (
!a.metadata?.content?.date_published ||
a.noteId === expandedNotes.pinnedNoteId
) {
return -1
} else if (!b.metadata?.content?.date_published) {
} else if (
!b.metadata?.content?.date_published ||
b.noteId === expandedNotes.pinnedNoteId
) {
return 1
} else {
return (
@@ -681,3 +706,14 @@ export const getDistinctNoteTagsOfCharacter = async (characterId: number) => {
return result
}
export async function getPinnedPage({ characterId }: { characterId?: number }) {
if (!characterId) return null
const character = await indexer.character.get(characterId)
const noteId = extractCharacterAttribute(character, PINNED_PAGE_KEY)?.value
if (!character || !noteId || typeof noteId !== "number") return null
return indexer.note.get(character.characterId, noteId)
}
+43 -1
View File
@@ -1,10 +1,11 @@
"use client"
import { NoteMetadata } from "crossbell"
import type { NoteEntity, NoteMetadata } from "crossbell"
import { nanoid } from "nanoid"
import {
useAccountState,
useCharacterAttribute,
useDeleteNote,
useIsNoteLiked,
useMintNote,
@@ -695,3 +696,44 @@ export const useGetDistinctNoteTagsOfCharacter = (characterId?: number) => {
return pageModel.getDistinctNoteTagsOfCharacter(characterId)
})
}
export const usePinPage = ({
characterId,
noteId,
}: Pick<NoteEntity, "noteId" | "characterId">) => {
const queryClient = useQueryClient()
const { data, isLoading, update } = useCharacterAttribute<number>(
{
characterId,
key: pageModel.PINNED_PAGE_KEY,
},
{
onSuccess() {
return queryClient.invalidateQueries(["getPagesBySite", characterId])
},
},
)
const isPinned = data === noteId
const togglePin = useRefCallback(() => {
if (isPinned) {
update(null)
} else {
update(noteId)
}
})
return { isLoading, isPinned, togglePin }
}
export const usePinnedPage = ({
characterId,
}: Pick<Partial<NoteEntity>, "characterId">) => {
const { data, isLoading } = useCharacterAttribute({
characterId,
key: pageModel.PINNED_PAGE_KEY,
})
return { isLoading, noteId: data as number }
}