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(web): download all artifacts as tarball #108

Merged
merged 8 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 4 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
},
"dependencies": {
"@octokit/rest": "^21.0.0",
"@zk-kit/artifacts": "1.6.0",
"@zk-kit/artifacts": "1.9.0",
cedoor marked this conversation as resolved.
Show resolved Hide resolved
"jotai": "^2.8.3",
"preact": "^10.22.0",
"react-query": "^3.39.3"
"react-query": "^3.39.3",
"semver": "^7.6.2"
cedoor marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@preact/preset-vite": "^2.8.2",
"@types/semver": "^7.5.8",
"typescript": "^5.2.2",
"vite": "^5.3.1"
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const artifacts = ['wasm', 'zkey']
export const owner = 'privacy-scaling-explorations'
export const repo = 'snark-artifacts'
export const cdnUrl = 'https://snark-artifacts.pse.dev'
export const npmRegistryUrl = 'https://registry.npmjs.org'
16 changes: 10 additions & 6 deletions apps/web/src/containers/DownloadButton.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { cdnUrl } from '../constants'
import { useStore } from '../hooks'
import { useDownloadUrl, useStore } from '../hooks'

export function DownloadButton() {
const { selectedFile, selectedProject, selectedVersion } = useStore()
const { selectedFile, tickedDownloadAll } = useStore()
const disabled = !(selectedFile || tickedDownloadAll)
const downloadUrl = useDownloadUrl()

const onClick = () => {
const url = `${cdnUrl}/${selectedProject}/${selectedVersion}/${selectedFile}`
window.location.href = url
window.location.href = downloadUrl
}

return <button type='button' onClick={onClick} className='btn btn-primary' disabled={!selectedFile}>Download</button>
return (
<button type='button' onClick={onClick} className='btn btn-primary' disabled={disabled}>
Download
</button>
)
}
23 changes: 14 additions & 9 deletions apps/web/src/containers/FileSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Options } from '../components'
import { useFiles, useStore } from '../hooks'

