Skip to content

Commit

Permalink
refactor: add zod
Browse files Browse the repository at this point in the history
  • Loading branch information
kul-sudo committed Nov 15, 2023
1 parent b559032 commit 4f049b3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 243 deletions.
Binary file modified bun.lockb
Binary file not shown.
12 changes: 2 additions & 10 deletions components/WordWhenSearching.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FC } from 'react'
import { useCallback, useEffect, useState } from 'react'
import { colors, wordsWhenSearching } from '@/lib/consts'
import { Text } from '@chakra-ui/react'
import { wordsWhenSearching } from '@/lib/consts'

const WordWhenSearching: FC = () => {
const [word, setWord] = useState<string>(
Expand All @@ -20,15 +20,7 @@ const WordWhenSearching: FC = () => {
return () => clearInterval(interval)
}, [memorisedSetWord])

return (
<Text
p="1"
rounded="lg"
backgroundColor={colors[~~(colors.length * Math.random())]}
>
{word}
</Text>
)
return <Text>{word}</Text>
}

export default WordWhenSearching
223 changes: 0 additions & 223 deletions lib/consts.ts
Original file line number Diff line number Diff line change
@@ -1,226 +1,3 @@
export const colors = Object.freeze([
'#fff1f2',
'#ffe4e6',
'#fecdd3',
'#fda4af',
'#fb7185',
'#f43f5e',
'#e11d48',
'#be123c',
'#9f1239',
'#881337',
'#fdf2f8',
'#fce7f3',
'#fbcfe8',
'#f9a8d4',
'#f472b6',
'#ec4899',
'#db2777',
'#be185d',
'#9d174d',
'#831843',
'#fdf4ff',
'#fae8ff',
'#f5d0fe',
'#f0abfc',
'#e879f9',
'#d946ef',
'#c026d3',
'#a21caf',
'#86198f',
'#701a75',
'#faf5ff',
'#f3e8ff',
'#e9d5ff',
'#d8b4fe',
'#c084fc',
'#a855f7',
'#9333ea',
'#7e22ce',
'#6b21a8',
'#581c87',
'#f5f3ff',
'#ede9fe',
'#ddd6fe',
'#c4b5fd',
'#a78bfa',
'#8b5cf6',
'#7c3aed',
'#6d28d9',
'#5b21b6',
'#4c1d95',
'#eef2ff',
'#e0e7ff',
'#c7d2fe',
'#a5b4fc',
'#818cf8',
'#6366f1',
'#4f46e5',
'#4338ca',
'#3730a3',
'#312e81',
'#eff6ff',
'#dbeafe',
'#bfdbfe',
'#93c5fd',
'#60a5fa',
'#3b82f6',
'#2563eb',
'#1d4ed8',
'#1e40af',
'#1e3a8a',
'#f0f9ff',
'#e0f2fe',
'#bae6fd',
'#7dd3fc',
'#38bdf8',
'#0ea5e9',
'#0284c7',
'#0369a1',
'#075985',
'#0c4a6e',
'#ecfeff',
'#cffafe',
'#a5f3fc',
'#67e8f9',
'#22d3ee',
'#06b6d4',
'#0891b2',
'#0e7490',
'#155e75',
'#164e63',
'#f0fdfa',
'#ccfbf1',
'#99f6e4',
'#5eead4',
'#2dd4bf',
'#14b8a6',
'#0d9488',
'#0f766e',
'#115e59',
'#134e4a',
'#ecfdf5',
'#d1fae5',
'#a7f3d0',
'#6ee7b7',
'#34d399',
'#10b981',
'#059669',
'#047857',
'#065f46',
'#064e3b',
'#f0fdf4',
'#dcfce7',
'#bbf7d0',
'#86efac',
'#4ade80',
'#22c55e',
'#16a34a',
'#15803d',
'#166534',
'#14532d',
'#f7fee7',
'#ecfccb',
'#d9f99d',
'#bef264',
'#a3e635',
'#84cc16',
'#65a30d',
'#4d7c0f',
'#3f6212',
'#365314',
'#fefce8',
'#fef9c3',
'#fef08a',
'#fde047',
'#facc15',
'#eab308',
'#ca8a04',
'#a16207',
'#854d0e',
'#713f12',
'#fffbeb',
'#fef3c7',
'#fde68a',
'#fcd34d',
'#fbbf24',
'#f59e0b',
'#d97706',
'#b45309',
'#92400e',
'#78350f',
'#fff7ed',
'#ffedd5',
'#fed7aa',
'#fdba74',
'#fb923c',
'#f97316',
'#ea580c',
'#c2410c',
'#9a3412',
'#7c2d12',
'#fef2f2',
'#fee2e2',
'#fecaca',
'#fca5a5',
'#f87171',
'#ef4444',
'#dc2626',
'#b91c1c',
'#991b1b',
'#7f1d1d',
'#fafaf9',
'#f5f5f4',
'#e7e5e4',
'#d6d3d1',
'#a8a29e',
'#78716c',
'#57534e',
'#44403c',
'#292524',
'#1c1917',
'#fafafa',
'#f5f5f5',
'#e5e5e5',
'#d4d4d4',
'#a3a3a3',
'#737373',
'#525252',
'#404040',
'#262626',
'#171717',
'#fafafa',
'#f4f4f5',
'#e4e4e7',
'#d4d4d8',
'#a1a1aa',
'#71717a',
'#52525b',
'#3f3f46',
'#27272a',
'#18181b',
'#f9fafb',
'#f3f4f6',
'#e5e7eb',
'#d1d5db',
'#9ca3af',
'#6b7280',
'#4b5563',
'#374151',
'#1f2937',
'#111827',
'#f8fafc',
'#f1f5f9',
'#e2e8f0',
'#cbd5e1',
'#94a3b8',
'#64748b',
'#475569',
'#334155',
'#1e293b',
'#0f172a'
])

