Fixed to retrieve the template header file from gist (#316)

This commit is contained in:
tequ
2025-02-13 22:32:17 +09:00
committed by GitHub
parent 6ed0b54fc5
commit 12ebd4c1bc
2 changed files with 35 additions and 13 deletions

View File

@@ -24,19 +24,28 @@ export const fetchFiles = async (gistId: string) => {
.includes(id)
if (isTemplate(gistId)) {
// fetch headers
const headerRes = await fetch(
`${process.env.NEXT_PUBLIC_COMPILE_API_BASE_URL}/api/header-files`
)
if (!headerRes.ok) throw Error('Failed to fetch headers')
const headerJson = await headerRes.json()
const headerFiles: Record<string, { filename: string; content: string; language: string }> =
const template = Object.values(templateFileIds).find(tmp => tmp.id === gistId)
let headerFiles: Record<string, { filename: string; content: string; language: string }> =
{}
Object.entries(headerJson).forEach(([key, value]) => {
const fname = `${key}.h`
headerFiles[fname] = { filename: fname, content: value as string, language: 'C' }
})
if (template?.headerId) {
const resHeader = await octokit.request('GET /gists/{gist_id}', { gist_id: template.headerId })
if (!resHeader.data.files) throw new Error('No header files could be fetched from given gist id!')
headerFiles = resHeader.data.files as any
} else {
// fetch headers
const headerRes = await fetch(
`${process.env.NEXT_PUBLIC_COMPILE_API_BASE_URL}/api/header-files`
)
if (!headerRes.ok) throw Error('Failed to fetch headers')
const headerJson = await headerRes.json()
const headerFiles: Record<string, { filename: string; content: string; language: string }> =
{}
Object.entries(headerJson).forEach(([key, value]) => {
const fname = `${key}.h`
headerFiles[fname] = { filename: fname, content: value as string, language: 'C' }
})
}
const files = {
...res.data.files,
...headerFiles

View File

@@ -4,36 +4,49 @@ import Notary from '../../components/icons/Notary'
import Peggy from '../../components/icons/Peggy'
import Starter from '../../components/icons/Starter'
export const templateFileIds = {
type Template = {
id: string
name: string
description: string
headerId?: string
icon: () => JSX.Element
}
export const templateFileIds: Record<string, Template> = {
starter: {
id: '1f8109c80f504e6326db2735df2f0ad6', // Forked
name: 'Starter',
description:
'Just a basic starter with essential imports, just accepts any transaction coming through',
headerId: '028e8ce6d6d674776970caf8acc77ecc',
icon: Starter
},
firewall: {
id: '1cc30f39c8a0b9c55b88c312669ca45e', // Forked
name: 'Firewall',
description: 'This Hook essentially checks a blacklist of accounts',
headerId: '028e8ce6d6d674776970caf8acc77ecc',
icon: Firewall
},
notary: {
id: '87b6f5a8c2f5038fb0f20b8b510efa10', // Forked
name: 'Notary',
description: 'Collecting signatures for multi-sign transactions',
headerId: '028e8ce6d6d674776970caf8acc77ecc',
icon: Notary
},
carbon: {
id: '953662b22d065449f8ab6f69bc2afe41', // Forked
name: 'Carbon',
description: 'Send a percentage of sum to an address',
headerId: '028e8ce6d6d674776970caf8acc77ecc',
icon: Carbon
},
peggy: {
id: '049784a83fa068faf7912f663f7b6471', // Forked
name: 'Peggy',
description: 'An oracle based stable coin hook',
headerId: '028e8ce6d6d674776970caf8acc77ecc',
icon: Peggy
}
}