Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix public share links #234

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-actors-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mexit-webapp': patch
---

Fix share note link of public space
5 changes: 5 additions & 0 deletions .changeset/smooth-mice-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mexit-webapp': patch
---

Fix public share links
54 changes: 42 additions & 12 deletions apps/webapp/src/Components/EditorInfobar/ShareOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const ShareOptions = ({ context, id }: ShareOptionsProps) => {
const [isLoading, setIsLoading] = useState(false)
const { makeNotePrivate, makeNotePublic, isPublic } = useApi()
// const { makeNamespacePublic, makeNamespacePrivate } = useNamespaceApi()
const { isNamespacePublic, makeNamespacePublic } = useNamespaces()
const { getNamespaceOfNodeid, isNamespacePublic, makeNamespacePublic } = useNamespaces()

const publicUrl = useMemo(() => {
if (context === 'note') {
Expand All @@ -56,6 +56,16 @@ const ShareOptions = ({ context, id }: ShareOptionsProps) => {
}
}, [id, isPublic, context, isNamespacePublic])

const noteNamespacePublicLink = useMemo(() => {
if (context === 'note') {
const namespace = getNamespaceOfNodeid(id)
return namespace && isNamespacePublic(namespace.id)
? apiURLs.namespaces.getPublicURLofNote(namespace.id, id)
: undefined
}
return undefined
}, [id, context])

// Helper function to set loading
const tryError = async (fn: () => Promise<void>) => {
try {
Expand Down Expand Up @@ -106,26 +116,46 @@ const ShareOptions = ({ context, id }: ShareOptionsProps) => {
<div style={{ display: 'flex', flex: 1, alignItems: 'center' }}>
<MexIcon color={theme.colors.primary} icon={globalLine} fontSize={24} margin="0 1rem 0 0" />
<div style={{ gap: '1', userSelect: 'none' }}>
<CardTitle>Make this {context} public?</CardTitle>
<ItemDesc>Publish and share the link with everyone!</ItemDesc>
{context === 'note' && noteNamespacePublicLink ? (
<>
<CardTitle>This note is in a public space.</CardTitle>
<ItemDesc>Share the public space link</ItemDesc>
</>
) : (
<>
<CardTitle>Make this {context} public?</CardTitle>
<ItemDesc>Publish and share the link with everyone!</ItemDesc>
</>
)}
</div>
</div>
<div style={{ display: 'flex', alignItems: 'center' }}>
<span style={{ marginRight: '.5rem' }}>
{publicUrl && (
<CopyButton text={publicUrl} size="20px" beforeCopyTooltip="Copy link" afterCopyTooltip="Link copied!" />
{noteNamespacePublicLink ? (
<CopyButton
text={noteNamespacePublicLink}
size="20px"
beforeCopyTooltip="Copy link"
afterCopyTooltip="Link copied!"
/>
) : (
publicUrl && (
<CopyButton text={publicUrl} size="20px" beforeCopyTooltip="Copy link" afterCopyTooltip="Link copied!" />
)
)}
</span>
{isLoading ? (
<Loading dots={3} transparent />
) : (
<ToggleButton
id="toggle-public"
value={!!publicUrl}
size="sm"
onChange={flipPublicAccess}
checked={!!publicUrl}
/>
noteNamespacePublicLink === undefined && (
<ToggleButton
id="toggle-public"
value={!!publicUrl}
size="sm"
onChange={flipPublicAccess}
checked={!!publicUrl}
/>
)
)}
</div>
</Container>
Expand Down
8 changes: 7 additions & 1 deletion apps/webapp/src/Views/PublicNamespaceView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function PublicNamespaceView() {
const [showLoader, setShowLoader] = useState(true)
const { setNamespace, setILinks, setCurrentNode } = usePublicNodeStore()
const namespaceID = useParams().namespaceID
const noteID = useParams().nodeId
const navigate = useNavigate()
const { getPublicNamespaceAPI } = useNamespaceApi()

Expand All @@ -30,7 +31,12 @@ function PublicNamespaceView() {
updatedAt: response.updatedAt
})

const firstNode = response.nodeHierarchy[0]
// Use noteID in path params if found, otherwise open the first note in hierarchy
const firstNode = noteID
? response.nodeHierarchy.find((node: any) => node.nodeid === noteID) ?? response.nodeHierarchy[0]
: response.nodeHierarchy[0]

// mog('firstNode', { firstNode, noteID })

setILinks(response.nodeHierarchy)
navigate(`node/${firstNode.nodeid}`)
Expand Down
4 changes: 3 additions & 1 deletion libs/core/src/Utils/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ export const apiURLs = {
share: `${MEXIT_BACKEND_URL_BASE}/namespace/share`,
getUsersOfShared: (id: string) => `${MEXIT_BACKEND_URL_BASE}/namespace/shared/${id}/users`,

getPublicURL: (id: string) => `${MEXIT_FRONTEND_URL_BASE}/share/namespace/${id}`
getPublicURL: (id: string) => `${MEXIT_FRONTEND_URL_BASE}/share/namespace/${id}`,
getPublicURLofNote: (namespaceid: string, noteid: string) =>
`${MEXIT_FRONTEND_URL_BASE}/share/namespace/${namespaceid}/node/${noteid}`
},

// Link Shortener URLs
Expand Down