Skip to content

Commit

Permalink
fix: Fixed the problem of not prompting after synchronizing documents
Browse files Browse the repository at this point in the history
  • Loading branch information
1943time committed Nov 27, 2023
1 parent 32576c8 commit 693813d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 54 deletions.
59 changes: 11 additions & 48 deletions src/renderer/src/server/Share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {CloseShare} from './ui/CloseShare'
import {ShareData} from './ui/ShareData'
import {action, runInAction} from 'mobx'
import {ServiceSet} from './ui/ServiceSet'
import {shareSuccessfully$, Successfully} from './ui/Successfully'

export const shareSuccess$ = new Subject<string>()
export const Share = observer(() => {
const [state, setState] = useLocalState({
popOpen: false,
Expand All @@ -45,81 +45,45 @@ export const Share = observer(() => {
})
}, [])

const copyDocUrl = useCallback((url: string) => {
window.api.copyToClipboard(url)
message$.next({
type: 'success',
content: 'Copied to clipboard'
})
}, [])

useEffect(() => {
if (state.popOpen) {
getBooks()
setState({refresh: !state.refresh})
}
}, [state.popOpen])

const [api, contextHolder] = notification.useNotification()

useSubject(shareSuccess$, (url: string) => {
shareSuccess(url)
})

const curDoc = useMemo(() => {
return shareStore.docMap.get(treeStore.openedNote?.filePath || '')
}, [treeStore.openNote, state.refresh])

const copyDocUrl = useCallback((url: string) => {
window.api.copyToClipboard(url)
message$.next({
type: 'success',
content: 'Copied to clipboard'
})
}, [])

const closeMask = useCallback(() => {
setTimeout(() => {
setState({mask: false})
})
}, [])

const shareSuccess = useCallback((url: string) => {
const key = nanoid()
api.success({
key,
message: 'Synchronization succeeded',
duration: 3,
btn: (
<Space>
<Button
onClick={() => {
api.destroy(key)
copyDocUrl(url)
}}
>
Copy Link
</Button>
<Button
type={'primary'}
onClick={() => {
window.open(url)
api.destroy(key)
}}
>
Open
</Button>
</Space>
)
})
}, [])

const share = useCallback(() => {
const note = treeStore.openedNote!
if (note && mediaType(note.filePath) === 'markdown') {
setState({syncing: true})
shareStore.shareDoc(note.filePath, treeStore.root?.filePath).then(res => {
setState({refresh: !state.refresh})
shareSuccess(`${shareStore.serviceConfig!.domain}/doc/${res.name}`)
shareSuccessfully$.next(`${shareStore.serviceConfig!.domain}/doc/${res.name}`)
}).finally(() => setState({syncing: false}))
}
}, [])

return (
<>
{contextHolder}
<Successfully/>
<Popover
zIndex={100}
content={(
Expand Down Expand Up @@ -236,7 +200,6 @@ export const Share = observer(() => {
books={state.books}
onCopy={copyDocUrl}
onOpenSetting={() => setState({mask: true, openSetting: true})}
onShareSuccess={shareSuccess}
onRefresh={getBooks}
onMask={mask => setState({mask})}
/>
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/src/server/ui/BookItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import {action} from 'mobx'
import {shareStore} from '../store'
import {NotLogged} from './NotLogged'
import {EBook} from './Ebook'
import {shareSuccessfully$} from './Successfully'

export const BookItem = observer((props: {
books: ShareBook[]
onCopy: (url: string) => void
onMask: (mask: boolean) => void
onRefresh: () => void
onShareSuccess: (url: string) => void
onOpenSetting: () => void
}) => {
const [state, setState] = useLocalState({
Expand Down Expand Up @@ -80,7 +80,7 @@ export const BookItem = observer((props: {
...b,
name: ''
}).then(() => {
props.onShareSuccess(`${shareStore.serviceConfig!.domain}/book/${b.path}`)
shareSuccessfully$.next(`${shareStore.serviceConfig!.domain}/book/${b.path}`)
}).finally(action(() => b.updating = false))
})}
>
Expand Down Expand Up @@ -162,7 +162,6 @@ export const BookItem = observer((props: {
closeMask()
}}
onSave={book => {
props.onShareSuccess(`${shareStore.serviceConfig!.domain}/book/${book.path}`)
props.onRefresh()
}}
/>
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/src/server/ui/Ebook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {useLocalState} from '../../hooks/useLocalState'
import {stat} from '../../utils'
import {AceCode} from '../../components/AceCode'
import {MainApi, openDialog} from '../../api/main'
import {shareSuccessfully$} from './Successfully'

export const EBook = observer((props: {
open: boolean
Expand Down Expand Up @@ -77,6 +78,7 @@ export const EBook = observer((props: {
})
if (res) {
props.onSave(res.book)
shareSuccessfully$.next(`${shareStore.serviceConfig!.domain}/book/${res.book.path}`)
props.onClose()
}
} finally {
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/src/server/ui/ShareData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import {default as BookIcon} from '../../icons/IBook'
import {IBook, IDevice, IDoc, IFile} from '../model'
import {useLocalState} from '../../hooks/useLocalState'
import {shareStore} from '../store'
import {shareSuccess$} from '../Share'
import {CloseShare} from './CloseShare'
import {message$, sizeUnit} from '../../utils'
import {ServiceSet} from './ServiceSet'
import {EBook} from './Ebook'
import {shareSuccessfully$} from './Successfully'

const Sync = observer((props: {
doc?: IDoc
Expand All @@ -33,14 +33,14 @@ const Sync = observer((props: {
try {
if (props.doc) {
const res = await shareStore.shareDoc(props.doc.filePath)
shareSuccess$.next(`${shareStore.serviceConfig!.domain}/doc/${res.name}`)
shareSuccessfully$.next(`${shareStore.serviceConfig!.domain}/doc/${res.name}`)
}
if (props.book) {
await shareStore.shareBook({
...props.book,
name: ''
})
shareSuccess$.next(`${shareStore.serviceConfig!.domain}/book/${props.book.path}`)
shareSuccessfully$.next(`${shareStore.serviceConfig!.domain}/book/${props.book.path}`)
}
} finally {
setState({syncing: false})
Expand Down
50 changes: 50 additions & 0 deletions src/renderer/src/server/ui/Successfully.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {observer} from 'mobx-react-lite'
import {useSubject} from '../../hooks/subscribe'
import {useCallback} from 'react'
import {nanoid} from 'nanoid'
import {Button, notification, Space} from 'antd'
import {Subject} from 'rxjs'
import {message$} from '../../utils'
export const shareSuccessfully$ = new Subject<string>()
export const Successfully = observer(() => {
const [api, contextHolder] = notification.useNotification()
const copyDocUrl = useCallback((url: string) => {
window.api.copyToClipboard(url)
message$.next({
type: 'success',
content: 'Copied to clipboard'
})
}, [])
useSubject(shareSuccessfully$, (url: string) => {
const key = nanoid()
api.success({
key,
message: 'Synchronization succeeded',
duration: 3,
btn: (
<Space>
<Button
onClick={() => {
api.destroy(key)
copyDocUrl(url)
}}
>
Copy Link
</Button>
<Button
type={'primary'}
onClick={() => {
window.open(url)
api.destroy(key)
}}
>
Open
</Button>
</Space>
)
})
})
return (
<>{contextHolder}</>
)
})

0 comments on commit 693813d

Please sign in to comment.