Skip to content

Commit

Permalink
Best version yet
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jul 12, 2024
1 parent 8c7a5b2 commit 19a808a
Show file tree
Hide file tree
Showing 22 changed files with 260 additions and 1,058 deletions.
56 changes: 33 additions & 23 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,19 @@ type App struct {
titleTemplate *template.Template
Series dalle.Series `json:"series"`
names []types.Name
dallesCache map[string]*dalle.DalleDress
dalleCache map[string]*dalle.DalleDress
}

func NewApp() *App {
a := App{
databases: make(map[string][]string),
databases: make(map[string][]string),
dalleCache: make(map[string]*dalle.DalleDress),
}

// it's okay if it's not found
_ = a.session.Load()

var err error
if a.Series, err = a.LoadSeries(); err != nil {
logger.Fatal(err)
}
logger.Info("Loaded series:", a.Series.Suffix)

for _, db := range dalle.DatabaseNames {
if a.databases[db] == nil {
if lines, err := a.toLines(db); err != nil {
logger.Fatal(err)
} else {
a.databases[db] = lines
for i := 0; i < len(a.databases[db]); i++ {
a.databases[db][i] = strings.Replace(a.databases[db][i], "v0.1.0,", "", -1)
}
}
}
}
logger.Info("Loaded", len(dalle.DatabaseNames), "databases")

if a.promptTemplate, err = template.New("prompt").Parse(promptTemplate); err != nil {
logger.Fatal("could not create prompt template:", err)
}
Expand All @@ -82,6 +64,8 @@ func NewApp() *App {
}
logger.Info("Compiled templates")

a.ReloadDatabases()

return &a
}

Expand All @@ -90,9 +74,35 @@ func (a App) String() string {
return string(bytes)
}

func (a *App) ReloadDatabases() {
a.Series = dalle.Series{}
a.databases = make(map[string][]string)

var err error
if a.Series, err = a.LoadSeries(); err != nil {
logger.Fatal(err)
}
logger.Info("Loaded series:", a.Series.Suffix)

for _, db := range dalle.DatabaseNames {
if a.databases[db] == nil {
if lines, err := a.toLines(db); err != nil {
logger.Fatal(err)
} else {
a.databases[db] = lines
for i := 0; i < len(a.databases[db]); i++ {
a.databases[db][i] = strings.Replace(a.databases[db][i], "v0.1.0,", "", -1)
}
}
}
}
logger.Info("Loaded", len(dalle.DatabaseNames), "databases")
}

