diff --git a/state/actions/fetchFiles.ts b/state/actions/fetchFiles.ts index fc6fd00..74fd87c 100644 --- a/state/actions/fetchFiles.ts +++ b/state/actions/fetchFiles.ts @@ -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 = + const template = Object.values(templateFileIds).find(tmp => tmp.id === gistId) + let headerFiles: Record = {} - 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 = + {} + 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 diff --git a/state/constants/templates.ts b/state/constants/templates.ts index 2b4a5de..df8723e 100644 --- a/state/constants/templates.ts +++ b/state/constants/templates.ts @@ -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 = { 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 } }