-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1524 from ecency/feature/pinned-comment
Comments pinning
- Loading branch information
Showing
49 changed files
with
1,392 additions
and
3,450 deletions.
There are no files selected for viewing
18 changes: 2 additions & 16 deletions
18
src/common/api/mutations.ts → src/common/api/mutations/account-claiming.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Entry } from "../../store/entries/types"; | ||
import { useMappedStore } from "../../store/use-mapped-store"; | ||
import { useContext } from "react"; | ||
import { EntriesCacheContext, QueryIdentifiers } from "../../core"; | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { comment, CommentOptions, formatError, MetaData } from "../operations"; | ||
import tempEntry from "../../helper/temp-entry"; | ||
import { FullAccount } from "../../store/accounts/types"; | ||
import * as ss from "../../util/session-storage"; | ||
import { error } from "../../components/feedback"; | ||
|
||
export function useCreateReply(entry: Entry | null, parent?: Entry, onSuccess?: () => void) { | ||
const { activeUser } = useMappedStore(); | ||
const { addReply, updateRepliesCount, updateCache } = useContext(EntriesCacheContext); | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation( | ||
["reply-create", activeUser?.username, entry?.author, entry?.permlink], | ||
async ({ | ||
permlink, | ||
text, | ||
jsonMeta, | ||
options, | ||
point | ||
}: { | ||
permlink: string; | ||
text: string; | ||
jsonMeta: MetaData; | ||
point: boolean; | ||
options?: CommentOptions; | ||
}) => { | ||
if (!activeUser || !activeUser.data.__loaded || !entry) { | ||
throw new Error("[Reply][Create] – no active user provided"); | ||
} | ||
|
||
await comment( | ||
activeUser.username, | ||
entry.author, | ||
entry.permlink, | ||
permlink, | ||
"", | ||
text, | ||
jsonMeta, | ||
options ?? null, | ||
point | ||
); | ||
return tempEntry({ | ||
author: activeUser.data as FullAccount, | ||
permlink, | ||
parentAuthor: entry.author, | ||
parentPermlink: entry.permlink, | ||
title: "", | ||
body: text, | ||
tags: [], | ||
description: null | ||
}); | ||
}, | ||
{ | ||
onSuccess: (data) => { | ||
if (!entry) { | ||
return; | ||
} | ||
|
||
addReply(entry, data); | ||
updateCache([data]); | ||
|
||
// remove reply draft | ||
ss.remove(`reply_draft_${entry.author}_${entry.permlink}`); | ||
|
||
if (entry.children === 0) { | ||
// Update parent comment. | ||
updateRepliesCount(entry, 1); | ||
} | ||
const previousReplies = | ||
queryClient.getQueryData<Entry[]>([ | ||
QueryIdentifiers.FETCH_DISCUSSIONS, | ||
parent?.author ?? entry.author, | ||
parent?.permlink ?? entry.permlink | ||
]) ?? []; | ||
queryClient.setQueryData( | ||
[ | ||
QueryIdentifiers.FETCH_DISCUSSIONS, | ||
parent?.author ?? entry.author, | ||
parent?.permlink ?? entry.permlink | ||
], | ||
[data, ...previousReplies] | ||
); | ||
|
||
onSuccess?.(); | ||
}, | ||
onError: (e) => error(...formatError(e)) | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./account-claiming"; | ||
export * from "./create-reply"; | ||
export * from "./update-reply"; | ||
export * from "./user-activity"; | ||
export * from "./pin-reply"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { Entry } from "../../store/entries/types"; | ||
import { createPatch, makeJsonMetaDataReply } from "../../helper/posting"; | ||
import { version } from "../../../../package.json"; | ||
import { useUpdateReply } from "./update-reply"; | ||
import { MetaData } from "../operations"; | ||
|
||
export function usePinReply(reply: Entry, parent: Entry) { | ||
const { mutateAsync: updateReply } = useUpdateReply(parent); | ||
|
||
return useMutation(["reply-pin", reply, parent], async ({ pin }: { pin: boolean }) => { | ||
const meta = makeJsonMetaDataReply( | ||
parent.json_metadata.tags || ["ecency"], | ||
version | ||
) as MetaData; | ||
|
||
let newBody = parent.body.replace(/[\x00-\x09\x0B-\x0C\x0E-\x1F\x7F-\x9F]/g, ""); | ||
const patch = createPatch(parent.body, newBody.trim()); | ||
if (patch && patch.length < Buffer.from(parent.body, "utf-8").length) { | ||
newBody = patch; | ||
} | ||
|
||
meta.pinned_reply = pin ? `${reply.author}/${reply.permlink}` : undefined; | ||
return updateReply({ text: newBody, point: true, jsonMeta: meta }); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Entry } from "../../store/entries/types"; | ||
import { useMappedStore } from "../../store/use-mapped-store"; | ||
import { useContext } from "react"; | ||
import { EntriesCacheContext } from "../../core"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { comment, CommentOptions, formatError, MetaData } from "../operations"; | ||
import * as ss from "../../util/session-storage"; | ||
import { error } from "../../components/feedback"; | ||
|
||
export function useUpdateReply(entry: Entry | null, onSuccess?: () => void) { | ||
const { activeUser } = useMappedStore(); | ||
const { updateCache } = useContext(EntriesCacheContext); | ||
|
||
return useMutation( | ||
["reply-update", activeUser?.username, entry?.author, entry?.permlink], | ||
async ({ | ||
text, | ||
jsonMeta, | ||
options, | ||
point | ||
}: { | ||
text: string; | ||
jsonMeta: MetaData; | ||
point: boolean; | ||
options?: CommentOptions; | ||
}) => { | ||
if (!activeUser || !activeUser.data.__loaded || !entry) { | ||
throw new Error("[Reply][Create] – no active user provided"); | ||
} | ||
|
||
await comment( | ||
activeUser.username, | ||
entry.parent_author ?? "", | ||
entry.parent_permlink ?? entry.category, | ||
entry.permlink, | ||
"", | ||
text, | ||
jsonMeta, | ||
options ?? null, | ||
point | ||
); | ||
return { | ||
...entry, | ||
json_metadata: jsonMeta, | ||
body: text | ||
}; | ||
}, | ||
{ | ||
onSuccess: (data) => { | ||
if (!entry) { | ||
return; | ||
} | ||
|
||
updateCache([data]); | ||
|
||
// remove reply draft | ||
ss.remove(`reply_draft_${entry.author}_${entry.permlink}`); | ||
onSuccess?.(); | ||
}, | ||
onError: (e) => error(...formatError(e)) | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { usrActivity } from "../private-api"; | ||
|
||
interface Params { | ||
bl?: string | number; | ||
tx?: string | number; | ||
} | ||
|
||
export function useUserActivity(username: string | undefined, ty: number) { | ||
return useMutation(["user-activity", username, ty], async (params: Params | undefined) => { | ||
if (username) { | ||
await usrActivity(username, ty, params?.bl, params?.tx); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.