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

feat: add concept links to hierarchy view #38

Merged
merged 3 commits into from
May 12, 2023
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
3 changes: 2 additions & 1 deletion src/components/ChildConcepts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Tooltip, Box, Stack, Text} from '@sanity/ui'
import {ErrorOutlineIcon} from '@sanity/icons'
import {ChildConceptTerm} from '../types'
import {hues} from '@sanity/color'
import {ConceptDetailLink} from './ConceptDetailLink'

const StyledChildConcept = styled.ul`
list-style: none;
Expand All @@ -21,7 +22,7 @@ export const ChildConcepts = ({concepts}: {concepts: ChildConceptTerm[]}) => {
{concepts.map((concept: any) => {
return (
<li key={concept.id}>
{concept.prefLabel}
<ConceptDetailLink concept={concept} />
{concept.childConcepts?.length > 0 && concept.level == 5 && (
<Tooltip
content={
Expand Down
38 changes: 38 additions & 0 deletions src/components/ConceptDetailLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {usePaneRouter} from 'sanity/desk'
import {RouterContext} from 'sanity/router'
import {useCallback, useContext} from 'react'
import {ChildConceptTerm} from '../types'
import styled from 'styled-components'

const StyledConceptLink = styled.span`
cursor: pointer;
&:hover {
text-decoration: underline;
}
`

export function ConceptDetailLink({concept}: {concept: ChildConceptTerm}) {
const routerContext = useContext(RouterContext)
const {routerPanesState, groupIndex} = usePaneRouter()

const {id, prefLabel} = concept

const openInNewPane = useCallback(() => {
if (!routerContext || !id) {
return
}

const panes = [...routerPanesState]
panes.splice(groupIndex + 1, groupIndex + 1, [
{
id: id,
params: {type: 'skosConcept'},
},
])

const href = routerContext.resolvePathFromState({panes})
routerContext.navigateUrl({path: href})
}, [id, routerContext, routerPanesState, groupIndex])

return <StyledConceptLink onClick={openInNewPane}>{prefLabel}</StyledConceptLink>
}
3 changes: 2 additions & 1 deletion src/components/Orphans.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components'
import {Text, Inline} from '@sanity/ui'
import {ChildConcepts} from './ChildConcepts'
import {ConceptDetailLink} from './ConceptDetailLink'
import {ChildConceptTerm, DocumentConcepts} from '../types'

const StyledOrphan = styled.li`
Expand All @@ -19,7 +20,7 @@ export const Orphans = ({
return (
<StyledOrphan key={concept.id}>
<Inline space={2}>
{concept?.prefLabel}
<ConceptDetailLink concept={concept} />
{docConcepts.topConcepts?.length > 0 && (
<Text size={1} muted>
orphan
Expand Down
3 changes: 2 additions & 1 deletion src/components/TopConcepts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Text, Inline} from '@sanity/ui'
import styled from 'styled-components'
import {ChildConcepts} from './ChildConcepts'
import {ConceptDetailLink} from './ConceptDetailLink'
import {TopConceptTerm} from '../types'

const StyledTopConcept = styled.li`
Expand All @@ -12,7 +13,7 @@ export const TopConcepts = ({concept}: {concept: TopConceptTerm}) => {
return (
<StyledTopConcept>
<Inline space={2}>
{concept?.prefLabel}
<ConceptDetailLink concept={concept} />
<Text size={1} muted>
top concept
</Text>
Expand Down
9 changes: 9 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ export interface PrefLabelValue {
// eslint-disable-next-line
renderDefault: (props: PrefLabelValue) => React.ReactElement
}

export interface ConceptDetailLinkProps {
concept: {
id: string
prefLabel: string
childConcepts?: ChildConceptTerm[]
level?: number
}
}