Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Route validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Feb 7, 2024
1 parent c122af1 commit 471c6ef
Show file tree
Hide file tree
Showing 27 changed files with 182 additions and 120 deletions.
18 changes: 13 additions & 5 deletions frontend/apps/desktop/src/app-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {NavRoute} from '@mintter/app/utils/navigation'
import {resolveHmIdToAppRoute} from '@mintter/app/utils/navigation'
import {NavRoute, defaultRoute, navRouteSchema} from '@mintter/app/utils/routes'
import type {AppWindowEvent} from '@mintter/app/utils/window-events'
import {observable} from '@trpc/server/observable'
import {
Expand Down Expand Up @@ -71,12 +71,20 @@ info('App UserData: ', userDataPath)

export function openInitialWindows() {
const windowsState = getWindowsState()
if (!Object.keys(windowsState).length) {
trpc.createAppWindow({routes: [{key: 'documents'}]})
const validWindowEntries = Object.entries(windowsState).filter(
([windowId, window]) => {
if (window.routes.length === 0) return false
return window.routes.every((route) => {
return navRouteSchema.safeParse(route).success
})
},
)
if (!validWindowEntries.length) {
trpc.createAppWindow({routes: [defaultRoute]})
return
}
try {
Object.entries(windowsState).forEach(([windowId, window]) => {
validWindowEntries.forEach(([windowId, window]) => {
trpc.createAppWindow({
routes: window.routes,
routeIndex: window.routeIndex,
Expand All @@ -87,7 +95,7 @@ export function openInitialWindows() {
})
} catch (error) {
info(`[MAIN]: openInitialWindows Error: ${JSON.stringify(error)}`)
trpc.createAppWindow({routes: [{key: 'documents'}]})
trpc.createAppWindow({routes: [defaultRoute]})
return
}
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/apps/desktop/src/app-menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// this menu is visible on macOS only
// the keyboard shortcuts apply to every platform

import {defaultRoute} from '@mintter/app/utils/routes'
import {Menu, MenuItem} from 'electron'
import {dispatchFocusedWindowAppEvent, openRoute, trpc} from './app-api'

Expand Down Expand Up @@ -65,7 +66,7 @@ export function createAppMenu() {
label: 'New Window',
accelerator: 'CmdOrCtrl+Shift+n',
click: () => {
trpc.createAppWindow({routes: [{key: 'documents'}]})
trpc.createAppWindow({routes: [defaultRoute]})
},
},
{type: 'separator'},
Expand Down Expand Up @@ -106,7 +107,7 @@ export function createAppMenu() {
label: 'Documents',
accelerator: 'CmdOrCtrl+1',
click: () => {
openRoute({key: 'documents'})
openRoute({key: 'documents', tab: 'trusted'})
},
},
{
Expand Down
5 changes: 3 additions & 2 deletions frontend/apps/desktop/src/app-windows.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import appError from '@mintter/app/errors'
import type {NavRoute, NavState} from '@mintter/app/utils/navigation'
import type {NavState} from '@mintter/app/utils/navigation'
import {NavRoute} from '@mintter/app/utils/routes'
import type {AppWindowEvent} from '@mintter/app/utils/window-events'
import {getRouteWindowType} from '@mintter/app/utils/window-types'
import {BrowserWindow, app, nativeTheme} from 'electron'
Expand Down Expand Up @@ -73,7 +74,7 @@ let windowsState =
({} as Record<string, AppWindow>)

export function getWindowsState() {
return windowsState
return windowsState || {}
}

function getAWindow() {
Expand Down
5 changes: 3 additions & 2 deletions frontend/apps/desktop/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {defaultRoute} from '@mintter/app/utils/routes'
import {IS_PROD_DESKTOP} from '@mintter/shared'
import * as Sentry from '@sentry/electron/main'
import {BrowserWindow, Menu, app, ipcMain, nativeTheme, shell} from 'electron'
Expand Down Expand Up @@ -75,7 +76,7 @@ app.on('did-become-active', () => {
if (BrowserWindow.getAllWindows().length === 0) {
log.debug('[MAIN]: will open the home window')
trpc.createAppWindow({
routes: [{key: 'documents'}],
routes: [defaultRoute],
})
}
})
Expand Down Expand Up @@ -137,7 +138,7 @@ if (!gotTheLock) {
if (BrowserWindow.getAllWindows().length === 0) {
log.debug('[MAIN]: will open the home window')
trpc.createAppWindow({
routes: [{key: 'documents'}],
routes: [defaultRoute],
})
}
})
Expand Down
3 changes: 2 additions & 1 deletion frontend/packages/app/components/changes-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import {
usePublishDocToGroup,
} from '../models/groups'
import {useOpenUrl} from '../open-url'
import {NavRoute, useNavRoute} from '../utils/navigation'
import {useNavRoute} from '../utils/navigation'
import {NavRoute} from '../utils/routes'
import {AccessoryContainer} from './accessory-sidebar'
import {AccountLinkAvatar} from './account-link-avatar'
import {useAppDialog} from './dialog'
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/components/citations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {PanelCard} from '@mintter/ui'
import {useAccount} from '../models/accounts'
import {useEntityTimeline} from '../models/changes'
import {useDocTextContent, usePublication} from '../models/documents'
import {PublicationRoute} from '../utils/navigation'
import {PublicationRoute} from '../utils/routes'
import {AccessoryContainer} from './accessory-sidebar'
import {AccountLinkAvatar} from './account-link-avatar'

Expand Down
3 changes: 2 additions & 1 deletion frontend/packages/app/components/edit-doc-button.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {useGRPCClient} from '@mintter/app/app-context'
import {useDraftList} from '@mintter/app/models/documents'
import {usePublicationVariant} from '@mintter/app/models/publication'
import {NavMode, NavRoute} from '@mintter/app/utils/navigation'
import {NavMode} from '@mintter/app/utils/navigation'
import {useNavigate} from '@mintter/app/utils/useNavigate'
import {GroupVariant, PublicationVariant} from '@mintter/shared'
import {Button, Tooltip, toast} from '@mintter/ui'
import {Pencil} from '@tamagui/lucide-icons'
import appError from '../errors'
import {NavRoute} from '../utils/routes'

export function useEditDraft(
docId: string,
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/components/first-publish-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import {useAppContext} from '../app-context'
import {useGatewayUrl} from '../models/gateway-settings'
import {useIsHMUrlReady} from '../models/networking'
import {PublicationRoute} from '../utils/navigation'
import {PublicationRoute} from '../utils/routes'

export function FirstPublishDialog({
input,
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/components/pin-entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {useHover} from '@mintter/shared'
import {Button, ButtonProps, Tooltip} from '@mintter/ui'
import {Pin, PinOff} from '@tamagui/lucide-icons'
import {usePinAccount, usePinDocument, usePinGroup} from '../models/pins'
import {PublicationRoute} from '../utils/navigation'
import {PublicationRoute} from '../utils/routes'

export function PinAccountButton({accountId}: {accountId: string}) {
const {isPinned, togglePin} = usePinAccount(accountId)
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/components/publication-list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
XStack,
copyTextToClipboard,
} from '@mintter/ui'
import {NavRoute} from '../utils/navigation'
import {NavRoute} from '../utils/routes'
import {useNavigate} from '../utils/useNavigate'
import {BaseAccountLinkAvatar} from './account-link-avatar'
import {ListItem, TimeAccessory} from './list-item'
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/components/quick-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import {useGroups} from '../models/groups'
import {importWebCapture} from '../models/web-importer'
import {AppQueryClient} from '../query-client'
import {
NavRoute,
isHttpUrl,
resolveHmIdToAppRoute,
useHmIdToAppRouteResolver,
} from '../utils/navigation'
import {NavRoute} from '../utils/routes'
import {useListenAppEvent} from '../utils/window-events'
import './quick-switcher.css'

Expand Down
9 changes: 3 additions & 6 deletions frontend/packages/app/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ import {
import {getAccountName} from '../pages/account-page'
import {SidebarWidth, useSidebarContext} from '../src/sidebar-context'
import {getAvatarUrl} from '../utils/account-url'
import {
NavRoute,
useHmIdToAppRouteResolver,
useNavRoute,
} from '../utils/navigation'
import {useHmIdToAppRouteResolver, useNavRoute} from '../utils/navigation'
import {useOpenDraft} from '../utils/open-draft'
import {NavRoute} from '../utils/routes'
import {useNavigate} from '../utils/useNavigate'
import {useTriggerWindowEvent} from '../utils/window-events'
import {Avatar} from './avatar'
Expand Down Expand Up @@ -166,7 +163,7 @@ function FullAppSidebar() {
active={route.key == 'documents'}
data-testid="menu-item-global"
onPress={() => {
navigate({key: 'documents'})
navigate({key: 'documents', tab: 'trusted'})
}}
title="Documents"
bold
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/components/titlebar-common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {ContactsPrompt} from '@mintter/app/components/contacts-prompt'
import {useMyAccount} from '@mintter/app/models/accounts'
import {usePublicationVariant} from '@mintter/app/models/publication'
import {
NavRoute,
useNavRoute,
useNavigationDispatch,
useNavigationState,
Expand Down Expand Up @@ -50,6 +49,7 @@ import {
import {usePinAccount, usePinDocument, usePinGroup} from '../models/pins'
import {SidebarWidth, useSidebarContext} from '../src/sidebar-context'
import {useOpenDraft} from '../utils/open-draft'
import {NavRoute} from '../utils/routes'
import {CloneGroupDialog} from './clone-group'
import {useCopyGatewayReference} from './copy-gateway-reference'
import {useAppDialog} from './dialog'
Expand Down
14 changes: 8 additions & 6 deletions frontend/packages/app/components/titlebar-title.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {useDraftTitle} from '@mintter/app/models/documents'
import {usePublicationVariant} from '@mintter/app/models/publication'
import {
DraftRoute,
PublicationRoute,
useNavRoute,
} from '@mintter/app/utils/navigation'
import {useNavRoute} from '@mintter/app/utils/navigation'
import {
ErrorIcon,
FontSizeTokens,
Expand All @@ -17,7 +13,13 @@ import {Book, Contact, FileText, Library} from '@tamagui/lucide-icons'
import {useEffect} from 'react'
import {useAccount} from '../models/accounts'
import {useGroup} from '../models/groups'
import {AccountRoute, GroupRoute, NavRoute} from '../utils/navigation'
import {
AccountRoute,
DraftRoute,
GroupRoute,
NavRoute,
PublicationRoute,
} from '../utils/routes'
import {getDocumentTitle} from './publication-list-item'

export function TitleContent({size = '$4'}: {size?: FontSizeTokens}) {
Expand Down
3 changes: 1 addition & 2 deletions frontend/packages/app/components/variants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {usePublicationVariant} from '@mintter/app/models/publication'
import {usePopoverState} from '@mintter/app/use-popover-state'
import {
NavContextProvider,
NavRoute,
PublicationRoute,
useNavRoute,
useNavigation,
} from '@mintter/app/utils/navigation'
Expand Down Expand Up @@ -77,6 +75,7 @@ import {useGatewayUrl} from '../models/gateway-settings'
import {useCurrentDocumentGroups} from '../models/groups'
import {getAccountName} from '../pages/account-page'
import {RenamePubDialog} from '../src/rename-publication-dialog'
import {NavRoute, PublicationRoute} from '../utils/routes'
import CommitDraftButton from './commit-draft-button'
import {useAppDialog} from './dialog'
import DiscardDraftButton from './discard-draft-button'
Expand Down
9 changes: 5 additions & 4 deletions frontend/packages/app/components/version-changes-info.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Timestamp} from '@bufbuild/protobuf'
import {NavRoute, useNavRoute} from '../utils/navigation'
import {useNavigate} from '../utils/useNavigate'
import {ButtonText, Tooltip, XStack} from '@mintter/ui'
import {formattedDateLong, formattedDateMedium} from '@mintter/shared'
import {useChange} from '../models/changes'
import {ButtonText, Tooltip, XStack} from '@mintter/ui'
import {useAccount} from '../models/accounts'
import {useChange} from '../models/changes'
import {useNavRoute} from '../utils/navigation'
import {NavRoute} from '../utils/routes'
import {useNavigate} from '../utils/useNavigate'
import {AccountLinkAvatar} from './account-link-avatar'

export function VersionChangesInfo({version}: {version: string}) {
Expand Down
3 changes: 2 additions & 1 deletion frontend/packages/app/components/windows-linux-titlebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {Contact, FileText, Library} from '@tamagui/lucide-icons'
import {useMemo} from 'react'
import {useNavRoute, useNavigationDispatch} from '../utils/navigation'
import {useOpenDraft} from '../utils/open-draft'
import {defaultRoute} from '../utils/routes'
import {useTriggerWindowEvent} from '../utils/window-events'
import {WindowsLinuxWindowControls} from './window-controls'

Expand Down Expand Up @@ -150,7 +151,7 @@ export function SystemMenu() {
id: 'newwindow',
title: 'New Window',
accelerator: 'Ctrl+Shift+N',
onSelect: () => spawn({key: 'documents'}),
onSelect: () => spawn(defaultRoute),
icon: AddSquare,
},
{
Expand Down
3 changes: 2 additions & 1 deletion frontend/packages/app/models/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ import _ from 'lodash'
import {useEffect, useMemo, useRef} from 'react'
import {ContextFrom, fromPromise} from 'xstate'
import {useGRPCClient} from '../app-context'
import {NavRoute, useNavRoute} from '../utils/navigation'
import {useNavRoute} from '../utils/navigation'
import {pathNameify} from '../utils/path'
import {NavRoute} from '../utils/routes'
import {useNavigate} from '../utils/useNavigate'
import {useAllAccounts} from './accounts'
import {DraftStatusContext, draftMachine} from './draft-machine'
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/models/pins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@mintter/shared'
import {useMemo} from 'react'
import {useQueryInvalidator} from '../app-context'
import {PublicationRoute} from '../utils/navigation'
import {PublicationRoute} from '../utils/routes'
import {useGroupsContent} from './groups'

export function usePinAccount(accountId: string) {
Expand Down
5 changes: 3 additions & 2 deletions frontend/packages/app/pages/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import {useListen} from '@mintter/app/app-context'

import {AppErrorPage} from '@mintter/app//components/app-error'
import {TitleBar} from '@mintter/app/components/titlebar'
import {getRouteKey, NavRoute, useNavRoute} from '@mintter/app/utils/navigation'
import {getRouteKey, useNavRoute} from '@mintter/app/utils/navigation'
import {useNavigate} from '@mintter/app/utils/useNavigate'
import {Spinner, YStack} from '@mintter/ui'
import {lazy, ReactElement, useMemo} from 'react'
import {ReactElement, lazy, useMemo} from 'react'
import {ErrorBoundary} from 'react-error-boundary'
import {QuickSwitcher} from '../components/quick-switcher'
import {AppSidebar} from '../components/sidebar'
import {DraftStatusContext} from '../models/draft-machine'
import {SidebarContextProvider} from '../src/sidebar-context'
import {NavRoute} from '../utils/routes'
import {getWindowType} from '../utils/window-types'
import {NotFoundPage} from './base'
import {DocumentPlaceholder} from './document-placeholder'
Expand Down
6 changes: 2 additions & 4 deletions frontend/packages/app/utils/navigation-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ import {ReactNode, useEffect, useMemo} from 'react'
import {useIPC} from '../app-context'
import {useConfirmConnection} from '../components/contacts-prompt'
import {
DocumentsRoute,
NavAction,
NavContextProvider,
NavState,
navStateReducer,
setAppNavDispatch,
} from './navigation'
import {defaultRoute} from './routes'
import {AppWindowEvent} from './window-events'

const homeRoute: DocumentsRoute = {key: 'documents'}

export function NavigationContainer({
children,
initialNav = {
sidebarLocked: false,
routes: [homeRoute],
routes: [defaultRoute],
routeIndex: 0,
lastAction: 'replace',
},
Expand Down
Loading

0 comments on commit 471c6ef

Please sign in to comment.