Connect, collaborate, and grow with a community of TypeScript developers
add tooltip to unauthorized bookmark and updoot button (#73)
Ben Asokanthanb06c7cb45a47
diff --git a/package.json b/package.jsonindex b8ae438b..c8b852b3 100644--- a/package.json+++ b/package.json@@ -28,6 +28,7 @@"@radix-ui/react-slot": "^1.0.2","@radix-ui/react-tabs": "^1.0.4","@radix-ui/react-toast": "^1.1.4",+ "@radix-ui/react-tooltip": "^1.0.6","@t3-oss/env-nextjs": "^0.3.1","@tanstack/react-query": "^4.29.7","@trpc/client": "^10.26.0",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex fefa90cc..549b14c7 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -47,6 +47,9 @@ dependencies:'@radix-ui/react-toast':specifier: ^1.1.4version: 1.1.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-tooltip':+ specifier: ^1.0.6+ version: 1.0.6(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@t3-oss/env-nextjs':specifier: ^0.3.1version: 0.3.1([email protected])([email protected])@@ -1418,6 +1421,38 @@ packages:react-dom: 18.2.0([email protected])dev: false+ /@radix-ui/[email protected](@types/[email protected])(@types/[email protected])([email protected])([email protected]):+ resolution: {integrity: sha512-DmNFOiwEc2UDigsYj6clJENma58OelxD24O4IODoZ+3sQc3Zb+L8w1EP+y9laTuKCLAysPw4fD6/v0j4KNV8rg==}+ peerDependencies:+ '@types/react': '*'+ '@types/react-dom': '*'+ react: ^16.8 || ^17.0 || ^18.0+ react-dom: ^16.8 || ^17.0 || ^18.0+ peerDependenciesMeta:+ '@types/react':+ optional: true+ '@types/react-dom':+ optional: true+ dependencies:+ '@babel/runtime': 7.22.5+ '@radix-ui/primitive': 1.0.1+ '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-context': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-dismissable-layer': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-id': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-popper': 1.1.2(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-portal': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-presence': 1.0.1(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-slot': 1.0.2(@types/[email protected])([email protected])+ '@radix-ui/react-use-controllable-state': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-visually-hidden': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@types/react': 18.2.8+ '@types/react-dom': 18.2.4+ react: 18.2.0+ react-dom: 18.2.0([email protected])+ dev: false+resolution: {integrity: sha512-D94LjX4Sp0xJFVaoQOd3OO9k7tpBYNOXdVhkltUbGv2Qb9OXdrg/CpsjlZv7ia14Sylv398LswWBVVu5nqKzAQ==}peerDependencies:diff --git a/src/components/challenge/description-panel.tsx b/src/components/challenge/description-panel.tsxindex ff72b731..51a7ab74 100644--- a/src/components/challenge/description-panel.tsx+++ b/src/components/challenge/description-panel.tsx@@ -10,16 +10,23 @@ import ReactMarkdown from 'react-markdown';import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';import remarkGfm from 'remark-gfm';+import { Button } from '~/components/ui/button';+import {+ Dialog,+ DialogContent,+ DialogHeader,+ DialogTitle,+ DialogTrigger,+} from '~/components/ui/dialog';+import { Tabs, TabsContent, TabsList, TabsTrigger } from '~/components/ui/tabs';+import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '~/components/ui/tooltip';+import { TypographyH3 } from '~/components/ui/typography/h3';+import type { Challenge } from '.';import { DifficultyBadge } from '../explore/difficulty-badge';-import { Button } from '../ui/button';-import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '../ui/dialog';-import { Tabs, TabsContent, TabsList, TabsTrigger } from '../ui/tabs';-import { TypographyH3 } from '../ui/typography/h3';import { addOrRemoveBookmark } from './bookmark.action';import { incrementOrDecrementUpvote } from './increment.action';import { ShareForm } from './share-form';import { Solutions } from './solutions';-import type { Challenge } from '.';interface Props {challenge: NonNullable<Challenge>;@@ -55,6 +62,7 @@ export function DescriptionPanel({ challenge }: Props) {}}, 500),).current;+return (<><Tabs defaultValue="description" className="w-full">@@ -72,39 +80,50 @@ export function DescriptionPanel({ challenge }: Props) {<div className="mb-6 flex items-center gap-4"><DifficultyBadge difficulty={challenge.difficulty} />- <Button- variant="ghost"- className="p-1"- disabled={!session?.data?.user?.id}- onClick={(): void => {- let shouldBookmark = false;- if (hasBookmarked) {- shouldBookmark = false;- setHasBookmarked(false);- } else {- shouldBookmark = true;- setHasBookmarked(true);- }- debouncedBookmark(- challenge.id,- session?.data?.user?.id as string,- shouldBookmark,- )?.catch((e) => {- console.error(e);- });- }}- >- <BookmarkIcon- size={20}- className={clsx(- {- 'fill-blue-500 stroke-blue-500': hasBookmarked,- 'stroke-gray-500': !hasBookmarked,- },- 'hover:stroke-gray-400',- )}- />- </Button>+ <TooltipProvider>+ <Tooltip delayDuration={0.05} open={session?.data?.user?.id ? false : undefined}>+ <TooltipTrigger asChild>+ <span>+ <Button+ variant="ghost"+ className="p-1"+ disabled={!session?.data?.user?.id}+ onClick={(): void => {+ let shouldBookmark = false;+ if (hasBookmarked) {+ shouldBookmark = false;+ setHasBookmarked(false);+ } else {+ shouldBookmark = true;+ setHasBookmarked(true);+ }+ debouncedBookmark(+ challenge.id,+ session?.data?.user?.id as string,+ shouldBookmark,+ )?.catch((e) => {+ console.error(e);+ });+ }}+ >+ <BookmarkIcon+ size={20}+ className={clsx(+ {+ 'fill-blue-500 stroke-blue-500': hasBookmarked,+ 'stroke-gray-500': !hasBookmarked,+ },+ 'hover:stroke-gray-400',+ )}+ />+ </Button>+ </span>+ </TooltipTrigger>+ <TooltipContent>+ <p>Login to Bookmark</p>+ </TooltipContent>+ </Tooltip>+ </TooltipProvider><Dialog><DialogTrigger><Button variant="ghost" className="p-1">@@ -120,53 +139,64 @@ export function DescriptionPanel({ challenge }: Props) {</DialogHeader></DialogContent></Dialog>- <Button- className="group -ml-1 w-14 gap-2 rounded-lg p-1"- variant="ghost"- disabled={!session?.data?.user?.id}- onClick={(): void => {- let shouldIncrement = false;- if (hasVoted) {- setVotes((v) => v - 1);- shouldIncrement = false;- setHasVoted(false);- } else {- setVotes((v) => v + 1);- shouldIncrement = true;- setHasVoted(true);- }- debouncedSearch(- challenge.id,- session?.data?.user?.id as string,- shouldIncrement,- )?.catch((e) => {- console.error(e);- });- }}- >- <ThumbsUp- size={20}- className={clsx(- {- 'fill-emerald-600 stroke-emerald-600 group-hover:stroke-emerald-500 dark:fill-emerald-400 dark:stroke-emerald-400':- hasVoted,- 'stroke-zinc-500': !hasVoted,- },- 'duration-300 group-hover:stroke-zinc-400',- )}- />- <span- className={clsx(- {- 'text-emerald-600 dark:text-emerald-400': hasVoted,- 'text-zinc-500 group-hover:text-zinc-400': !hasVoted,- },- 'self-end text-lg duration-300',- )}- >- {votes}- </span>- </Button>+ <TooltipProvider>+ <Tooltip delayDuration={0.05} open={session?.data?.user?.id ? false : undefined}>+ <TooltipTrigger asChild>+ <span>+ <Button+ className="group -ml-1 w-14 gap-2 rounded-lg p-1"+ variant="ghost"+ disabled={!session?.data?.user?.id}+ onClick={(): void => {+ let shouldIncrement = false;+ if (hasVoted) {+ setVotes((v) => v - 1);+ shouldIncrement = false;+ setHasVoted(false);+ } else {+ setVotes((v) => v + 1);+ shouldIncrement = true;+ setHasVoted(true);+ }+ debouncedSearch(+ challenge.id,+ session?.data?.user?.id as string,+ shouldIncrement,+ )?.catch((e) => {+ console.error(e);+ });+ }}+ >+ <ThumbsUp+ size={20}+ className={clsx(+ {+ 'fill-emerald-600 stroke-emerald-600 group-hover:stroke-emerald-500 dark:fill-emerald-400 dark:stroke-emerald-400':+ hasVoted,+ 'stroke-zinc-500': !hasVoted,+ },+ 'duration-300 group-hover:stroke-zinc-400',+ )}+ />+ <span+ className={clsx(+ {+ 'text-emerald-600 dark:text-emerald-400': hasVoted,+ 'text-zinc-500 group-hover:text-zinc-400': !hasVoted,+ },+ 'self-end text-lg duration-300',+ )}+ >+ {votes}+ </span>+ </Button>+ </span>+ </TooltipTrigger>+ <TooltipContent>+ <p>Login to Upvote</p>+ </TooltipContent>+ </Tooltip>+ </TooltipProvider></div><div className="prose-invert leading-8 prose-h3:text-xl">{/* @ts-ignore */}diff --git a/src/components/ui/tooltip.tsx b/src/components/ui/tooltip.tsxnew file mode 100644index 00000000..5500d646--- /dev/null+++ b/src/components/ui/tooltip.tsx@@ -0,0 +1,30 @@+"use client"++import * as React from "react"+import * as TooltipPrimitive from "@radix-ui/react-tooltip"++import { cn } from "~/utils/cn"++const TooltipProvider = TooltipPrimitive.Provider++const Tooltip = TooltipPrimitive.Root++const TooltipTrigger = TooltipPrimitive.Trigger++const TooltipContent = React.forwardRef<+ React.ElementRef<typeof TooltipPrimitive.Content>,+ React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>+>(({ className, sideOffset = 4, ...props }, ref) => (+ <TooltipPrimitive.Content+ ref={ref}+ sideOffset={sideOffset}+ className={cn(+ "z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",+ className+ )}+ {...props}+ />+))+TooltipContent.displayName = TooltipPrimitive.Content.displayName++export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }