Skip to content

Commit

Permalink
Merge pull request #308 from briefercloud/collapsed-sidebar-link
Browse files Browse the repository at this point in the history
allow share links with collapsible sidebar param
  • Loading branch information
lucasfcosta authored Jan 6, 2025
2 parents a927ac4 + e139ca5 commit 1376cbc
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { broadcastDocument } from '../../../../../websocket/workspace/documents.
const DocumentSettings = z.object({
runUnexecutedBlocks: z.boolean().optional(),
runSQLSelection: z.boolean().optional(),
shareLinksWithoutSidebar: z.boolean().optional(),
})

export default function settingsRouter(socketServer: IOServer) {
Expand All @@ -22,13 +23,18 @@ export default function settingsRouter(socketServer: IOServer) {
return
}

const { runUnexecutedBlocks, runSQLSelection } = body.data
const { runUnexecutedBlocks, runSQLSelection, shareLinksWithoutSidebar } =
body.data

try {
const doc = await recoverFromNotFound(
prisma().document.update({
where: { id: documentId },
data: { runUnexecutedBlocks, runSQLSelection },
data: {
runUnexecutedBlocks,
runSQLSelection,
shareLinksWithoutSidebar,
},
})
)
if (!doc) {
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/Dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ export default function Dashboard(props: Props) {
)
}, [props.publish, props.publishing])

const shareLinkWithoutSidebar = props.document.shareLinksWithoutSidebar
const copyLink = useMemo(
() =>
`${NEXT_PUBLIC_PUBLIC_URL()}/workspaces/${
props.document.workspaceId
}/documents/${props.document.id}`,
[router]
}/documents/${props.document.id}/dashboard${shareLinkWithoutSidebar ? '?sidebarCollapsed=true' : ''}`,
[props.document.workspaceId, props.document.id, shareLinkWithoutSidebar]
)

const documentTitle = useMemo(
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/components/PageSettingsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export default function PageSettingsPanel(props: Props) {
enabled={document?.runSQLSelection ?? false}
onToggle={api.toggleRunSQLSelection}
/>
<PageSettingToggle
name="Share links without sidebar"
description="Whether the 'copy link' button should include a query parameter to hide the sidebar (open dashboards with the sidebar collapsed)."
enabled={document?.shareLinksWithoutSidebar ?? false}
onToggle={api.toggleShareLinksWithoutSidebar}
/>
</div>
</div>
</Transition>
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/PrivateDocumentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,13 @@ function PrivateDocumentPageInner(
}, [setSelectedSidebar])

const router = useRouter()
const shareLinkWithoutSidebar = props.document.shareLinksWithoutSidebar
const copyLink = useMemo(
() =>
`${NEXT_PUBLIC_PUBLIC_URL()}/workspaces/${props.workspaceId}/documents/${
props.documentId
}`,
[router]
}/notebook${shareLinkWithoutSidebar ? `?sidebarCollapsed=true` : ''}`,
[props.workspaceId, props.documentId, shareLinkWithoutSidebar]
)

const isViewer = props.user.roles[props.workspaceId] === 'viewer'
Expand Down
26 changes: 25 additions & 1 deletion apps/web/src/hooks/useDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type API = {
publish: () => Promise<void>
toggleRunUnexecutedBlocks: () => Promise<void>
toggleRunSQLSelection: () => Promise<void>
toggleShareLinksWithoutSidebar: () => Promise<void>
}

type UseDocument = [
Expand Down Expand Up @@ -76,17 +77,40 @@ function useDocument(workspaceId: string, documentId: string): UseDocument {
api.updateDocumentSettings,
])

const toggleShareLinksWithoutSidebar = useCallback(async () => {
const newShareLinksWithoutSidebar = !document?.shareLinksWithoutSidebar
try {
await api.updateDocumentSettings(documentId, {
shareLinksWithoutSidebar: newShareLinksWithoutSidebar,
})
} catch (err) {
alert('Failed to update document settings')
}
}, [
workspaceId,
documentId,
document?.shareLinksWithoutSidebar,
api.updateDocumentSettings,
])

return useMemo(
() => [
{ document, loading, publishing },
{ setIcon, publish, toggleRunUnexecutedBlocks, toggleRunSQLSelection },
{
setIcon,
publish,
toggleRunUnexecutedBlocks,
toggleRunSQLSelection,
toggleShareLinksWithoutSidebar,
},
],
[
loading,
setIcon,
publish,
toggleRunUnexecutedBlocks,
toggleRunSQLSelection,
toggleShareLinksWithoutSidebar,
]
)
}
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/hooks/useDocuments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function upsertDocumentInMemory(
userAppClock: {},
runUnexecutedBlocks: false,
runSQLSelection: false,
shareLinksWithoutSidebar: true,
hasDashboard: false,
})
}
Expand Down Expand Up @@ -207,7 +208,11 @@ type API = {
) => Promise<void>
updateDocumentSettings: (
id: string,
settings: { runUnexecutedBlocks?: boolean; runSQLSelection?: boolean }
settings: {
runUnexecutedBlocks?: boolean
runSQLSelection?: boolean
shareLinksWithoutSidebar?: boolean
}
) => Promise<void>
publish: (id: string) => Promise<void>
}
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/hooks/useSideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useContext,
useState,
} from 'react'
import { useStringQuery } from './useQueryArgs'

type Context = [boolean, Dispatch<SetStateAction<boolean>>]
const Context = createContext<Context>([true, () => {}] as unknown as Context)
Expand All @@ -14,7 +15,8 @@ export default function useSideBar(): Context {
}

export function SideBarProvider({ children }: { children: React.ReactNode }) {
const value = useState(true)
const noSidebarQs = useStringQuery('sidebarCollapsed')
const value = useState(noSidebarQs === 'true')

return <Context.Provider value={value}>{children}</Context.Provider>
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ export default function DashboardPage() {
if (document.publishedAt === null) {
if (!role || role === 'viewer') {
// viewers can't see dashboard in edit mode
router.replace(`/workspaces/${workspaceId}/documents/${documentId}`)
router.replace(
`/workspaces/${workspaceId}/documents/${documentId}${location.search}`
)
} else {
router.replace(
`/workspaces/${workspaceId}/documents/${documentId}/dashboard/edit`
`/workspaces/${workspaceId}/documents/${documentId}/dashboard/edit${location.search}`
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ function PrivateDocumentPage(props: PrivateDocumentPageProps) {

if (document.publishedAt === null) {
router.replace(
`/workspaces/${props.workspaceId}/documents/${props.documentId}/notebook/edit`
`/workspaces/${props.workspaceId}/documents/${props.documentId}/notebook/edit${location.search}`
)
}

if (document.hasDashboard) {
router.replace(
`/workspaces/${props.workspaceId}/documents/${props.documentId}/dashboard`
`/workspaces/${props.workspaceId}/documents/${props.documentId}/dashboard${location.search}`
)
} else {
router.replace(
`/workspaces/${props.workspaceId}/documents/${props.documentId}/notebook`
`/workspaces/${props.workspaceId}/documents/${props.documentId}/notebook${location.search}`
)
}
}, [document, loading, props.workspaceId, props.documentId, props.user])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function Notebook(props: Props) {

if (document.publishedAt === null) {
router.replace(
`/workspaces/${props.workspaceId}/documents/${props.documentId}/notebook/edit`
`/workspaces/${props.workspaceId}/documents/${props.documentId}/notebook/edit${location.search}`
)
}
}, [document, loading, props.user])
Expand Down
10 changes: 10 additions & 0 deletions docs/settings/page-settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ When you run a SQL block, Briefer will run its entire content by default.
If you only want to run a specific part of the SQL block, you need to enable the "Run selected SQL only" switch.

After enabling this setting, Briefer will only run the SQL code you've selected. If you haven't selected any code, Briefer will run the entire block.

## Share links without sidebar

By default, clicking the "copy link" element within the "Share" menu will copy a link that opens the page with the sidebar _collapsed_.

If you want to share a link that opens the page with the sidebar _expanded_, you can disable the "Share links without sidebar" switch.

<Note>
The query parameter which controls the sidebar state is `sidebarCollapsed`. You can also manually set it to `true` or `false` to control the sidebar state.
</Note>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Document" ADD COLUMN "shareLinksWithoutSidebar" BOOLEAN NOT NULL DEFAULT true;
5 changes: 3 additions & 2 deletions packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ model Document {
reusableComponents ReusableComponent[]
reusableComponentInstances ReusableComponentInstance[]
runUnexecutedBlocks Boolean @default(false)
runSQLSelection Boolean @default(false)
runUnexecutedBlocks Boolean @default(false)
runSQLSelection Boolean @default(false)
shareLinksWithoutSidebar Boolean @default(false)
}

model YjsDocument {
Expand Down

0 comments on commit 1376cbc

Please sign in to comment.