export function FileSelect() {
const { selectedFile, setSelectedFile } = useStore()
const { selectedFile, setSelectedFile, tickedDownloadAll, toggleTickedDownloadAll } = useStore()
const { data: files, isLoading, isFetched, isSuccess, isError, error } = useFiles()
const onChange = (e: ChangeEvent<HTMLSelectElement>) => {
const file = e.target as HTMLSelectElement
setSelectedFile(file.value)
const onSelectChange = (e: ChangeEvent<HTMLSelectElement>) => {
const { value } = e.target as HTMLSelectElement
setSelectedFile(value)
}

if (isLoading) return <div>Loading...</div>
Expand All @@ -17,13 +17,18 @@ export function FileSelect() {
return (
<div>
<label htmlFor='file'>File</label>
<select id='file' onChange={onChange} value={selectedFile}>
<select id='file' onChange={onSelectChange} value={selectedFile} disabled={tickedDownloadAll}>
<Options items={files} />
</select>
{
/* <label htmlFor='all-files' style={{ marginLeft: '1rem'}}>Download all</label>
<input id='all-files' type='checkbox' /> */
}

<label htmlFor='all-files' style={{ marginLeft: '1rem' }}>All</label>
<input
id='all-files'
type='checkbox'
onChange={toggleTickedDownloadAll}
checked={tickedDownloadAll}
title='Download all artifacts as .tgz tarball'
/>
</div>
)
}
Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/containers/ProjectSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import { Options } from '../components'
import { useStore } from '../hooks'

export function ProjectSelect() {
const { selectedProject, setSelectedProject, resetSelectedFile, resetSelectedVersion } = useStore()
const { selectedProject, setSelectedProject } = useStore()

const onChange = (e: ChangeEvent<HTMLSelectElement>) => {
resetSelectedFile()
resetSelectedVersion()
const target = e.target as HTMLSelectElement
setSelectedProject(target.value)
}
Expand Down
7 changes: 3 additions & 4 deletions apps/web/src/containers/VersionSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { Options } from '../components'
import { useStore, useVersions } from '../hooks'

export function VersionSelect() {
const { selectedVersion, setSelectedVersion, resetSelectedFile } = useStore()
const { selectedVersion, setSelectedVersion } = useStore()
const { data: versions, isSuccess, isFetched, isLoading, isError, error } = useVersions()

const onChange = (e: ChangeEvent<HTMLSelectElement>) => {
resetSelectedFile()
const target = e.target as HTMLSelectElement
setSelectedVersion(target.value)
const { value } = e.target as HTMLSelectElement
setSelectedVersion(value)
}

if (isLoading) return <div>Loading...</div>
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useDownloadUrl'
export * from './useFiles'
export * from './useStore'
export * from './useVersions'
9 changes: 9 additions & 0 deletions apps/web/src/hooks/useDownloadUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { cdnUrl, npmRegistryUrl } from '../constants'
import { useStore } from './useStore'

export const useDownloadUrl = () => {
const { selectedFile, selectedProject, selectedVersion, tickedDownloadAll } = useStore()
return tickedDownloadAll
? `${npmRegistryUrl}/@zk-kit/${selectedProject}-artifacts/-/${selectedProject}-artifacts-${selectedVersion}.tgz`
: `${cdnUrl}/${selectedProject}/${selectedVersion}/${selectedFile}`
}
29 changes: 25 additions & 4 deletions apps/web/src/hooks/useStore.ts
cedoor marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,41 @@ import { atom, useAtom } from 'jotai'
const selectedFileAtom = atom('')
const selectedProjectAtom = atom('')
const selectedVersionAtom = atom('')
const tickedDownloadAllAtom = atom(false)

export const useStore = () => {
const [selectedFile, setSelectedFile] = useAtom(selectedFileAtom)
const [selectedProject, setSelectedProject] = useAtom(selectedProjectAtom)
const [selectedVersion, setSelectedVersion] = useAtom(selectedVersionAtom)
const resetSelectedFile = () => setSelectedFile('')

const [selectedVersion, _setSelectedVersion] = useAtom(selectedVersionAtom)
const setSelectedVersion = (...args: Parameters<typeof _setSelectedVersion>) => {
resetSelectedFile()
_setSelectedVersion(...args)
}
const resetSelectedVersion = () => setSelectedVersion('')

const [tickedDownloadAll, _setTickedDownloadAll] = useAtom(tickedDownloadAllAtom)
const toggleTickedDownloadAll = () => {
resetSelectedFile()
_setTickedDownloadAll(prev => !prev)
}

const [selectedProject, _setSelectedProject] = useAtom(selectedProjectAtom)
const setSelectedProject = (...args: Parameters<typeof _setSelectedProject>) => {
resetSelectedVersion()
_setSelectedProject(...args)
}

return {
selectedFile,
resetSelectedFile: () => setSelectedFile(''),
resetSelectedFile,
setSelectedFile,
selectedProject,
setSelectedProject,
selectedVersion,
resetSelectedVersion: () => setSelectedVersion(''),
resetSelectedVersion,
setSelectedVersion,
tickedDownloadAll,
toggleTickedDownloadAll,
}
}
7 changes: 6 additions & 1 deletion apps/web/src/hooks/useVersions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useQuery } from 'react-query'
import rcompare from 'semver/functions/rcompare'
import { owner, repo } from '../constants'
import { gh } from '../gh'
import { useStore } from './useStore'
Expand All @@ -16,6 +17,10 @@ export function useVersions() {
queryKey: ['versions', selectedProject],
queryFn,
select: (data) =>
data.filter(tag => tag.includes(`@zk-kit/${selectedProject}-artifacts@`)).map(tag => tag.split('@')[2]),
data
.filter(tag => tag.includes(`@zk-kit/${selectedProject}-artifacts@`))
.map(tag => tag.split('@')[2])
.filter(version => version !== 'latest')
.sort(rcompare),
cedoor marked this conversation as resolved.
Show resolved Hide resolved
})
}
15 changes: 13 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading