| 1 | import {
|
|---|
| 2 | cablesTable, caseMoboFormFactorsTable, casePsFormFactorsTable,
|
|---|
| 3 | caseStorageFormFactorsTable, coolerCPUSocketsTable, coolersTable,
|
|---|
| 4 | CPUTable,
|
|---|
| 5 | GPUTable, memoryCardsTable,
|
|---|
| 6 | memoryTable, motherboardsTable,
|
|---|
| 7 | networkAdaptersTable,
|
|---|
| 8 | networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable,
|
|---|
| 9 | soundCardsTable, storageTable
|
|---|
| 10 | } from "../schema";
|
|---|
| 11 |
|
|---|
| 12 | export type ComponentConfig<SelectType = any, InsertType = any> = {
|
|---|
| 13 | table: any;
|
|---|
| 14 | multiTables?: {
|
|---|
| 15 | storageFormFactors?: typeof caseStorageFormFactorsTable;
|
|---|
| 16 | psFormFactors?: typeof casePsFormFactorsTable;
|
|---|
| 17 | moboFormFactors?: typeof caseMoboFormFactorsTable;
|
|---|
| 18 | cpuSockets?: typeof coolerCPUSocketsTable;
|
|---|
| 19 | }
|
|---|
| 20 | };
|
|---|
| 21 |
|
|---|
| 22 | export type ComponentType =
|
|---|
| 23 | | 'cpu'
|
|---|
| 24 | | 'gpu'
|
|---|
| 25 | | 'memory'
|
|---|
| 26 | | 'storage'
|
|---|
| 27 | | 'power_supply'
|
|---|
| 28 | | 'motherboard'
|
|---|
| 29 | | 'case'
|
|---|
| 30 | | 'cooler'
|
|---|
| 31 | | 'memory_card'
|
|---|
| 32 | | 'optical_drive'
|
|---|
| 33 | | 'sound_card'
|
|---|
| 34 | | 'cables'
|
|---|
| 35 | | 'network_adapter'
|
|---|
| 36 | | 'network_card';
|
|---|
| 37 |
|
|---|
| 38 | export const typeConfigMap: Record<ComponentType, ComponentConfig> = {
|
|---|
| 39 | cpu: { table: CPUTable },
|
|---|
| 40 | gpu: { table: GPUTable },
|
|---|
| 41 | memory: { table: memoryTable },
|
|---|
| 42 | storage: { table: storageTable },
|
|---|
| 43 | power_supply: { table: powerSupplyTable },
|
|---|
| 44 | motherboard: { table: motherboardsTable },
|
|---|
| 45 | case: {
|
|---|
| 46 | table: pcCasesTable,
|
|---|
| 47 | multiTables: {
|
|---|
| 48 | storageFormFactors: caseStorageFormFactorsTable,
|
|---|
| 49 | psFormFactors: casePsFormFactorsTable,
|
|---|
| 50 | moboFormFactors: caseMoboFormFactorsTable,
|
|---|
| 51 | },
|
|---|
| 52 | },
|
|---|
| 53 | cooler: {
|
|---|
| 54 | table: coolersTable,
|
|---|
| 55 | multiTables: {
|
|---|
| 56 | cpuSockets: coolerCPUSocketsTable,
|
|---|
| 57 | },
|
|---|
| 58 | },
|
|---|
| 59 | memory_card: { table: memoryCardsTable },
|
|---|
| 60 | optical_drive: { table: opticalDrivesTable },
|
|---|
| 61 | sound_card: { table: soundCardsTable },
|
|---|
| 62 | cables: { table: cablesTable },
|
|---|
| 63 | network_adapter: { table: networkAdaptersTable },
|
|---|
| 64 | network_card: { table: networkCardsTable },
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | export const requiredFields: Record<ComponentType, string[]> = {
|
|---|
| 68 | cpu: ['socket', 'cores', 'threads', 'baseClock', 'tdp'],
|
|---|
| 69 | gpu: ['vram', 'tdp', 'chipset', 'length'],
|
|---|
| 70 | memory: ['type', 'speed', 'capacity', 'modules'],
|
|---|
| 71 | storage: ['type', 'capacity', 'formFactor'],
|
|---|
| 72 | power_supply: ['type', 'wattage', 'formFactor'],
|
|---|
| 73 | motherboard: ['socket', 'chipset', 'formFactor', 'ramType', 'numRamSlots', 'maxRamCapacity', 'pciExpressSlots'],
|
|---|
| 74 | case: ['coolerMaxHeight', 'gpuMaxLength'],
|
|---|
| 75 | cooler: ['type', 'height', 'maxTdpSupported'],
|
|---|
| 76 | memory_card: ['numSlots', 'interface'],
|
|---|
| 77 | optical_drive: ['formFactor', 'type', 'interface', 'writeSpeed', 'readSpeed'],
|
|---|
| 78 | sound_card: ['sampleRate', 'bitDepth', 'chipset', 'interface', 'channel'],
|
|---|
| 79 | cables: ['lengthCm', 'type'],
|
|---|
| 80 | network_adapter: ['wifiVersion', 'interface', 'numAntennas'],
|
|---|
| 81 | network_card: ['numPorts', 'speed', 'interface']
|
|---|
| 82 | };
|
|---|
| 83 |
|
|---|
| 84 | export function validateComponentSpecificData(type: string, specificData: any): boolean {
|
|---|
| 85 | if (!(type in requiredFields)) {
|
|---|
| 86 | return false;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | const fields = requiredFields[type as ComponentType];
|
|---|
| 90 |
|
|---|
| 91 | for (const field of fields) {
|
|---|
| 92 | const value = specificData[field];
|
|---|
| 93 | if (value === undefined || value === null || value === '') {
|
|---|
| 94 | return false;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if (type === 'case') {
|
|---|
| 99 | if (specificData.storageFormFactors && !Array.isArray(specificData.storageFormFactors)) return false;
|
|---|
| 100 | if (specificData.psFormFactors && !Array.isArray(specificData.psFormFactors)) return false;
|
|---|
| 101 | if (specificData.moboFormFactors && !Array.isArray(specificData.moboFormFactors)) return false;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | if (type === 'cooler') {
|
|---|
| 105 | if (specificData.cpuSockets && !Array.isArray(specificData.cpuSockets)) return false;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | return true;
|
|---|
| 109 | } |
|---|