Skip to content

Commit

Permalink
refactoring and add missing types
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyblue committed Jul 25, 2024
1 parent d3a97ca commit babaf13
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
38 changes: 28 additions & 10 deletions gui/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { NextUIProvider } from "@nextui-org/react"
import React from "react"
import { createRoot } from "react-dom/client"
import Button from "./components/Button"
import Button from "./components/nextui/Button"
import { Config, defaultConfig } from "./config"

interface Config {
[key: string]: string
type Health = {
check: () => Promise<void>
}

const App = () => {
const [filePath, setFilePath] = React.useState("")
const [config, setConfig] = React.useState({})
const [filePath, setFilePath] = React.useState<string>("")
const [config, setConfig] = React.useState<Config | null>(null)

React.useEffect(() => {
if (config === null) {
console.log("setting default config")
setConfig(defaultConfig)
localStorage.setItem("config", JSON.stringify(defaultConfig))
}
}, [config])

React.useEffect(() => {
const cfg = localStorage.getItem("config")
if (cfg) {
console.log("config from local storage:", JSON.parse(cfg))
setConfig(JSON.parse(cfg))
}

setInterval(() => {
window.health.check()
}, 5000)
Expand All @@ -20,7 +34,7 @@ const App = () => {
React.useEffect(() => {
if (filePath) {
const readFile = async () => {
const content: Config = await window.electronAPI.readFile(filePath)
const content = await window.electronAPI.readFile<Partial<Config>>(filePath)
const newConfig = { ...config, ...content }
setConfig(newConfig)
}
Expand All @@ -44,7 +58,11 @@ const App = () => {

const root = createRoot(document.getElementById("root"))
root.render(
<NextUIProvider>
<App />
</NextUIProvider>
<React.StrictMode>
<NextUIProvider>
<main className="dark text-foreground bg-background">
<App />
</main>
</NextUIProvider>
</React.StrictMode>
)
File renamed without changes.
41 changes: 41 additions & 0 deletions gui/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type Auth = {
api_key: string
api_secret: string
user_token: string
user_secret: string
}

type Store = {
destination: string
file_names: string
use_metadata_times: boolean
force_metadata_times: boolean
write_csv: boolean
force_video_download: boolean
concurrent_albums: number
concurrent_downloads: number
}

export type Config = {
auth: Auth
store: Store
}

export const defaultConfig: Config = {
auth: {
api_key: "",
api_secret: "",
user_token: "",
user_secret: "",
},
store: {
destination: "",
file_names: "",
use_metadata_times: true,
force_metadata_times: true,
write_csv: true,
force_video_download: true,
concurrent_albums: 5,
concurrent_downloads: 10,
},
}
13 changes: 13 additions & 0 deletions gui/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export {}

declare global {
interface Window {
electronAPI: {
openFile: () => Promise<string[]>
readFile: <T>(path: string) => Promise<T>
}
health: {
check: () => Promise<void>
}
}
}
2 changes: 1 addition & 1 deletion gui/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import grpc from "@grpc/grpc-js"
import protoLoader from "@grpc/proto-loader"
import { app, BrowserWindow, dialog, ipcMain } from "electron"
import fs from "fs"

import path from "path"

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require("electron-squirrel-startup")) {
app.quit()
Expand Down

0 comments on commit babaf13

Please sign in to comment.