feat: follow function
This commit is contained in:
@@ -16,6 +16,10 @@ import { ConnectButton } from "../common/ConnectButton"
|
||||
import { RSS3Icon } from "../icons/RSS3"
|
||||
import { CrossbellIcon } from "../icons/Crossbell"
|
||||
import { useAccount } from "wagmi"
|
||||
import unidata from "~/lib/unidata"
|
||||
import { useEffect, useState } from "react"
|
||||
import toast from "react-hot-toast"
|
||||
import { useGetSubscription, useSubscribeToSite, useUnsubscribeFromSite } from "~/queries/site"
|
||||
|
||||
export type HeaderLinkType = {
|
||||
icon?: React.ReactNode
|
||||
@@ -47,18 +51,33 @@ export const SiteHeader: React.FC<{
|
||||
description: string | undefined | null
|
||||
icon: string | null | undefined
|
||||
navigation?: HeaderLinkType[]
|
||||
subscribed?: boolean
|
||||
}> = ({ siteName, description, icon, navigation, subscribed }) => {
|
||||
const setSubscribeModalOpened = useStore(
|
||||
(store) => store.setSubscribeModalOpened,
|
||||
)
|
||||
const setLoginModalOpened = useStore((store) => store.setLoginModalOpened)
|
||||
site?: string | undefined
|
||||
}> = ({ siteName, description, icon, navigation, site }) => {
|
||||
const { data: viewer } = useAccount()
|
||||
const subscribeToSite = useSubscribeToSite()
|
||||
const unsubscribeFromSite = useUnsubscribeFromSite()
|
||||
|
||||
const handleClickSubscribe = () => {
|
||||
setSubscribeModalOpened(true)
|
||||
const handleClickSubscribe = async () => {
|
||||
if (site && viewer?.address) {
|
||||
if (subscription.data) {
|
||||
unsubscribeFromSite.mutate({
|
||||
userId: viewer.address,
|
||||
siteId: site,
|
||||
})
|
||||
} else {
|
||||
subscribeToSite.mutate({
|
||||
userId: viewer.address,
|
||||
siteId: site,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const subscription = useGetSubscription({
|
||||
userId: viewer?.address || '',
|
||||
siteId: site || '',
|
||||
})
|
||||
|
||||
const leftLinks: HeaderLinkType[] = [
|
||||
{ label: "Home", url: "/" },
|
||||
...(navigation || []),
|
||||
@@ -82,34 +101,42 @@ export const SiteHeader: React.FC<{
|
||||
<div className="text-gray-500 text-sm">{description}</div>
|
||||
)}
|
||||
<div className="mt-3 text-sm">
|
||||
<Button
|
||||
rounded="full"
|
||||
size="sm"
|
||||
variant="rss3"
|
||||
onClick={() => window.open(`https://rss3.io/result?search=${viewer?.address}&filter=social`, '_blank')}
|
||||
className="space-x-1 mr-2"
|
||||
>
|
||||
<span className="pl-1">
|
||||
<RSS3Icon />
|
||||
</span>
|
||||
<span className="pr-1">
|
||||
Watch on RSS3
|
||||
</span>
|
||||
</Button>
|
||||
<Button
|
||||
rounded="full"
|
||||
size="sm"
|
||||
variant="crossbell"
|
||||
onClick={() => window.open(`https://crossbell.io`, '_blank')}
|
||||
className="space-x-1"
|
||||
>
|
||||
<span className="pl-1">
|
||||
<CrossbellIcon />
|
||||
</span>
|
||||
<span className="pr-1">
|
||||
Follow on Crossbell
|
||||
</span>
|
||||
</Button>
|
||||
{subscription.data ?
|
||||
<Button
|
||||
rounded="full"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
onClick={handleClickSubscribe}
|
||||
className="space-x-1 group"
|
||||
isLoading={unsubscribeFromSite.isLoading || subscribeToSite.isLoading}
|
||||
>
|
||||
<span className="pl-1">
|
||||
<CrossbellIcon />
|
||||
</span>
|
||||
<span className="pr-1 group-hover:hidden">
|
||||
Following
|
||||
</span>
|
||||
<span className="pr-1 hidden group-hover:block">
|
||||
Unfollow
|
||||
</span>
|
||||
</Button> :
|
||||
<Button
|
||||
rounded="full"
|
||||
size="sm"
|
||||
variant="crossbell"
|
||||
onClick={handleClickSubscribe}
|
||||
className="space-x-1"
|
||||
isLoading={unsubscribeFromSite.isLoading || subscribeToSite.isLoading || subscription.isLoading}
|
||||
>
|
||||
<span className="pl-1">
|
||||
<CrossbellIcon />
|
||||
</span>
|
||||
<span className="pr-1">
|
||||
Follow
|
||||
</span>
|
||||
</Button>
|
||||
}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -11,7 +11,6 @@ import { useStore } from "~/lib/store"
|
||||
export type SiteLayoutProps = {
|
||||
site?: Profile
|
||||
viewer?: Profile | null
|
||||
subscription?: { email?: boolean } | null
|
||||
children: React.ReactNode
|
||||
title?: string | null
|
||||
ogDescription?: string | null
|
||||
@@ -20,7 +19,6 @@ export type SiteLayoutProps = {
|
||||
export const SiteLayout: React.FC<SiteLayoutProps> = ({
|
||||
site,
|
||||
viewer,
|
||||
subscription,
|
||||
children,
|
||||
title,
|
||||
ogDescription,
|
||||
@@ -49,7 +47,7 @@ export const SiteLayout: React.FC<SiteLayoutProps> = ({
|
||||
siteName={site?.name}
|
||||
description={site?.bio}
|
||||
icon={site?.avatars?.[0]}
|
||||
subscribed={!!subscription}
|
||||
site={site?.username}
|
||||
/>
|
||||
<style>{ site?.css }</style>
|
||||
<div className={clsx(`max-w-screen-md mx-auto px-5 pb-12`, `pt-12`)}>
|
||||
|
||||
@@ -65,11 +65,13 @@ body {
|
||||
--shadow-color-button: theme("colors.indigo.700");
|
||||
@apply bg-indigo-500 hover:bg-indigo-600 focus:bg-indigo-700;
|
||||
@apply text-white;
|
||||
@apply fill-indigo-500;
|
||||
}
|
||||
|
||||
.button.is-secondary {
|
||||
@apply bg-gray-50 hover:bg-gray-100 focus:bg-gray-200;
|
||||
@apply text-gray-500;
|
||||
@apply fill-gray-500;
|
||||
}
|
||||
|
||||
.button.is-rss3 {
|
||||
|
||||
+30
-83
@@ -85,18 +85,15 @@ export const getSubscription = async (data: {
|
||||
userId: string
|
||||
siteId: string
|
||||
}) => {
|
||||
const membership = await getMembership({
|
||||
...data,
|
||||
role: MembershipRole.SUBSCRIBER,
|
||||
const links = await unidata.links.get({
|
||||
source: 'Crossbell Link',
|
||||
identity: data.userId,
|
||||
platform: 'Ethereum',
|
||||
filter: {
|
||||
to: data.siteId
|
||||
}
|
||||
})
|
||||
if (!membership) return
|
||||
const config = membership.config as any
|
||||
return {
|
||||
...membership,
|
||||
config: config && {
|
||||
email: config.email,
|
||||
},
|
||||
}
|
||||
return !!links?.list?.length
|
||||
}
|
||||
|
||||
export async function updateSite(
|
||||
@@ -148,85 +145,35 @@ export async function createSite(
|
||||
}
|
||||
|
||||
export async function subscribeToSite(
|
||||
gate: Gate,
|
||||
input: {
|
||||
userId: string
|
||||
siteId: string
|
||||
email?: boolean
|
||||
newUser?: {
|
||||
email: string
|
||||
url: string
|
||||
}
|
||||
},
|
||||
) {
|
||||
// const { newUser } = input
|
||||
// if (newUser) {
|
||||
// // Create the login token instead
|
||||
// sendLoginEmail({
|
||||
// email: newUser.email,
|
||||
// url: newUser.url,
|
||||
// toSubscribeSiteId: input.siteId,
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
|
||||
// const user = gate.getUser(true)
|
||||
|
||||
// const site = await getSite(input.siteId)
|
||||
// const subscription = await getSubscription({
|
||||
// userId: user.id,
|
||||
// siteId: site.id,
|
||||
// })
|
||||
// if (!subscription) {
|
||||
// await prismaPrimary.membership.create({
|
||||
// data: {
|
||||
// role: MembershipRole.SUBSCRIBER,
|
||||
// user: {
|
||||
// connect: {
|
||||
// id: user.id,
|
||||
// },
|
||||
// },
|
||||
// site: {
|
||||
// connect: {
|
||||
// id: site.id,
|
||||
// },
|
||||
// },
|
||||
// config: {
|
||||
// email: input.email,
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
// } else {
|
||||
// await prismaPrimary.membership.update({
|
||||
// where: {
|
||||
// id: subscription.id,
|
||||
// },
|
||||
// data: {
|
||||
// config: {
|
||||
// email: input.email,
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
// }
|
||||
return unidata.links.set({
|
||||
source: 'Crossbell Link',
|
||||
identity: input.userId,
|
||||
platform: 'Ethereum',
|
||||
action: 'add',
|
||||
}, {
|
||||
to: input.siteId,
|
||||
type: 'follow',
|
||||
})
|
||||
}
|
||||
|
||||
export async function unsubscribeFromSite(
|
||||
gate: Gate,
|
||||
input: { siteId: string },
|
||||
input: {
|
||||
userId: string
|
||||
siteId: string
|
||||
},
|
||||
) {
|
||||
const user = gate.getUser(true)
|
||||
|
||||
const subscription = await getSubscription({
|
||||
userId: user.id,
|
||||
siteId: input.siteId,
|
||||
})
|
||||
|
||||
if (!subscription) {
|
||||
throw new Error(`Subscription not found`)
|
||||
}
|
||||
|
||||
await prismaPrimary.membership.delete({
|
||||
where: {
|
||||
id: subscription.id,
|
||||
},
|
||||
return unidata.links.set({
|
||||
source: 'Crossbell Link',
|
||||
identity: input.userId,
|
||||
platform: 'Ethereum',
|
||||
action: 'remove',
|
||||
}, {
|
||||
to: input.siteId,
|
||||
type: 'follow',
|
||||
})
|
||||
}
|
||||
|
||||
@@ -17,6 +17,18 @@ export const useGetSite = (input: string) => {
|
||||
})
|
||||
}
|
||||
|
||||
export const useGetSubscription = (data: {
|
||||
userId: string
|
||||
siteId: string
|
||||
}) => {
|
||||
return useQuery(['getSubscription', data], async () => {
|
||||
if (!data.userId || !data.siteId) {
|
||||
return false
|
||||
}
|
||||
return siteModel.getSubscription(data)
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateSite() {
|
||||
const queryClient = useQueryClient();
|
||||
const mutation = useMutation(async (
|
||||
@@ -44,3 +56,25 @@ export function useCreateSite() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useSubscribeToSite() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation(async (input: Parameters<typeof siteModel.subscribeToSite>[0]) => {
|
||||
return siteModel.subscribeToSite(input)
|
||||
}, {
|
||||
onSuccess: (data, variables) => {
|
||||
queryClient.invalidateQueries(['getSubscription', variables])
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUnsubscribeFromSite() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation(async (input: Parameters<typeof siteModel.subscribeToSite>[0]) => {
|
||||
return siteModel.unsubscribeFromSite(input)
|
||||
}, {
|
||||
onSuccess: (data, variables) => {
|
||||
queryClient.invalidateQueries(['getSubscription', variables])
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user