1+ import { captureException } from '@sentry/nextjs' ;
12import Head from 'next/head' ;
23import { useSearchParams } from 'next/navigation' ;
34import { useRouter } from 'next/router' ;
4- import { ReactElement , useEffect } from 'react' ;
5+ import { ReactElement , useCallback , useEffect } from 'react' ;
56
67import { Loading } from '@/components' ;
7- import { LinkReach , useCreateDoc } from '@/features/docs/doc-management' ;
8+ import {
9+ LinkReach ,
10+ LinkRole ,
11+ useCreateDoc ,
12+ } from '@/features/docs/doc-management' ;
813import { useUpdateDocLink } from '@/features/docs/doc-share/api/useUpdateDocLink' ;
914import { useSkeletonStore } from '@/features/skeletons' ;
1015import { MainLayout } from '@/layouts' ;
@@ -15,8 +20,8 @@ const Page: NextPageWithLayout = () => {
1520 const router = useRouter ( ) ;
1621 const searchParams = useSearchParams ( ) ;
1722 const linkReach = searchParams . get ( 'link-reach' ) ;
18- const linkTitle = searchParams . get ( 'title ' ) ;
19- const linkpermission = searchParams . get ( 'linkpermission ' ) ;
23+ const linkRole = searchParams . get ( 'link-role ' ) ;
24+ const title = searchParams . get ( 'title ' ) ;
2025 const peoplesharing = searchParams . get ( 'peoplesharing' ) ;
2126
2227 const {
@@ -25,49 +30,75 @@ const Page: NextPageWithLayout = () => {
2530 data : doc ,
2631 } = useCreateDoc ( {
2732 onSuccess : ( doc ) => {
28- if ( linkReach || linkpermission || peoplesharing ) {
33+ if ( ( linkReach && linkRole ) || linkReach || peoplesharing ) {
2934 return ;
3035 }
3136
32- router
33- . push ( `/docs/${ doc . id } ` )
34- . then ( ( ) => { } )
35- . catch ( ( ) => { } ) ;
37+ redirectToDoc ( doc . id ) ;
3638 } ,
3739 onError : ( ) => { } ,
3840 } ) ;
3941
40- const { mutate : updateDocLink } = useUpdateDocLink ( ) ;
42+ const { mutate : updateDocLink } = useUpdateDocLink ( {
43+ onSuccess : ( response , params ) => {
44+ if ( peoplesharing || ! params . id ) {
45+ return ;
46+ }
47+
48+ redirectToDoc ( params . id ) ;
49+ } ,
50+ onError : ( error , params ) => {
51+ captureException ( error , {
52+ extra : {
53+ docId : params . id ,
54+ linkReach,
55+ linkRole,
56+ } ,
57+ } ) ;
58+
59+ if ( params . id ) {
60+ redirectToDoc ( params . id ) ;
61+ }
62+ } ,
63+ } ) ;
64+
65+ const redirectToDoc = useCallback (
66+ ( docId : string ) => {
67+ void router . push ( `/docs/${ docId } ` ) ;
68+ } ,
69+ [ router ] ,
70+ ) ;
4171
4272 useEffect ( ( ) => {
4373 setIsSkeletonVisible ( true ) ;
4474 } , [ setIsSkeletonVisible ] ) ;
4575
76+ // Doc creation effect
4677 useEffect ( ( ) => {
4778 if ( doc ) {
4879 return ;
4980 }
5081
51- createDoc ( ) ;
52- } , [ createDoc , doc ] ) ;
82+ createDoc ( {
83+ title : title || undefined ,
84+ } ) ;
85+ } , [ createDoc , doc , title ] ) ;
5386
5487 useEffect ( ( ) => {
5588 if ( ! linkReach || ! doc ) {
5689 return ;
5790 }
5891
59- console . log ( 'linkReach' , linkReach ) ;
60-
61- if ( ! Object . values ( LinkReach ) . includes ( linkReach as LinkReach ) ) {
62- throw new Error ( 'Invalid link reach value' ) ;
63- }
64-
6592 updateDocLink ( {
6693 id : doc . id ,
6794 link_reach : linkReach as LinkReach ,
68- // link_role: linkRole,
95+ link_role : ( linkRole as LinkRole | undefined ) || undefined ,
6996 } ) ;
70- } , [ linkReach , doc , updateDocLink ] ) ;
97+ } , [ linkReach , doc , updateDocLink , redirectToDoc , linkRole ] ) ;
98+
99+ if ( ! linkReach && linkRole ) {
100+ console . warn ( 'link-reach parameter is missing' ) ;
101+ }
71102
72103 return < Loading /> ;
73104} ;
0 commit comments