11<script setup lang="ts">
22import { Link , router } from ' @inertiajs/vue3' ;
3- import { Check , Eye , Heart , MessageSquare , Share2 } from ' lucide-vue-next' ;
3+ import {
4+ Check ,
5+ Eye ,
6+ Heart ,
7+ Loader2 ,
8+ MessageSquare ,
9+ Share2 ,
10+ } from ' lucide-vue-next' ;
411import { ref , watch } from ' vue' ;
512
613import VerifiedBadge from ' @/components/VerifiedBadge.vue' ;
714import { useAppreciation } from ' @/lib/useAppreciation' ;
815import { useAuth } from ' @/lib/useAuth' ;
16+ import { getCsrfToken } from ' @/lib/useCsrf' ;
917import { formatTimeAgo } from ' @/lib/useDate' ;
1018
1119interface User {
@@ -166,18 +174,92 @@ const toggleAppreciation = () => {
166174};
167175
168176const copied = ref (false );
177+ const isSharing = ref (false );
169178
170- const copyLink = async () => {
171- const url = ` ${window .location .origin }/blogs/${props .blog .slug } ` ;
179+ const shareBlog = async () => {
180+ if (isSharing .value ) {
181+ return ;
182+ }
183+
184+ isSharing .value = true ;
185+
186+ const originalUrl = ` ${window .location .origin }/blogs/${props .blog .slug } ` ;
187+ let finalUrl = originalUrl ;
172188
173189 try {
174- await navigator .clipboard .writeText (url );
190+ const res = await fetch (' /api/short-urls' , {
191+ method: ' POST' ,
192+ headers: {
193+ ' Content-Type' : ' application/json' ,
194+ Accept: ' application/json' ,
195+ ' X-XSRF-TOKEN' : getCsrfToken (),
196+ ' X-Requested-With' : ' XMLHttpRequest' ,
197+ },
198+ credentials: ' same-origin' ,
199+ body: JSON .stringify ({
200+ original_url: originalUrl ,
201+ }),
202+ });
203+
204+ if (res .ok ) {
205+ const data = await res .json ();
206+
207+ if (data .short_url ) {
208+ finalUrl = data .short_url ;
209+ }
210+ }
211+ } catch {
212+ // Fallback to original URL
213+ } finally {
214+ isSharing .value = false ;
215+ }
216+
217+ if (navigator .share ) {
218+ try {
219+ await navigator .share ({
220+ title: props .blog .title ,
221+ url: finalUrl ,
222+ });
223+ copied .value = true ;
224+ setTimeout (() => {
225+ copied .value = false ;
226+ }, 2500 );
227+
228+ return ;
229+ } catch {
230+ // fallback to clipboard
231+ }
232+ }
233+
234+ try {
235+ if (navigator .clipboard && window .isSecureContext ) {
236+ await navigator .clipboard .writeText (finalUrl );
237+ } else {
238+ throw new Error (' Clipboard API unavailable' );
239+ }
240+
175241 copied .value = true ;
176242 setTimeout (() => {
177243 copied .value = false ;
178- }, 2000 );
244+ }, 2500 );
179245 } catch {
180- // fallback
246+ try {
247+ const textarea = document .createElement (' textarea' );
248+ textarea .value = finalUrl ;
249+ textarea .style .position = ' fixed' ;
250+ textarea .style .opacity = ' 0' ;
251+ document .body .appendChild (textarea );
252+ textarea .focus ();
253+ textarea .select ();
254+ document .execCommand (' copy' );
255+ document .body .removeChild (textarea );
256+ copied .value = true ;
257+ setTimeout (() => {
258+ copied .value = false ;
259+ }, 2500 );
260+ } catch {
261+ // fallback failed
262+ }
181263 }
182264};
183265 </script >
@@ -327,7 +409,7 @@ const copyLink = async () => {
327409 <!-- Featured Image (Attached image preview below excerpt) -->
328410 <div
329411 v-if =" blog.featured_image || blog.featured_image_path"
330- class =" dark:bg-gray-850 mt-3 max-h-80 overflow-hidden rounded-xl border border-slate-200/80 bg-slate-50 dark:border-gray-800"
412+ class =" mt-3 max-h-80 overflow-hidden rounded-xl border border-slate-200/80 bg-slate-50 dark:border-gray-800 dark:bg-gray-800/50 "
331413 >
332414 <img
333415 :src =" blog.featured_image || blog.featured_image_path || ''"
@@ -355,7 +437,7 @@ const copyLink = async () => {
355437 :class =" [
356438 isReacted
357439 ? 'border-rose-200 bg-rose-50/90 text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/40 dark:text-rose-300'
358- : 'dark:bg-gray-850 border-slate-200/80 bg-slate-50/70 text-slate-700 hover:border-slate-300 hover:bg-slate-100 dark:border-gray-800 dark:text-gray-300 dark:hover:bg-gray-800',
440+ : 'border-slate-200/80 bg-slate-50/70 text-slate-700 hover:border-slate-300 hover:bg-slate-100 dark:border-gray-800 dark:bg-gray-800/60 dark:text-gray-300 dark:hover:bg-gray-800',
359441 ]"
360442 :title =" isReacted ? 'রিঅ্যাক্ট সরান' : 'লাভ রিঅ্যাক্ট দিন'"
361443 >
@@ -372,7 +454,7 @@ const copyLink = async () => {
372454
373455 <Link
374456 :href =" ` /blogs/${blog .slug }#comments ` "
375- class="dark: bg-gray-850 inline-flex items-center gap-1.5 rounded-xl border border-slate-200/80 bg-slate-50/70 px-2.5 py-1 text-xs font-semibold text-slate-700 transition hover:border-slate-300 hover:bg-slate-100 dark:border-gray-800 dark:text-gray-300 dark:hover :bg-gray-800 "
457+ class="inline-flex items-center gap-1.5 rounded-xl border border-slate-200/80 bg-slate-50/70 px-2.5 py-1 text-xs font-semibold text-slate-700 transition hover:border-slate-300 hover:bg-slate-100 dark:border-gray-800 dark: bg-gray-800 /60 dark:text-gray-300 dark:hover :bg-gray-800 "
376458 >
377459 <MessageSquare
378460 class="h-3.5 w-3.5 text-purple-600 dark:text-purple-400 "
@@ -392,19 +474,26 @@ const copyLink = async () => {
392474 <div class =" flex items-center gap-2" @click.stop >
393475 <button
394476 type =" button"
395- @click =" copyLink"
396- class =" dark:bg-gray-850 inline-flex cursor-pointer items-center gap-1.5 rounded-xl border border-slate-200/80 bg-slate-50/70 px-3 py-1.5 text-xs font-semibold text-slate-600 transition hover:border-slate-300 hover:bg-slate-100 dark:border-gray-800 dark:text-gray-300 dark:hover:bg-gray-800"
477+ @click =" shareBlog"
478+ :disabled =" isSharing"
479+ class =" inline-flex cursor-pointer items-center gap-1.5 rounded-xl border border-slate-200/80 bg-slate-50/70 px-3 py-1.5 text-xs font-semibold text-slate-600 transition hover:border-slate-300 hover:bg-slate-100 disabled:cursor-wait disabled:opacity-75 dark:border-gray-800 dark:bg-gray-800/60 dark:text-gray-300 dark:hover:bg-gray-800"
397480 title =" Share blog"
398481 >
482+ <Loader2
483+ v-if =" isSharing "
484+ class="h-3.5 w-3.5 animate-spin text-indigo-600 dark:text-indigo-400 "
485+ />
399486 <Check
400- v-if =" copied "
487+ v-else- if =" copied "
401488 class="h-3.5 w-3.5 text-emerald-600 dark:text-emerald-400 "
402489 />
403490 <Share2
404491 v-else
405492 class="h-3.5 w-3.5 text-slate-500 dark:text-gray-400 "
406493 />
407- <span >{{ copied ? 'Copied' : 'Share' }}</span >
494+ <span >{{
495+ isSharing ? 'Copying...' : copied ? 'Copied' : 'Share'
496+ }}</span >
408497 </button >
409498 </div >
410499 </div >
0 commit comments