document loading working
This commit is contained in:
parent
3e97241cf6
commit
7dc55799b8
14 changed files with 236 additions and 136 deletions
25
package-lock.json
generated
25
package-lock.json
generated
|
@ -8,6 +8,7 @@
|
||||||
"name": "eexiv-2",
|
"name": "eexiv-2",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@tanstack/react-query": "^5.20.2",
|
||||||
"minisearch": "^6.3.0",
|
"minisearch": "^6.3.0",
|
||||||
"next": "14.1.0",
|
"next": "14.1.0",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
|
@ -466,6 +467,30 @@
|
||||||
"tslib": "^2.4.0"
|
"tslib": "^2.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@tanstack/query-core": {
|
||||||
|
"version": "5.20.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.20.2.tgz",
|
||||||
|
"integrity": "sha512-sAILwNiyA1I52e6imOsmNDUA/PuOayOzqz5jcLiIB5wBXqVk+HIiriWouPcAkjS8RqARfHUehuoPwcZ7Uzh0GQ==",
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/tannerlinsley"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@tanstack/react-query": {
|
||||||
|
"version": "5.20.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.20.2.tgz",
|
||||||
|
"integrity": "sha512-949myvMY77cPqwb71m3wRG2ypgwPijshO5kN9w0CDKWrFC0X8Wh1mwSqst88kIr58tWlWNsGy3U40AK23RgYQA==",
|
||||||
|
"dependencies": {
|
||||||
|
"@tanstack/query-core": "5.20.2"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/tannerlinsley"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": "^18.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@types/file-saver": {
|
"node_modules/@types/file-saver": {
|
||||||
"version": "2.0.7",
|
"version": "2.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/@types/file-saver/-/file-saver-2.0.7.tgz",
|
"resolved": "https://registry.npmjs.org/@types/file-saver/-/file-saver-2.0.7.tgz",
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
"format": "prettier --write ."
|
"format": "prettier --write ."
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@tanstack/react-query": "^5.20.2",
|
||||||
"minisearch": "^6.3.0",
|
"minisearch": "^6.3.0",
|
||||||
"next": "14.1.0",
|
"next": "14.1.0",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
|
|
|
@ -346,8 +346,7 @@ authorName (as a slug): {
|
||||||
website: website url
|
website: website url
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
export interface Authors {
|
export interface Author {
|
||||||
[key: string]: {
|
|
||||||
name: {
|
name: {
|
||||||
first: string
|
first: string
|
||||||
last: string
|
last: string
|
||||||
|
@ -359,9 +358,8 @@ export interface Authors {
|
||||||
formerAffiliations?: string[]
|
formerAffiliations?: string[]
|
||||||
bio?: string
|
bio?: string
|
||||||
website?: string
|
website?: string
|
||||||
}
|
|
||||||
}
|
}
|
||||||
export const authors: Authors = {
|
export const authors: { [key: string]: Author } = {
|
||||||
shasan: {
|
shasan: {
|
||||||
name: {
|
name: {
|
||||||
first: 'Saim',
|
first: 'Saim',
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Document } from './data'
|
import { Document, Author } from './data'
|
||||||
|
|
||||||
export const loadDocument = (id: string): Promise<Document> => {
|
export const loadDocument = (id: string): Promise<Document> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -62,3 +62,32 @@ export const loadAllDocuments = (): Promise<{ [key: string]: Document }> => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const loadAllAuthors = (): Promise<{ [key: string]: Author }> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (typeof Worker !== 'undefined') {
|
||||||
|
const worker = new Worker(
|
||||||
|
new URL('./workers/documentLoader.worker.ts', import.meta.url),
|
||||||
|
{ type: 'module' }
|
||||||
|
)
|
||||||
|
|
||||||
|
worker.onmessage = (e: MessageEvent<{ [key: string]: Author }>) => {
|
||||||
|
resolve(e.data)
|
||||||
|
worker.terminate()
|
||||||
|
}
|
||||||
|
|
||||||
|
worker.onerror = (error) => {
|
||||||
|
reject(error)
|
||||||
|
worker.terminate()
|
||||||
|
}
|
||||||
|
|
||||||
|
worker.postMessage('LOAD')
|
||||||
|
} else {
|
||||||
|
reject(
|
||||||
|
new Error(
|
||||||
|
'Web Workers are not supported in this environment. Please avoid using a prehistoric browser.'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
7
src/app/db/workers/affiliationLoader.worker.ts
Normal file
7
src/app/db/workers/affiliationLoader.worker.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { affiliations } from '../data'
|
||||||
|
|
||||||
|
onmessage = (e) => {
|
||||||
|
if (e.data === 'LOAD') {
|
||||||
|
self.postMessage(affiliations)
|
||||||
|
}
|
||||||
|
}
|
7
src/app/db/workers/authorLoader.worker.ts
Normal file
7
src/app/db/workers/authorLoader.worker.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { authors } from '../data'
|
||||||
|
|
||||||
|
onmessage = (e) => {
|
||||||
|
if (e.data === 'LOAD') {
|
||||||
|
self.postMessage(authors)
|
||||||
|
}
|
||||||
|
}
|
7
src/app/db/workers/nationalityLoader.worker.ts
Normal file
7
src/app/db/workers/nationalityLoader.worker.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { nationalities } from '../data'
|
||||||
|
|
||||||
|
onmessage = (e) => {
|
||||||
|
if (e.data === 'LOAD') {
|
||||||
|
self.postMessage(nationalities)
|
||||||
|
}
|
||||||
|
}
|
7
src/app/db/workers/topicLoader.worker.ts
Normal file
7
src/app/db/workers/topicLoader.worker.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { topics } from '../data'
|
||||||
|
|
||||||
|
onmessage = (e) => {
|
||||||
|
if (e.data === 'LOAD') {
|
||||||
|
self.postMessage(topics)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import { DocumentType, Document } from '@/app/db/data'
|
import { DocumentType } from '@/app/db/data'
|
||||||
import { Zilla_Slab } from 'next/font/google'
|
import { Zilla_Slab } from 'next/font/google'
|
||||||
import { epoch2datestring } from '@/app/utils/epoch2datestring'
|
import { epoch2datestring } from '@/app/utils/epoch2datestring'
|
||||||
import {
|
import {
|
||||||
|
@ -10,14 +10,21 @@ import {
|
||||||
} from '@/app/components/DataDisplay'
|
} from '@/app/components/DataDisplay'
|
||||||
import { ItemBadge, Status } from '@/app/components/Badges'
|
import { ItemBadge, Status } from '@/app/components/Badges'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
|
import { useSuspenseQuery } from '@tanstack/react-query'
|
||||||
|
import { loadDocument } from '@/app/db/loaders'
|
||||||
|
|
||||||
const zillaSlab = Zilla_Slab({ subsets: ['latin'], weight: ['500', '700'] })
|
const zillaSlab = Zilla_Slab({ subsets: ['latin'], weight: ['500', '700'] })
|
||||||
|
|
||||||
export default function DocumentViewer({
|
export default function DocumentViewer({ slug }: Readonly<{ slug: string }>) {
|
||||||
doc,
|
const { data, error } = useSuspenseQuery({
|
||||||
slug,
|
queryKey: [slug],
|
||||||
}: Readonly<{ doc: Document; slug: string }>) {
|
queryFn: () => {
|
||||||
const { manifest, abstract, file, citation } = doc
|
const data = loadDocument(slug)
|
||||||
|
return data
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const { manifest, abstract, file, citation } = data
|
||||||
const {
|
const {
|
||||||
title,
|
title,
|
||||||
authors,
|
authors,
|
||||||
|
@ -31,6 +38,8 @@ export default function DocumentViewer({
|
||||||
status,
|
status,
|
||||||
} = manifest
|
} = manifest
|
||||||
|
|
||||||
|
if (error) throw error
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='max-w-4xl lg:max-w-6xl mx-auto'>
|
<div className='max-w-4xl lg:max-w-6xl mx-auto'>
|
||||||
<h1
|
<h1
|
||||||
|
|
|
@ -1,29 +1,13 @@
|
||||||
'use client'
|
'use client'
|
||||||
import { Zilla_Slab } from 'next/font/google'
|
|
||||||
import { DocumentType, reviewer, Document, documents } from '@/app/db/data'
|
|
||||||
import { loadDocument, loadAllDocuments } from '@/app/db/loaders'
|
|
||||||
import DocumentViewer from './DocumentViewer'
|
import DocumentViewer from './DocumentViewer'
|
||||||
import createResource from '@/app/utils/createResource'
|
import ErrorBoundary from '@/app/utils/ErrorBoundary'
|
||||||
import { Suspense, useEffect, useMemo } from 'react'
|
|
||||||
|
|
||||||
export default function Page({
|
export default function Page({
|
||||||
params,
|
params,
|
||||||
}: Readonly<{ params: { slug: string } }>) {
|
}: Readonly<{ params: { slug: string } }>) {
|
||||||
// const docResource = createResource(loadDocument(params.slug))
|
|
||||||
// const doc = docResource.read()
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const check = async () => {
|
|
||||||
const doc = await loadDocument(params.slug)
|
|
||||||
console.log(doc)
|
|
||||||
}
|
|
||||||
check()
|
|
||||||
})
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<ErrorBoundary>
|
||||||
<div>hi</div>
|
<DocumentViewer slug={params.slug} />
|
||||||
<DocumentViewer doc={documents[params.slug]} slug={params.slug} />
|
</ErrorBoundary>
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import Container from './container/Container'
|
||||||
import MobileMenu from './components/MobileMenu'
|
import MobileMenu from './components/MobileMenu'
|
||||||
import { ToastContainer } from 'react-toastify'
|
import { ToastContainer } from 'react-toastify'
|
||||||
import 'react-toastify/dist/ReactToastify.css'
|
import 'react-toastify/dist/ReactToastify.css'
|
||||||
|
import Providers from './providers'
|
||||||
|
|
||||||
/* The default font is Inter. If you want to use Zilla Slab (or any other Google Font,
|
/* The default font is Inter. If you want to use Zilla Slab (or any other Google Font,
|
||||||
which are pre-provided by Next.js in the 'next/font/google' module), you need to
|
which are pre-provided by Next.js in the 'next/font/google' module), you need to
|
||||||
|
@ -31,6 +32,7 @@ export default function RootLayout({
|
||||||
return (
|
return (
|
||||||
<html lang='en'>
|
<html lang='en'>
|
||||||
<body className={inter.className}>
|
<body className={inter.className}>
|
||||||
|
<Providers>
|
||||||
<ToastContainer />
|
<ToastContainer />
|
||||||
<div className={styles.header}>
|
<div className={styles.header}>
|
||||||
<div className='max-w-[1200px] flex flex-nowrap mx-auto justify-between items-center'>
|
<div className='max-w-[1200px] flex flex-nowrap mx-auto justify-between items-center'>
|
||||||
|
@ -105,6 +107,7 @@ export default function RootLayout({
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
</Providers>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
)
|
)
|
||||||
|
|
20
src/app/providers.jsx
Normal file
20
src/app/providers.jsx
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
'use client'
|
||||||
|
|
||||||
|
import { useState } from 'react'
|
||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
|
|
||||||
|
export default function Providers({ children }) {
|
||||||
|
const [queryClient] = useState(
|
||||||
|
() =>
|
||||||
|
new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
staleTime: 60 * 1000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||||
|
)
|
||||||
|
}
|
29
src/app/utils/ErrorBoundary.jsx
Normal file
29
src/app/utils/ErrorBoundary.jsx
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { notFound } from 'next/navigation'
|
||||||
|
|
||||||
|
export default class ErrorBoundary extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props)
|
||||||
|
this.state = { hasError: false, error: null }
|
||||||
|
}
|
||||||
|
|
||||||
|
static getDerivedStateFromError(error) {
|
||||||
|
// Update state so the next render will show the fallback UI.
|
||||||
|
return { hasError: true, error }
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidCatch(error, errorInfo) {
|
||||||
|
// You can also log the error to an error reporting service
|
||||||
|
console.error('Error caught by Error Boundary:', error, errorInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (this.state.hasError) {
|
||||||
|
// You can render any custom fallback UI
|
||||||
|
if (this.state.error.message === '404') notFound()
|
||||||
|
return <h1>Something went wrong.</h1>
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.props.children
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,26 +0,0 @@
|
||||||
export default function wrapPromise(promise: Promise<any>) {
|
|
||||||
let status = 'pending'
|
|
||||||
let result: any
|
|
||||||
let suspender = promise.then(
|
|
||||||
(r) => {
|
|
||||||
status = 'success'
|
|
||||||
result = r
|
|
||||||
},
|
|
||||||
(e) => {
|
|
||||||
status = 'error'
|
|
||||||
result = e
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
read() {
|
|
||||||
if (status === 'pending') {
|
|
||||||
throw suspender
|
|
||||||
} else if (status === 'error') {
|
|
||||||
throw result
|
|
||||||
} else if (status === 'success') {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue