Skip to content

Commit

Permalink
feat(preview): prévisualise un corpus
Browse files Browse the repository at this point in the history
  • Loading branch information
thom4parisot committed Jan 22, 2025
1 parent f5780e4 commit 3561b7e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 36 deletions.
2 changes: 2 additions & 0 deletions front/src/components/Preview.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ query getArticle($id: ID!, $version: ID!, $hasVersion: Boolean!) {
}

version(version: $version) @include(if: $hasVersion) {
name
md
bib
metadata
Expand All @@ -33,6 +34,7 @@ query getArticle($id: ID!, $version: ID!, $hasVersion: Boolean!) {

query getCorpus($filter: FilterCorpusInput) {
corpus(filter: $filter) {
name
metadata
articles {
article {
Expand Down
111 changes: 79 additions & 32 deletions front/src/components/Preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,85 @@ import * as queries from './Preview.graphql'

import './Preview.scss'

function mapContent({ query, data }) {
if (!data) {
return {}
}
const strategies = new Map([
[
'article',
{
query({ id, version, workspaceId }) {
const hasVersion = Boolean(version)

if (query === 'getArticle') {
const root = data?.article?.workingVersion ?? data?.version
return {
md_content: root?.md,
metadata_content: root?.metadata,
bib_content: root?.bib,
}
} else if (query === 'getCorpus') {
return {
md_content: '',
metadata_content: '',
bib_content: '',
}
}
return {
query: queries.getArticle,
variables: {
id,
version: hasVersion ? version : 'dummy',
hasVersion,
},
}
},
mapContent(data) {
const root = data?.article?.workingVersion ?? data?.version
return {
md_content: root?.md,
metadata_content: root?.metadata,
bib_content: root?.bib,
}
},
title(data) {
const root = data?.article?.workingVersion ?? data?.version
return root.title ?? root.name
},
},
],
[
'corpus',
{
query({ id, workspaceId }) {
return {
query: queries.getCorpus,
variables: {
filter: {
corpusId: id,
workspaceId,
},
},
}
},
mapContent(data) {
return data?.corpus?.at(0)?.articles?.reduce(
(obj, { article }) => ({
md_content: obj.md_content + '\n\n\n' + article.workingVersion.md,
metadata_content: Object.keys(obj.metadata_content).length
? { ...data.corpus.metadata, ...obj.metadata_content }
: article.workingVersion.metadata,
bib_content:
obj.bib_content + '\n\n---\n\n' + article.workingVersion.bib,
}),
{
md_content: '',
metadata_content: {},
bib_content: '',
}
)
},
title(data) {
return data?.corpus?.at(0).name
},
},
],
])

throw Error('Unknown query mapping. Cannot preview this content.')
}
export default function Preview({ strategy: strategyId }) {
const { id, version, workspaceId } = useParams()

const strategy = useMemo(
() => strategies.get(strategyId),
[id, version, strategyId]
)

export default function Preview({ query }) {
const { id, version } = useParams()
if (!strategy) {
throw Error('Unknown query mapping. Cannot preview this content.')
}

useEffect(() => {
globalThis.hypothesisConfig = function hypothesisConfig() {
Expand All @@ -58,24 +112,16 @@ export default function Preview({ query }) {
return () => document.body.removeChild(script)
}, [])

const hasVersion = Boolean(version)
const { data, isLoading: isDataLoading } = useGraphQL(
{
query: queries[query],
variables: {
id,
version: hasVersion ? version : 'dummy',
hasVersion,
},
},
strategy.query({ id, version, workspaceId }),
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
}
)

const { html: __html, isLoading: isPreviewLoading } = useStyloExportPreview({
...mapContent({ data, query }),
...strategy.mapContent(data),
with_toc: true,
with_nocite: true,
with_link_citations: true,
Expand All @@ -93,6 +139,7 @@ export default function Preview({ query }) {
return (
<>
<meta name="robots" content="noindex, nofollow" />
<title>{strategy.title(data)}</title>
<section dangerouslySetInnerHTML={{ __html }} />
</>
)
Expand Down
12 changes: 9 additions & 3 deletions front/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,14 @@ root.render(
<Credentials />
</PrivateRoute>
{/* Annotate a Book */}
<Route path={[`/books/:id/preview`]} exact>
<Preview query="getCorpus" />
<Route
path={[
'/workspaces/:workspaceId/books/:id/preview',
'/books/:id/preview',
]}
exact
>
<Preview strategy="corpus" />
</Route>
{/* Annotate an article or its version */}
<Route
Expand All @@ -175,7 +181,7 @@ root.render(
]}
exact
>
<Preview query="getArticle" />
<Preview strategy="article" />
</Route>
{/* Write and Compare */}
<PrivateRoute
Expand Down
2 changes: 1 addition & 1 deletion graphql/resolvers/corpusResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ module.exports = {
if ('filter' in args) {
const filter = args.filter
if ('corpusId' in filter) {
return [await getCorpusByContext(filter.corpusId, context)]
return [await getCorpus(filter.corpusId)]
}
if ('workspaceId' in filter) {
// check that the user can access the workspace
Expand Down

0 comments on commit 3561b7e

Please sign in to comment.