1- import { Button , Select , SelectItem } from "@heroui/react" ;
1+ import { Button , Input , Select , SelectItem } from "@heroui/react" ;
22import ModalWrapper from "./modal-wrapper" ;
33import { useSearchParams } from "next/navigation" ;
44import QRDisplay from "../misc/qr-display" ;
55import Tabs from "../misc/tabs" ;
66import { TabItem } from "@/lib/types" ;
7- import { useEffect , useState } from "react" ;
7+ import { useState } from "react" ;
88import Icon from "../misc/icon" ;
99import toast from "react-hot-toast" ;
10- import { useAuth } from "@/lib/hooks/use-auth " ;
10+ import useSWR from "swr " ;
1111
1212export default function SharingModal ( {
1313 isOpen,
@@ -36,35 +36,57 @@ export default function SharingModal({
3636 ) ;
3737
3838 const visibilityOptions = [ "public" , "unlisted" , "private" ] ;
39- const [ selectedVisibility , setSelectedVisibility ] = useState <
40- string | undefined
41- > ( undefined ) ;
42-
43- const { session } = useAuth ( ) ;
44- const [ isDeveloper , setIsDeveloper ] = useState ( false ) ;
45-
46- // Load app visibility
47- useEffect ( ( ) => {
48- async function getVisibility ( ) {
49- if ( ! app ) return ;
50-
51- const url = new URL ( `https://pulse-editor.com/api/extension/get` ) ;
52- url . searchParams . append ( "app" , app ) ;
53- url . searchParams . append ( "latest" , "true" ) ;
5439
40+ const {
41+ data : shareInfo ,
42+ mutate,
43+ isLoading : isLoadingShareInfo ,
44+ } = useSWR <
45+ | {
46+ visibility : string ;
47+ canEdit : boolean ;
48+ inviteCode ?: string ;
49+ }
50+ | undefined
51+ > (
52+ app
53+ ? `${ process . env . NEXT_PUBLIC_BACKEND_URL } /api/extension/get-share-info?name=${ app } `
54+ : null ,
55+ async ( url : URL ) => {
5556 const res = await fetch ( url , {
5657 credentials : "include" ,
5758 } ) ;
5859
60+ if ( ! res . ok ) {
61+ toast . error ( "Failed to fetch extension share info" ) ;
62+ return undefined ;
63+ }
64+
5965 const data : {
6066 visibility : string ;
61- } [ ] = await res . json ( ) ;
62- if ( data ) {
63- const ext = data [ 0 ] ;
64- setSelectedVisibility ( ext . visibility ) ;
65- }
66- }
67- } , [ ] ) ;
67+ canEdit : boolean ;
68+ inviteCode ?: string ;
69+ } = await res . json ( ) ;
70+
71+ return data ;
72+ } ,
73+ ) ;
74+
75+ async function updateShareInfo ( visibility : string ) {
76+ const url = new URL (
77+ `${ process . env . NEXT_PUBLIC_BACKEND_URL } /api/extension/update` ,
78+ ) ;
79+ await fetch ( url , {
80+ method : "PATCH" ,
81+ body : JSON . stringify ( {
82+ visibility,
83+ name : app ,
84+ } ) ,
85+ credentials : "include" ,
86+ } ) ;
87+
88+ mutate ( ) ;
89+ }
6890
6991 return (
7092 < ModalWrapper
@@ -79,19 +101,25 @@ export default function SharingModal({
79101 label = "Visibility"
80102 placeholder = "Select visibility"
81103 onChange = { ( e ) => {
82- setSelectedVisibility ( e . target . value ) ;
104+ updateShareInfo ( e . target . value ) ;
83105 } }
84- isDisabled = { ! isDeveloper }
106+ isDisabled = { ! shareInfo ?. canEdit }
107+ isLoading = { isLoadingShareInfo }
108+ selectedKeys = { shareInfo ?. visibility ? [ shareInfo . visibility ] : [ ] }
85109 >
86110 { visibilityOptions . map ( ( option ) => (
87111 < SelectItem key = { option } > { option } </ SelectItem >
88112 ) ) }
89113 </ Select >
90114 ) }
91115
92- { ! selectedVisibility && app ? (
116+ { app && shareInfo ?. canEdit && shareInfo . visibility === "unlisted" && (
117+ < Input value = { shareInfo . inviteCode } label = "Invite Code" readOnly />
118+ ) }
119+
120+ { ! shareInfo ?. visibility && app ? (
93121 < p > Select a visibility option to see sharing options.</ p >
94- ) : selectedVisibility === "private" && app ? (
122+ ) : shareInfo ?. visibility === "private" && app ? (
95123 < p > Your workspace is private.</ p >
96124 ) : (
97125 < >
@@ -106,7 +134,9 @@ export default function SharingModal({
106134 < p className = "text-content4-foreground text-sm" >
107135 Share your workspace via this QR Code
108136 </ p >
109- < QRDisplay url = { window . location . href } />
137+ < QRDisplay
138+ url = { `${ window . location . origin } ?app=${ app } ${ shareInfo ?. inviteCode ? `&inviteCode=${ shareInfo . inviteCode } ` : "" } ` }
139+ />
110140 </ div >
111141 ) }
112142
@@ -116,11 +146,13 @@ export default function SharingModal({
116146 Share your workspace via this URL
117147 </ p >
118148
119- < p className = "font-bold break-all" > { window . location . href } </ p >
149+ < p className = "font-bold break-all" > { ` ${ window . location . origin } ?app= ${ app } ${ shareInfo ?. inviteCode ? `&inviteCode= ${ shareInfo . inviteCode } ` : "" } ` } </ p >
120150 < Button
121151 color = "primary"
122152 onPress = { ( ) => {
123- navigator . clipboard . writeText ( window . location . href ) ;
153+ navigator . clipboard . writeText (
154+ `${ window . location . origin } ?app=${ app } ${ shareInfo ?. inviteCode ? `&inviteCode=${ shareInfo . inviteCode } ` : "" } ` ,
155+ ) ;
124156 toast . success ( "URL copied to clipboard" ) ;
125157 } }
126158 >
0 commit comments