-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ingawei/add dashboard follows (#2036)
* follow functionality
- Loading branch information
Showing
9 changed files
with
165 additions
and
15 deletions.
There are no files selected for viewing
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,32 @@ | ||
import { getUserFollowsDashboard } from 'common/supabase/dashboard-follows' | ||
import { run } from 'common/supabase/utils' | ||
import { createSupabaseClient } from 'shared/supabase/init' | ||
import { z } from 'zod' | ||
import { authEndpoint } from './helpers' | ||
|
||
const schema = z.object({ | ||
dashboardId: z.string(), | ||
}) | ||
|
||
export const followdashboard = authEndpoint(async (req, auth) => { | ||
const { dashboardId } = schema.parse(req.body) | ||
const followerId = auth.uid | ||
|
||
const db = createSupabaseClient() | ||
|
||
const isFollowing = await getUserFollowsDashboard(followerId, dashboardId, db) | ||
|
||
const query = isFollowing | ||
? db | ||
.from('dashboard_follows') | ||
.delete() | ||
.eq('dashboard_id', dashboardId) | ||
.eq('follower_id', followerId) | ||
: db | ||
.from('dashboard_follows') | ||
.upsert([{ dashboard_id: dashboardId, follower_id: followerId }]) | ||
|
||
await run(query) | ||
|
||
return { isFollowing: !isFollowing } | ||
}) |
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,7 @@ | ||
create table if not exists | ||
dashboard_follows ( | ||
dashboard_id text not null, | ||
follower_id text not null, | ||
created_time timestamptz default now(), | ||
primary key (dashboard_id, follower_id) | ||
); |
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,19 @@ | ||
import { SupabaseClient } from '@supabase/supabase-js' | ||
import { run } from './utils' | ||
|
||
export async function getUserFollowsDashboard( | ||
userId: string, | ||
dashboardId: string, | ||
db: SupabaseClient | ||
) { | ||
const { data: follows } = await run( | ||
db | ||
.from('dashboard_follows') | ||
.select('*') | ||
.eq('follower_id', userId) | ||
.eq('dashboard_id', dashboardId) | ||
.limit(1) | ||
) | ||
|
||
return follows.length > 0 | ||
} |
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,45 @@ | ||
import { FaBookmark, FaRegBookmark } from 'react-icons/fa6' | ||
import { useUserFollowsDashboard } from 'web/hooks/use-dashboard-follows' | ||
import { useIsAuthorized, useUser } from 'web/hooks/use-user' | ||
import { followDashboard } from 'web/lib/firebase/api' | ||
import { Tooltip } from '../widgets/tooltip' | ||
import { Col } from '../layout/col' | ||
import clsx from 'clsx' | ||
|
||
export function FollowDashboardButton(props: { | ||
dashboardId: string | ||
dashboardCreatorId: string | ||
size?: 'sm' | 'md' | ||
}) { | ||
const { dashboardId, dashboardCreatorId, size = 'md' } = props | ||
const user = useUser() | ||
const { isFollowing, setIsFollowing } = useUserFollowsDashboard( | ||
user?.id, | ||
dashboardId | ||
) | ||
const isAuth = useIsAuthorized() | ||
if ( | ||
!user || | ||
!isAuth | ||
|| dashboardCreatorId === user?.id | ||
) { | ||
return null | ||
} | ||
return ( | ||
<Tooltip text={'Bookmark'} placement="left-start"> | ||
<button | ||
onClick={() => | ||
followDashboard({ dashboardId: dashboardId }).then((result) => { | ||
setIsFollowing(result.isFollowing) | ||
}) | ||
} | ||
> | ||
{isFollowing ? ( | ||
<FaBookmark className={'h-5 w-5 text-yellow-500'} /> | ||
) : ( | ||
<FaRegBookmark className={'text-ink-500 h-5 w-5'} /> | ||
)} | ||
</button> | ||
</Tooltip> | ||
) | ||
} |
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,19 @@ | ||
import { getUserFollowsDashboard } from 'common/supabase/dashboard-follows' | ||
import { get } from 'lodash' | ||
import { useEffect, useState } from 'react' | ||
import { db } from 'web/lib/supabase/db' | ||
|
||
export const useUserFollowsDashboard = ( | ||
userId: string | undefined, | ||
dashboardId: string | ||
) => { | ||
const [isFollowing, setIsFollowing] = useState<boolean | undefined>(undefined) | ||
useEffect(() => { | ||
if (userId) { | ||
getUserFollowsDashboard(userId, dashboardId, db).then((res) => { | ||
setIsFollowing(res) | ||
}) | ||
} | ||
}, [userId, dashboardId]) | ||
return { isFollowing, setIsFollowing } | ||
} |
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
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
592e821
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./docs
docs-mantic.vercel.app
docs-pi-teal.vercel.app
docs-git-main-mantic.vercel.app
docs.manifold.markets
592e821
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
prod – ./web
prod-git-main-mantic.vercel.app
prod-mantic.vercel.app
manifold.markets
www.manifold.markets
mantic.vercel.app
592e821
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
dev – ./web
dev-manifold.vercel.app
dev-mantic.vercel.app
dev-git-main-mantic.vercel.app
dev.manifold.markets