add user page

This commit is contained in:
Ren Amamiya
2023-06-28 17:23:36 +07:00
parent 3fe601cfc6
commit ec1ff9ab87
27 changed files with 492 additions and 491 deletions

View File

@@ -11,7 +11,9 @@ export function useEvent(id: string) {
async () => {
const result = await getNoteByID(id);
if (result) {
result["content"] = parser(result);
if (result.kind === 1 || result.kind === 1063) {
result["content"] = parser(result);
}
return result;
} else {
const event = await ndk.fetchEvent(id);
@@ -24,8 +26,10 @@ export function useEvent(id: string) {
event.created_at,
);
event["event_id"] = event.id;
// @ts-ignore
event["content"] = parser(event);
if (event.kind === 1 || event.kind === 1063) {
// @ts-ignore
event["content"] = parser(event);
}
return event;
}
},

View File

@@ -1,27 +0,0 @@
import { useAccount } from "./useAccount";
import { RelayContext } from "@shared/relayProvider";
import { useQuery } from "@tanstack/react-query";
import { nip02ToArray } from "@utils/transform";
import { useContext } from "react";
export function useFollows() {
const ndk = useContext(RelayContext);
const { account } = useAccount();
const { status, data: follows } = useQuery(
["follows", account.pubkey],
async () => {
const res = await ndk.fetchEvents({
kinds: [3],
authors: [account.pubkey],
});
const latest = [...res].slice(-1)[0];
const list = nip02ToArray(latest.tags);
return list;
},
{
enabled: account ? true : false,
},
);
return { status, follows };
}

View File

@@ -23,11 +23,13 @@ export function useProfile(id: string) {
const current = Math.floor(Date.now() / 1000);
const result = await getPleb(npub);
if (result && result.created_at + 86400 > current) {
if (result && parseInt(result.created_at) + 86400 >= current) {
console.log("cache", result);
return result;
} else {
const user = ndk.getUser({ npub });
await user.fetchProfile();
console.log("new", user);
await createPleb(id, user.profile);
return user.profile;

View File

@@ -0,0 +1,65 @@
import { useAccount } from "./useAccount";
import { usePublish } from "@libs/ndk";
import { RelayContext } from "@shared/relayProvider";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { nip02ToArray } from "@utils/transform";
import { useContext } from "react";
export function useSocial() {
const ndk = useContext(RelayContext);
const queryClient = useQueryClient();
const publish = usePublish();
const { account } = useAccount();
const { status, data: userFollows } = useQuery(
["userFollows", account.pubkey],
async () => {
const res = await ndk.fetchEvents({
kinds: [3],
authors: [account.pubkey],
});
const latest = [...res].slice(-1)[0];
const list = nip02ToArray(latest.tags);
return list;
},
{
enabled: account ? true : false,
},
);
const unfollow = (pubkey: string) => {
const followsAsSet = new Set(userFollows);
followsAsSet.delete(pubkey);
const tags = [];
followsAsSet.forEach((item) => {
tags.push(["p", item]);
});
// publish event
publish({ content: "", kind: 3, tags: tags });
// invalid cache
queryClient.invalidateQueries({
queryKey: ["userFollows", account.pubkey],
});
};
const follow = (pubkey: string) => {
const followsAsSet = new Set(userFollows);
followsAsSet.add(pubkey);
const tags = [];
followsAsSet.forEach((item) => {
tags.push(["p", item]);
});
// publish event
publish({ content: "", kind: 3, tags: tags });
// invalid cache
queryClient.invalidateQueries({
queryKey: ["userFollows", account.pubkey],
});
};
return { status, userFollows, follow, unfollow };
}

View File

@@ -2,10 +2,11 @@ import { MentionUser } from "@shared/notes/mentions/user";
import destr from "destr";
import getUrls from "get-urls";
import { parseReferences } from "nostr-tools";
import { ReactNode } from "react";
import { Link } from "react-router-dom";
import reactStringReplace from "react-string-replace";
function isJsonString(str) {
function isJsonString(str: string) {
try {
JSON.parse(str);
} catch (e) {
@@ -24,7 +25,7 @@ export function parser(event: any) {
const content: {
original: string;
parsed: any;
parsed: ReactNode[];
notes: string[];
images: string[];
videos: string[];
@@ -39,9 +40,11 @@ export function parser(event: any) {
};
// remove unnecessary whitespaces
// @ts-ignore
content.parsed = content.parsed.replace(/\s{2,}/g, " ");
// remove unnecessary linebreak
// @ts-ignore
content.parsed = content.parsed.replace(/(\r\n|\r|\n){2,}/g, "$1\n");
// parse urls