func (a *App) toLines(db string) ([]string, error) {
filename := "./databases/" + db + ".csv"
lines := file.AsciiFileToLines(filename)
lines = lines[1:] // skip header
var err error
if len(lines) == 0 {
err = fmt.Errorf("could not load %s", filename)
Expand Down Expand Up @@ -201,7 +211,7 @@ func (a *App) HandleLines() {
// msg := fmt.Sprintf("Content policy violation, skipping retry for address: %s Error: %s", address, err)
// logger.Error(msg)
// return
// } else
// } else
if strings.Contains(err.Error(), "seed length is less than 66") {
msg := fmt.Sprintf("Invalid address, skipping retry for address: %s Error: %s", address, err)
logger.Error(msg)
Expand Down Expand Up @@ -247,7 +257,7 @@ func (a *App) LoadSeries() (dalle.Series, error) {
return dalle.Series{}, err
}

s.Suffix = strings.ReplaceAll(s.Suffix, " ", "-")
s.Suffix = strings.Trim(strings.ReplaceAll(s.Suffix, " ", "-"), "-")
s.SaveSeries(filepath.Join("./output/series", s.Suffix+".json"), 0)
return s, nil
}
63 changes: 30 additions & 33 deletions app/app_state.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
package app

func (a *App) GetLastRoute() string {
return a.GetSession().LastRoute
}

func (a *App) GetLastTab() string {
return a.GetSession().LastTab
}

func (a *App) GetLastAddress() string {
return a.GetSession().LastAddress
}

func (a *App) GetLastSeries() string {
return a.GetSession().LastSeries
}

func (a *App) SetLastRoute(route string) {
a.GetSession().LastRoute = route
a.GetSession().Save()
}

func (a *App) SetLastTab(tab string) {
a.GetSession().LastTab = tab
a.GetSession().Save()
}

func (a *App) SetLastAddress(addr string) {
a.GetSession().LastAddress = addr
a.GetSession().Save()
}

func (a *App) SetLastSeries(series string) {
a.GetSession().LastSeries = series
func (a *App) GetLast(which string) string {
switch which {
case "route":
return a.GetSession().LastRoute
case "tab":
return a.GetSession().LastTab
case "address":
return a.GetSession().LastAddress
case "series":
return a.GetSession().LastSeries
}
return "Unknown"
}

func (a *App) SetLast(which, value string) {
reload := false
switch which {
case "route":
a.GetSession().LastRoute = value
case "tab":
a.GetSession().LastTab = value
case "address":
a.GetSession().LastAddress = value
case "series":
a.GetSession().LastSeries = value
reload = true
}
a.GetSession().Save()
if reload {
a.ReloadDatabases()
}
}
23 changes: 14 additions & 9 deletions app/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import (
var dalleCacheMutex sync.Mutex

func (a *App) MakeDalleDress(addressIn string) (*dalle.DalleDress, error) {
dalleCacheMutex.Lock()
defer dalleCacheMutex.Unlock()
if a.dalleCache[addressIn] != nil {
logger.Info("Returning cached dalle for", addressIn)
return a.dalleCache[addressIn], nil
}

address := addressIn
logger.Info("Making dalle for", addressIn)
if strings.HasSuffix(address, ".eth") {
opts := sdk.NamesOptions{
Terms: []string{address},
Expand All @@ -29,6 +37,7 @@ func (a *App) MakeDalleDress(addressIn string) (*dalle.DalleDress, error) {
}
}
}
logger.Info("Resolved", addressIn)

parts := strings.Split(address, ",")
seed := parts[0] + reverse(parts[0])
Expand All @@ -39,16 +48,10 @@ func (a *App) MakeDalleDress(addressIn string) (*dalle.DalleDress, error) {
seed = seed[2:66]
}

dalleCacheMutex.Lock()
defer dalleCacheMutex.Unlock()

if a.dallesCache == nil {
a.dallesCache = make(map[string]*dalle.DalleDress)
}
fn := validFilename(address)
if a.dallesCache[fn] != nil {
if a.dalleCache[fn] != nil {
logger.Info("Returning cached dalle for", addressIn)
return a.dallesCache[fn], nil
return a.dalleCache[fn], nil
}

dd := dalle.DalleDress{
Expand Down Expand Up @@ -86,7 +89,9 @@ func (a *App) MakeDalleDress(addressIn string) (*dalle.DalleDress, error) {
dd.EnhancedPrompt = file.AsciiFileToString(fn)
}

a.dallesCache[dd.Filename] = &dd
a.dalleCache[dd.Filename] = &dd
a.dalleCache[addressIn] = &dd

return &dd, nil
}

Expand Down
4 changes: 2 additions & 2 deletions app/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/TrueBlocks/trueblocks-dalledress/pkg/utils"
)

func (a *App) GetFilelist() []string {
folder := filepath.Join("./output/series")
func (a *App) GetFilelist(baseFolder string) []string {
folder := filepath.Join(baseFolder)
if list, err := utils.Listing(folder); err != nil {
return []string{err.Error()}
} else {
Expand Down
25 changes: 23 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import React from "react";
import React, { useEffect } from "react";
import { AppShell, Text } from "@mantine/core";
import Aside from "./components/global/Aside";
import Header from "./components/global/Header";
import Navbar from "./components/global/Navbar";
import Routes from "./components/global/Routes";
import classes from "@/App.module.css";
import { EventsOn, EventsOff, EventsEmit } from "../wailsjs/runtime";

function App() {
const [showHelp, setShowHelp] = React.useState(true);

const sendMessage = () => {
EventsEmit("toggleHelp", "Hello from SenderComponent");
setShowHelp(!showHelp);
};

useEffect(() => {
const messageListener = (message: string) => {
console.log("Received in help component:", message);
};

EventsOn("toggleHelp", messageListener);

return () => {
EventsOff("toggleHelp");
};
}, [showHelp]);

return (
<AppShell
header={{ height: "3rem" }}
navbar={{ collapsed: { desktop: false }, width: "10rem", breakpoint: 0 }}
aside={{ collapsed: { desktop: false }, width: "10rem", breakpoint: 0 }}
aside={{ collapsed: { desktop: showHelp }, width: "10rem", breakpoint: 0 }}
footer={{ height: "2rem" }}
>
<AppShell.Header>
Expand All @@ -28,6 +48,7 @@ function App() {
</AppShell.Aside>
<AppShell.Footer>
<Text size={"sm"}>time / date / currently opened file</Text>
<button onClick={sendMessage}>Send Message</button>{" "}
</AppShell.Footer>
</AppShell>
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/ImageDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const ImageDisplay = ({ address, loading }: { address: string; loading: b

return (
<div>
<Text>{title}</Text>
<Image style={{ height: "700px" }} fit="contain" src={imgSrc} alt={address} />
<Text color="white">{title}</Text>
<Image fit="contain" src={imgSrc} alt={address} />
</div>
);
};
25 changes: 24 additions & 1 deletion frontend/src/components/StringTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React, { useState } from "react";
import { Table, Checkbox } from "@mantine/core";
import { Link, useLocation } from "wouter";
import { Anchor } from "@mantine/core";
import { SetLast } from "@gocode/app/App";

export interface DataItem {
id: number;
Expand All @@ -12,6 +15,16 @@ interface StringTableProps {

const StringTable: React.FC<StringTableProps> = ({ data }) => {
const [selectedIds, setSelectedIds] = useState<number[]>([]);
const [location, setLocation] = useLocation();

const handleSeriesClick = async (seriesName: string) => {
try {
await SetLast("series", seriesName);
setLocation("/dalle");
} catch (error) {
console.error("Error updating data:", error);
}
};

const handleCheckboxChange = (id: number) => {
setSelectedIds((prevSelectedIds) =>
Expand All @@ -33,7 +46,17 @@ const StringTable: React.FC<StringTableProps> = ({ data }) => {
<Table.Td>
<Checkbox checked={selectedIds.includes(item.id)} onChange={() => handleCheckboxChange(item.id)} />
</Table.Td>
<Table.Td>{item.value}</Table.Td>
<Table.Td>
<Link
href="#"
onClick={(e) => {
e.preventDefault();
handleSeriesClick(item.value);
}}
>
<Anchor>{item.value}</Anchor>
</Link>
</Table.Td>
</Table.Tr>
));

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/global/Aside.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import React, { useEffect } from "react";
import { EventsOn, EventsOff } from "../../../wailsjs/runtime";
import { Text } from "@mantine/core";

function Aside() {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/global/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import React, { ReactNode, useEffect, useState } from "react";
import { NavLink } from "@mantine/core";
import { IconHome, IconSettings, IconTag, IconList, IconSpider } from "@tabler/icons-react";
import { Link, useRoute } from "wouter";
import { GetLastRoute, SetLastRoute } from "@gocode/app/App";
import { GetLast, SetLast } from "@gocode/app/App";

function Menu() {
const [activeRoute, setActiveRoute] = useState("/");

useEffect(() => {
const lastRoute = (GetLastRoute() || "/").then((route) => {
const lastRoute = (GetLast("route") || "/").then((route) => {
setActiveRoute(route);
});
}, []);

const handleRouteChange = (route: string) => {
SetLastRoute(route);
SetLast("route", route);
setActiveRoute(route);
};

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/global/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React, { useEffect } from "react";
import { Route, Switch, useLocation } from "wouter";
import classes from "@/App.module.css";
import { DalleView, NamesView, HomeView, SeriesView, SettingsView } from "@/views";
import { GetLastRoute } from "@gocode/app/App";
import { GetLast } from "@gocode/app/App";

export const Routes = () => {
const [, setLocation] = useLocation();

useEffect(() => {
const lastRoute = (GetLastRoute() || "/").then((route) => {
const lastRoute = (GetLast("route") || "/").then((route) => {
setLocation(route);
});
}, [setLocation]);
Expand Down
Loading

0 comments on commit 19a808a

Please sign in to comment.