Connect, collaborate, and grow with a community of TypeScript developers
feat: add short url to share submission button (#1554)
Ayush Kumar Yadave0fd9a17a054
diff --git a/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/_components/overview.tsx b/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/_components/overview.tsxindex 11ea9af3..4b570dd0 100644--- a/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/_components/overview.tsx+++ b/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/_components/overview.tsx@@ -1,15 +1,22 @@import { Button } from '@repo/ui/components/button';import { Markdown } from '@repo/ui/components/markdown';-import { toast } from '@repo/ui/components/use-toast';import { CheckCircle2, Plus, Share, Twitter, X, XCircle } from '@repo/ui/icons';import { useQuery } from '@tanstack/react-query';-import lzstring from 'lz-string';import Link from 'next/link';import { useParams, useSearchParams } from 'next/navigation';import { getRelativeTimeStrict } from '~/utils/relativeTime';import { AOT_CHALLENGES } from '../../../aot-slugs';import { getChallengeSubmissionById } from '../getChallengeSubmissions';import { Suggestions } from './suggestions';+import {+ Dialog,+ DialogContent,+ DialogHeader,+ DialogTitle,+ DialogTrigger,+} from '@repo/ui/components/dialog';+import { Tooltip, TooltipContent, TooltipTrigger } from '@repo/ui/components/tooltip';+import { ShareUrl } from '~/components/share-url';interface Props {submissionId: string;@@ -35,31 +42,6 @@ export function SubmissionOverview({ submissionId }: Props) {const track = searchParams.get('slug');- const copyShareLinkToClipboard = async () => {- if (!navigator.clipboard || !submission) {- toast({- variant: 'destructive',- description: 'Could not copy to clipboard',- });-- // should never happen- if (!submission) console.error("can't share - no submission found, eh?");-- return;- }-- let url = `${window.location.origin}/challenge/${slug}`;-- const compressedCode = lzstring.compressToEncodedURIComponent(submission.code);- url += `?code=${compressedCode}`;-- await navigator.clipboard.writeText(url).catch(console.error);- toast({- variant: 'success',- description: 'Share link copied!',- });- };-const tweet = isAotChallenge? `https://twitter.com/intent/tweet?text=${encodeURIComponent(`I've completed ${submission?.challenge.name} - Advent of TypeScript 2023`,@@ -118,14 +100,36 @@ export function SubmissionOverview({ submissionId }: Props) {<Markdown>{code}</Markdown></div><div className="mb-3 flex gap-2 px-3">- <Button- className="flex items-center gap-2 rounded-xl border-2 px-4 py-2 dark:text-white"- variant="outline"- onClick={copyShareLinkToClipboard}- >- <Share className="h-4 w-4" />- Share Code on Playground- </Button>+ <Dialog>+ <DialogTrigger>+ <Tooltip>+ <TooltipTrigger asChild>+ <Button+ className="flex items-center gap-2 rounded-xl border-2 px-4 py-2 dark:text-white"+ variant="outline"+ >+ <Share className="h-4 w-4" />+ Share Code on Playground+ </Button>+ </TooltipTrigger>+ <TooltipContent>+ <p>Share Challenge with Code</p>+ </TooltipContent>+ </Tooltip>+ </DialogTrigger>+ <DialogContent>+ <DialogHeader>+ <DialogTitle>Share</DialogTitle>+ </DialogHeader>+ <div className="pt-4">+ <ShareUrl+ isChallenge+ code={submission.code}+ desciprtion="Click Copy to Share code on Playground."+ />+ </div>+ </DialogContent>+ </Dialog><ButtonasChildclassName="flex items-center gap-2 rounded-xl border-2 px-4 py-2 dark:text-white"diff --git a/apps/web/src/components/share-url.tsx b/apps/web/src/components/share-url.tsxindex 9eb6022f..fcaa6f9f 100644--- a/apps/web/src/components/share-url.tsx+++ b/apps/web/src/components/share-url.tsx@@ -12,36 +12,39 @@ import { useToast } from '@repo/ui/components/use-toast';interface ShareShortUrlProps {desciprtion?: string;isChallenge?: boolean;+ code?: string;+ longUrl?: string;}-export function ShareUrl({ desciprtion, isChallenge = false }: ShareShortUrlProps) {+export function ShareUrl({ desciprtion, isChallenge = false, code, longUrl }: ShareShortUrlProps) {const { slug } = useParams();const { toast } = useToast();const shareType = isChallenge ? 'challenge' : 'playground';const [state, setState] = useState<'copied' | 'idle' | 'loading'>('idle');- // enable copy with code by default on playground- const [copyWithCode, setCopyWithCode] = useState(shareType === 'playground');- const [codeToCompress] = useLocalStorage(`${shareType}-${slug}`, '');+ // enable copy with code by default on playground, or we are provided with code+ const [copyWithCode, setCopyWithCode] = useState(shareType === 'playground' || Boolean(code));+ const [localStorageCode] = useLocalStorage(`${shareType}-${slug}`, '');const [shortUrl, setShortUrl] = useState('');const [previousCode, setPreviousCode] = useState('');- const url = `${window.location.origin}/${shareType}/${slug}`;+ const url = longUrl ?? `${window.location.origin}/${shareType}/${slug}`;const genShortUrl = useCallback(async () => {let long = url;- if (copyWithCode && codeToCompress) {- const compressedCode = lzstring.compressToEncodedURIComponent(codeToCompress);+ const fullCode = code ?? localStorageCode;+ if (copyWithCode && fullCode) {+ const compressedCode = lzstring.compressToEncodedURIComponent(fullCode);long += `?code=${compressedCode}`;// Check if the code has changed- if (codeToCompress !== previousCode) {+ if (fullCode !== previousCode) {const short = await createShortURL(long);- setPreviousCode(codeToCompress);+ setPreviousCode(fullCode);return short ?? long;}return shortUrl;}return long;- }, [url, copyWithCode, codeToCompress, previousCode, shortUrl]);+ }, [url, code, localStorageCode, copyWithCode, previousCode, shortUrl]);const copyToClipboard = useCallback(async () => {setState('loading');@@ -78,12 +81,12 @@ export function ShareUrl({ desciprtion, isChallenge = false }: ShareShortUrlProparia-disabled={state === 'loading'}className="break-all rounded-lg border border-black/20 bg-white px-4 py-2 aria-disabled:cursor-not-allowed aria-disabled:opacity-50 dark:border-white/20 dark:bg-black">- {shortUrl.length === 0 ? url : shortUrl}+ {shortUrl.length === 0 ? (code ? 'Copy to Generate URL!' : url) : shortUrl}</code><DialogFooter><div className="flex items-center justify-end gap-2">- {/* Checkbox not needed in playground */}- {codeToCompress && isChallenge ? (+ {/* Checkbox not needed in playground, or we already are providing code */}+ {localStorageCode && isChallenge && !code ? (<div className="flex h-fit items-center justify-center gap-2 text-xs"><Checkboxid="copy-with-code"