export const settings = Object.freeze({
'Partially disable animations': {
isChecked: true,
Expand Down
21 changes: 21 additions & 0 deletions lib/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { z } from 'zod'
import type { ZodType } from 'zod'
import type { AddEventProps, VolumesListProps } from '@/types/types'

export const VolumesSchema: ZodType<VolumesListProps> = z.array(
z.object({
is_removable: z.boolean(),
kind: z.literal('HDD').or(z.literal('SSD')),
mountpoint: z.string(),
available_gb: z.number(),
used_gb: z.number(),
total_gb: z.number()
})
)

export const AddSchema: ZodType<AddEventProps> = z.object({
is_folder: z.boolean(),
name: z.string(),
path: z.string(),
extension: z.string()
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"react-dom": "latest",
"react-icons": "latest",
"react-virtuoso": "^4.6.2",
"typescript": "latest"
"typescript": "latest",
"zod": "^3.22.4"
},
"devDependencies": {
"@tauri-apps/cli": "^1.4.0",
Expand Down
19 changes: 10 additions & 9 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { FC } from 'react'
import type {
AddEventProps,
SearchingModeValue,
VolumesListProps
} from '@/types/types'
import { path, window } from '@tauri-apps/api'
import type { SearchingModeValue, VolumesListProps } from '@/types/types'
import type { path, window } from '@tauri-apps/api'
import { useState, useEffect, useRef, useCallback } from 'react'
import { invoke } from '@tauri-apps/api/tauri'
import { listen } from '@tauri-apps/api/event'
Expand Down Expand Up @@ -87,6 +83,7 @@ import { Virtuoso } from 'react-virtuoso'
import useUndoRedo from '@/lib/useUndoRedo'
import FileOrFolderItem from '@/components/FileOrFolderItem'
import WordWhenSearching from '@/components/WordWhenSearching'
import { AddSchema, VolumesSchema } from '@/lib/schemas'

const Home: FC = () => {
// Checkbox states
Expand Down Expand Up @@ -155,8 +152,11 @@ const Home: FC = () => {

// Preventing re-rendering
const memorisedSetReadDirArray = useCallback(() => {
const unlisten = listen('add', (event: { payload: AddEventProps }) => {
setReadDirArray(prevValue => [...prevValue, event.payload])
const unlisten = listen('add', event => {
setReadDirArray(prevValue => [
...prevValue,
AddSchema.parse(event.payload)
])
})

return () => {
Expand Down Expand Up @@ -213,6 +213,7 @@ const Home: FC = () => {

if (currentDirectory.length > 0) {
invoke('read_directory', { directory: currentDirectory }).then(() => {
setSearchingStopped(false)
setIsLoading(false)

setLastTime({
Expand All @@ -234,7 +235,7 @@ const Home: FC = () => {

useEffect(() => {
invoke('get_volumes').then(volumes => {
setVolumesList(volumes as VolumesListProps)
setVolumesList(VolumesSchema.parse(volumes))
setIsLoadingVolumes(false)
})
}, [setVolumesList, setIsLoadingVolumes])
Expand Down

0 comments on commit 4f049b3

Please sign in to comment.