File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import debounce from "@/helpers/debounce";
1313import editorLanguageOptions from "@/data/languages" ;
1414import editorThemeOptions from "@/data/themes" ;
1515import { getSession } from "@/service/api/session" ;
16+ import { shortenUrl } from "@/service/shortener" ;
1617import toast from "react-hot-toast" ;
1718import useSocket from "@/hooks/useSocket" ;
1819
@@ -51,9 +52,12 @@ export default function ShareCode() {
5152 } , [ socket , sessionID ] ) ;
5253
5354 const handleCopyURL = async ( ) => {
55+ const url = window . location . href ;
56+ const shortenedUrl = await shortenUrl ( url ) ;
57+
5458 const type = "text/plain" ;
5559 const clipboardItemData = {
56- [ type ] : window . location . href
60+ [ type ] : shortenedUrl
5761 } ;
5862 const clipboardItem = new ClipboardItem ( clipboardItemData ) ;
5963
Original file line number Diff line number Diff line change @@ -2,10 +2,12 @@ const NODE_ENV = process.env.NODE_ENV;
22const PORT = Number ( process . env . PORT ) || 3023 ;
33const API_URL = process . env . API_URL || "http://localhost:3123/api" ;
44const WS_SERVER_URL = process . env . WS_SERVER_URL || "ws://localhost:3123" ;
5+ const SHORTENER_API_URL = process . env . SHORTENER_API_URL || "http://localhost:3024/api" ;
56
67export const env = {
78 NODE_ENV ,
89 PORT ,
910 API_URL ,
10- WS_SERVER_URL
11+ WS_SERVER_URL ,
12+ SHORTENER_API_URL
1113} ;
Original file line number Diff line number Diff line change 1+ import axios from "axios" ;
2+ import { env } from "@/common/env" ;
3+
4+ const shortenerApi = axios . create ( {
5+ baseURL : env . SHORTENER_API_URL
6+ } ) ;
7+
8+ const authenticate = async ( ) => {
9+ const { data, status } = await shortenerApi . get ( "/auth" ) ;
10+
11+ if ( status === 200 && data . accessToken ) {
12+ shortenerApi . defaults . headers . common [ "Authorization" ] = `Bearer ${ data . accessToken } ` ;
13+ }
14+ } ;
15+
16+ export const shortenUrl = async ( url : string ) => {
17+ try {
18+ if ( ! shortenerApi . defaults . headers . common [ "Authorization" ] ) {
19+ await authenticate ( ) ;
20+ }
21+
22+ const { data, status } = await shortenerApi . post ( "/url/shorten" , { url } ) ;
23+
24+ if ( status === 200 && data . url ) {
25+ return data . url ;
26+ }
27+ } catch {
28+ return url ;
29+ }
30+ } ;
You can’t perform that action at this time.
0 commit comments