wip: native notification

This commit is contained in:
Ren Amamiya 2023-05-30 15:33:27 +07:00
parent b856c2b8b5
commit 1e5bd7e21f
7 changed files with 59 additions and 18 deletions

View File

@ -96,6 +96,8 @@ export function Page() {
}, },
); );
if (!account) return <div>Fuck SSR</div>;
return ( return (
<div className="h-full w-full grid grid-cols-3"> <div className="h-full w-full grid grid-cols-3">
<div className="col-span-2 flex flex-col justify-between border-r border-zinc-900"> <div className="col-span-2 flex flex-col justify-between border-r border-zinc-900">

View File

@ -24,7 +24,7 @@ export function Page() {
]); ]);
const addMessage = useChatMessages((state: any) => state.add); const addMessage = useChatMessages((state: any) => state.add);
useSWRSubscription(account ? ["chat", pubkey] : null, ([, key]) => { useSWRSubscription(account && pubkey ? ["chat", pubkey] : null, ([, key]) => {
const unsubscribe = pool.subscribe( const unsubscribe = pool.subscribe(
[ [
{ {
@ -59,6 +59,8 @@ export function Page() {
}; };
}, [pubkey]); }, [pubkey]);
if (!account) return <div>Fuck SSR</div>;
return ( return (
<div className="h-full w-full grid grid-cols-3"> <div className="h-full w-full grid grid-cols-3">
<div className="col-span-2 flex flex-col justify-between border-r border-zinc-900"> <div className="col-span-2 flex flex-col justify-between border-r border-zinc-900">

View File

@ -3,20 +3,22 @@ import { useEffect } from "react";
import { navigate } from "vite-plugin-ssr/client/router"; import { navigate } from "vite-plugin-ssr/client/router";
export function Page() { export function Page() {
const fetchLastLogin = useActiveAccount((state: any) => state.fetchLastLogin);
const fetchAccount = useActiveAccount((state: any) => state.fetch); const fetchAccount = useActiveAccount((state: any) => state.fetch);
const account = useActiveAccount((state: any) => state.account); const account = useActiveAccount((state: any) => state.account);
if (!account) { if (!account && typeof window !== "undefined") {
navigate("/app/auth", { overwriteLastHistoryEntry: true }); navigate("/app/auth", { overwriteLastHistoryEntry: true });
} }
if (account) { if (account && typeof window !== "undefined") {
navigate("/app/prefetch", { overwriteLastHistoryEntry: true }); navigate("/app/prefetch", { overwriteLastHistoryEntry: true });
} }
useEffect(() => { useEffect(() => {
fetchAccount(); fetchAccount();
}, [fetchAccount]); fetchLastLogin();
}, [fetchAccount, fetchLastLogin]);
return ( return (
<div className="h-screen w-screen bg-zinc-50 text-zinc-900 dark:bg-black dark:text-white" /> <div className="h-screen w-screen bg-zinc-50 text-zinc-900 dark:bg-black dark:text-white" />

View File

@ -8,24 +8,23 @@ import {
countTotalNotes, countTotalNotes,
createChat, createChat,
createNote, createNote,
getLastLogin,
} from "@utils/storage"; } from "@utils/storage";
import { getParentID } from "@utils/transform"; import { getParentID } from "@utils/transform";
import { useCallback, useContext, useRef } from "react"; import { useCallback, useContext, useRef } from "react";
import useSWRSubscription from "swr/subscription"; import useSWRSubscription from "swr/subscription";
import { navigate } from "vite-plugin-ssr/client/router"; import { navigate } from "vite-plugin-ssr/client/router";
let lastLogin: string;
let totalNotes: number; let totalNotes: number;
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
lastLogin = await getLastLogin();
totalNotes = await countTotalNotes(); totalNotes = await countTotalNotes();
} }
export function Page() { export function Page() {
const pool: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const account = useActiveAccount((state: any) => state.account); const [account, lastLogin] = useActiveAccount((state: any) => [
state.account,
state.lastLogin,
]);
const now = useRef(new Date()); const now = useRef(new Date());
const eose = useRef(0); const eose = useRef(0);
@ -33,15 +32,14 @@ export function Page() {
const getQuery = useCallback(() => { const getQuery = useCallback(() => {
const query = []; const query = [];
const follows = JSON.parse(account.follows); const follows = JSON.parse(account.follows);
const last = parseInt(lastLogin);
let queryNoteSince: number; let queryNoteSince: number;
if (totalNotes === 0) { if (totalNotes === 0) {
queryNoteSince = dateToUnix(getHourAgo(48, now.current)); queryNoteSince = dateToUnix(getHourAgo(48, now.current));
} else { } else {
if (parseInt(lastLogin) > 0) { if (lastLogin > 0) {
queryNoteSince = last; queryNoteSince = lastLogin;
} else { } else {
queryNoteSince = dateToUnix(getHourAgo(48, now.current)); queryNoteSince = dateToUnix(getHourAgo(48, now.current));
} }
@ -58,21 +56,21 @@ export function Page() {
query.push({ query.push({
kinds: [4], kinds: [4],
"#p": [account.pubkey], "#p": [account.pubkey],
since: last, since: lastLogin,
}); });
// kind 4 (chats) query // kind 4 (chats) query
query.push({ query.push({
kinds: [4], kinds: [4],
authors: [account.pubkey], authors: [account.pubkey],
since: last, since: lastLogin,
}); });
// kind 43, 43 (mute user, hide message) query // kind 43, 43 (mute user, hide message) query
query.push({ query.push({
authors: [account.pubkey], authors: [account.pubkey],
kinds: [43, 44], kinds: [43, 44],
since: last, since: lastLogin,
}); });
return query; return query;

View File

@ -1,10 +1,42 @@
import { Image } from "@shared/image"; import { Image } from "@shared/image";
import { DEFAULT_AVATAR } from "@stores/constants"; import { RelayContext } from "@shared/relayProvider";
import { useActiveAccount } from "@stores/accounts";
import { DEFAULT_AVATAR, READONLY_RELAYS } from "@stores/constants";
import { useProfile } from "@utils/hooks/useProfile"; import { useProfile } from "@utils/hooks/useProfile";
import { sendNativeNotification } from "@utils/notification";
import { useContext } from "react";
import useSWRSubscription from "swr/subscription";
export function ActiveAccount({ data }: { data: any }) { export function ActiveAccount({ data }: { data: any }) {
const pool: any = useContext(RelayContext);
const lastLogin = useActiveAccount((state: any) => state.lastLogin);
const { user } = useProfile(data.pubkey); const { user } = useProfile(data.pubkey);
useSWRSubscription(
user && lastLogin > 0 ? ["account", data.pubkey] : null,
([, key]) => {
// subscribe to channel
const unsubscribe = pool.subscribe(
[
{
"#p": [key],
since: lastLogin,
limit: 20,
},
],
READONLY_RELAYS,
(event) => {
sendNativeNotification(event.content);
},
);
return () => {
unsubscribe();
};
},
);
return ( return (
<button type="button" className="relative h-11 w-11 overflow-hidden"> <button type="button" className="relative h-11 w-11 overflow-hidden">
<Image <Image

View File

@ -1,12 +1,17 @@
import { getActiveAccount } from "@utils/storage"; import { getActiveAccount, getLastLogin } from "@utils/storage";
import { create } from "zustand"; import { create } from "zustand";
export const useActiveAccount = create((set) => ({ export const useActiveAccount = create((set) => ({
account: null, account: null,
lastLogin: 0,
fetch: async () => { fetch: async () => {
const response = await getActiveAccount(); const response = await getActiveAccount();
set({ account: response }); set({ account: response });
}, },
fetchLastLogin: async () => {
const response = await getLastLogin();
set({ lastLogin: parseInt(response) });
},
updateFollows: (list: any) => { updateFollows: (list: any) => {
set((state: any) => ({ account: { ...state.account, follows: list } })); set((state: any) => ({ account: { ...state.account, follows: list } }));
}, },

View File

@ -11,6 +11,6 @@ export async function sendNativeNotification(content: string) {
permissionGranted = permission === "granted"; permissionGranted = permission === "granted";
} }
if (permissionGranted) { if (permissionGranted) {
sendNotification({ title: "TAURI", body: content }); sendNotification({ title: "Lume", body: content });
} }
} }