2021-03-18 05:52:36 -04:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { postJSON } from '../../../infrastructure/fetch-json'
|
|
|
|
import { fileCollator } from '../util/file-collator'
|
2021-06-23 04:09:23 -04:00
|
|
|
import useAbortController from '../../../shared/hooks/use-abort-controller'
|
2021-03-18 05:52:36 -04:00
|
|
|
|
2023-05-15 05:17:13 -04:00
|
|
|
export type OutputEntity = {
|
|
|
|
path: string
|
|
|
|
clsiServerId: string
|
|
|
|
compileGroup: string
|
|
|
|
build: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const alphabetical = (a: OutputEntity, b: OutputEntity) =>
|
|
|
|
fileCollator.compare(a.path, b.path)
|
2021-03-18 05:52:36 -04:00
|
|
|
|
2023-05-15 05:17:13 -04:00
|
|
|
export function useProjectOutputFiles(projectId?: string) {
|
|
|
|
const [loading, setLoading] = useState<boolean>(false)
|
|
|
|
const [data, setData] = useState<OutputEntity[] | null>(null)
|
|
|
|
const [error, setError] = useState<any>(false)
|
2021-03-18 05:52:36 -04:00
|
|
|
|
2021-06-23 04:09:23 -04:00
|
|
|
const { signal } = useAbortController()
|
|
|
|
|
2021-03-18 05:52:36 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (projectId) {
|
|
|
|
setLoading(true)
|
|
|
|
setError(false)
|
|
|
|
setData(null)
|
|
|
|
|
|
|
|
postJSON(`/project/${projectId}/compile`, {
|
|
|
|
body: {
|
|
|
|
check: 'silent',
|
|
|
|
draft: false,
|
2021-04-27 03:52:58 -04:00
|
|
|
incrementalCompilesEnabled: false,
|
|
|
|
},
|
2021-06-23 04:09:23 -04:00
|
|
|
signal,
|
2021-03-18 05:52:36 -04:00
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
if (data.status === 'success') {
|
2023-05-15 05:17:13 -04:00
|
|
|
const filteredFiles = data.outputFiles.filter(
|
|
|
|
(file: OutputEntity) =>
|
|
|
|
file.path.match(/.*\.(pdf|png|jpeg|jpg|gif)/)
|
2021-03-18 05:52:36 -04:00
|
|
|
)
|
2023-05-15 05:17:13 -04:00
|
|
|
data.outputFiles.forEach((file: OutputEntity) => {
|
2022-05-13 05:35:31 -04:00
|
|
|
file.clsiServerId = data.clsiServerId
|
|
|
|
file.compileGroup = data.compileGroup
|
|
|
|
})
|
2021-03-18 05:52:36 -04:00
|
|
|
setData(filteredFiles.sort(alphabetical))
|
|
|
|
} else {
|
2021-07-30 04:57:48 -04:00
|
|
|
setError('linked-project-compile-error')
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => setError(error))
|
|
|
|
.finally(() => setLoading(false))
|
|
|
|
}
|
2021-06-23 04:09:23 -04:00
|
|
|
}, [projectId, signal])
|
2021-03-18 05:52:36 -04:00
|
|
|
|
|
|
|
return { loading, data, error }
|
|
|
|
}
|