Skip to content

Commit

Permalink
add breadcrumb (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur authored Jul 7, 2024
1 parent 6175409 commit bc515fd
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 23 deletions.
41 changes: 41 additions & 0 deletions ui2/src/components/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {useSelector} from "react-redux"
import {Breadcrumbs, Skeleton, Anchor} from "@mantine/core"

import {selectPanelBreadcrumbs} from "@/slices/dualPanel"
import type {PanelMode, NType} from "@/types"
import type {RootState} from "@/app/types"

type Args = {
mode: PanelMode
onClick: (node: NType) => void
}

export default function BreadcrumbsComponent({mode, onClick}: Args) {
const items = useSelector<RootState>(state =>
selectPanelBreadcrumbs(state, mode)
) as Array<[string, string]> | null | undefined

if (!items) {
return (
<Skeleton width={"25%"} my="md">
<Breadcrumbs>{["one", "two"]}</Breadcrumbs>
</Skeleton>
)
}
console.log(items)
const links = items.slice(0, -1).map(i => (
<Anchor key={i[0]} onClick={() => onClick({id: i[0], ctype: "folder"})}>
{i[1]}
</Anchor>
))
const lastOne = items[items.length - 1][1]

return (
<>
<Breadcrumbs>
{links}
{lastOne}
</Breadcrumbs>
</>
)
}
3 changes: 3 additions & 0 deletions ui2/src/components/Breadcrumbs/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Breadcrumbs from "./Breadcrumbs"

export default Breadcrumbs
9 changes: 6 additions & 3 deletions ui2/src/components/Commander/Commander.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
} from "@/slices/dualPanel"

import type {RootState} from "@/app/types"
import type {NodeType, PanelMode} from "@/types"
import type {NType, NodeType, PanelMode} from "@/types"
import Breadcrumbs from "@/components/Breadcrumbs"

type Args = {
mode: PanelMode
Expand Down Expand Up @@ -42,7 +43,7 @@ export default function Commander({mode}: Args) {
)
}

const onClick = (node: NodeType) => {
const onClick = (node: NType) => {
if (mode == "secondary" && node.ctype == "folder") {
dispatch(
fetchPaginatedNodes({
Expand All @@ -53,7 +54,7 @@ export default function Commander({mode}: Args) {
)
dispatch(
setCurrentNode({
node: {id: node.id, ctype: "folder"},
node: {id: node.id, ctype: "folder", breadcrumb: null},
panel: "secondary"
})
)
Expand All @@ -70,6 +71,7 @@ export default function Commander({mode}: Args) {
return (
<div>
<FolderNodeActions mode={mode} />
<Breadcrumbs mode={mode} onClick={onClick} />
<Group>{nodes}</Group>
</div>
)
Expand All @@ -78,6 +80,7 @@ export default function Commander({mode}: Args) {
return (
<div>
<FolderNodeActions mode={mode} />
<Breadcrumbs mode={mode} onClick={onClick} />
<Group>Empty</Group>
</div>
)
Expand Down
14 changes: 12 additions & 2 deletions ui2/src/components/DualPanel/ToggleSecondaryPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ export default function ToggleSecondaryPanel({mode}: Args) {

const onClick = () => {
const folderId = user.home_folder_id
dispatch(openSecondaryPanel())
dispatch(
openSecondaryPanel({
id: folderId,
ctype: "folder",
breadcrumb: null
})
)
dispatch(
fetchPaginatedNodes({
folderId,
Expand All @@ -36,7 +42,11 @@ export default function ToggleSecondaryPanel({mode}: Args) {
)
dispatch(
setCurrentNode({
node: {id: folderId, ctype: "folder"},
node: {
id: folderId,
ctype: "folder",
breadcrumb: [[folderId, ".home"]]
},
panel: "secondary"
})
)
Expand Down
2 changes: 1 addition & 1 deletion ui2/src/pages/Folder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function loader({params, request}: LoaderFunctionArgs) {

store.dispatch(
setCurrentNode({
node: {id: folderId, ctype: "folder"},
node: {id: folderId, ctype: "folder", breadcrumb: null},
panel: "main"
})
)
Expand Down
6 changes: 4 additions & 2 deletions ui2/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ export default function Home() {
export async function loader({params, request}: LoaderFunctionArgs) {
const url = new URL(request.url)
const user: User = await getCurrentUser()
let folderId
let folderId, breadcrumb

if (params.folderId) {
folderId = params.folderId
breadcrumb = null
} else {
folderId = user.home_folder_id
breadcrumb = [[folderId, ".home"]] as Array<[string, string]>
}

store.dispatch(
setCurrentNode({
node: {id: folderId, ctype: "folder"},
node: {id: folderId, ctype: "folder", breadcrumb: breadcrumb},
panel: "main"
})
)
Expand Down
2 changes: 1 addition & 1 deletion ui2/src/pages/Inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function loader({params, request}: LoaderFunctionArgs) {

store.dispatch(
setCurrentNode({
node: {id: folderId, ctype: "folder"},
node: {id: folderId, ctype: "folder", breadcrumb: null},
panel: "main"
})
)
Expand Down
60 changes: 46 additions & 14 deletions ui2/src/slices/dualPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import type {
SliceState,
NodeType,
PanelMode,
NType,
Paginated,
NodeLoaderResponseType,
FolderType
FolderType,
CurrentNodeType
} from "@/types"

type NodeWithSpinner = {
Expand All @@ -27,7 +27,7 @@ type NodeWithSpinner = {
}

type SetCurrentNodeArgs = {
node: NType
node: CurrentNodeType
panel: PanelMode
}

Expand All @@ -37,7 +37,7 @@ type FolderAddedArgs = {
}

interface Commander {
currentNode: string | null
currentNode: CurrentNodeType | null
pageSize: number
pageNumber: number
sort: string
Expand All @@ -57,9 +57,9 @@ interface DualPanelState {
nodes: Array<NodeType>
}

function commanderInitialState(folderId: string | null): Commander {
function commanderInitialState(node: CurrentNodeType | null): Commander {
return {
currentNode: folderId,
currentNode: node,
pageSize: 15,
pageNumber: 1,
sort: "-title",
Expand Down Expand Up @@ -129,13 +129,22 @@ const dualPanelSlice = createSlice({
if (action.payload.node.ctype == "folder") {
// commander
if (state.mainPanel.commander) {
// preserve breadcrumb
const prevBreadcrumb =
state.mainPanel.commander.currentNode?.breadcrumb
// just update commander's current node
state.mainPanel.commander.currentNode = action.payload.node.id
state.mainPanel.commander.currentNode = {
id: action.payload.node.id,
ctype: action.payload.node.ctype,
breadcrumb: prevBreadcrumb
}
} else {
// re-open commander
state.mainPanel.commander = commanderInitialState(
action.payload.node.id
)
state.mainPanel.commander = commanderInitialState({
id: action.payload.node.id,
ctype: "folder",
breadcrumb: null
})
}
} else {
// viewer
Expand All @@ -157,9 +166,9 @@ const dualPanelSlice = createSlice({
})
}
},
openSecondaryPanel(state) {
openSecondaryPanel(state, action: PayloadAction<CurrentNodeType>) {
state.secondaryPanel = {
commander: commanderInitialState(null),
commander: commanderInitialState(action.payload),
viewer: null
}
},
Expand Down Expand Up @@ -192,13 +201,21 @@ const dualPanelSlice = createSlice({
error: null,
data: newNodes
}
if (state.mainPanel.commander.currentNode) {
state.mainPanel.commander.currentNode.breadcrumb =
action.payload.breadcrumb
}
}
} else if (state.secondaryPanel && state.secondaryPanel.commander) {
state.secondaryPanel.commander.nodes = {
status: "succeeded",
error: null,
data: newNodes
}
if (state.secondaryPanel.commander.currentNode) {
state.secondaryPanel.commander.currentNode.breadcrumb =
action.payload.breadcrumb
}
}
})
}
Expand Down Expand Up @@ -306,11 +323,26 @@ const selectSecondaryPanelNodes = (

export const selectCurrentFolderID = (state: RootState, mode: PanelMode) => {
if (mode == "main") {
return state.dualPanel.mainPanel.commander?.currentNode
return state.dualPanel.mainPanel.commander?.currentNode?.id
}

if (state.dualPanel.secondaryPanel?.commander) {
return state.dualPanel.secondaryPanel.commander.currentNode?.id
}

return null
}

export const selectPanelBreadcrumbs = (
state: RootState,
mode: PanelMode
): Array<[string, string]> | null | undefined => {
if (mode == "main") {
return state.dualPanel.mainPanel.commander?.currentNode?.breadcrumb
}

if (state.dualPanel.secondaryPanel?.commander) {
return state.dualPanel.secondaryPanel.commander.currentNode
return state.dualPanel.secondaryPanel.commander.currentNode?.breadcrumb
}

return null
Expand Down
7 changes: 7 additions & 0 deletions ui2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export type OcrStatusEnum =

export type CType = "folder" | "document"

export type CurrentNodeType = {
id: string
ctype: CType
breadcrumb: Array<[string, string]> | null | undefined
}

export type CreateUser = {
username: string
email: string
Expand Down Expand Up @@ -101,6 +107,7 @@ export type NodeType = NType & {
ocr_status: OcrStatusEnum
ocr: boolean
thumbnail_url: string | null
breadcrumb: Array<[string, string]>
}

export type BreadcrumbItemType = [string, string]
Expand Down

0 comments on commit bc515fd

Please sign in to comment.