Skip to content

Commit

Permalink
refactor page-game components + overwrite saves button
Browse files Browse the repository at this point in the history
  • Loading branch information
nicanordlc committed Jun 5, 2024
1 parent 3d69168 commit 7e6d4f7
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 107 deletions.
8 changes: 7 additions & 1 deletion backend/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"context"
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -50,18 +51,23 @@ func (g *Game) AddGame(name, savePath string, isFile bool) uuid.UUID {
return id
}

func (g *Game) RemoveGame(id uuid.UUID) {
func (g *Game) RemoveGame(id uuid.UUID) error {
newList := []GameSingle{}
for _, game := range g.JsonGame.Data {
if game.ID == id {
continue
}
newList = append(newList, game)
}
if len(newList) == len(g.JsonGame.Data) {
errorMsg := fmt.Sprintf("no element deleted: %v", id)
return errors.New(errorMsg)
}
g.JsonGame.Data = newList
g.updateJson()
g.removeGameDir(id)
g.logf("Deleted: %v", id)
return nil
}

func (g *Game) ReadGames() (*JsonGame, error) {
Expand Down
11 changes: 8 additions & 3 deletions backend/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ func (s *Save) LoadSave(saveID, gameID uuid.UUID) error {
return nil
}

func (s *Save) OverwriteSave(saveID, gameID uuid.UUID) error {
err := s.copyGameContent(gameID, saveID)
if err != nil {
return err
}
return nil
}

func (s *Save) ReadSaves() (*JsonSave, error) {
saveJson, err := utils.ReadConfigFrom[JsonSave](s.filename)
return saveJson, err
Expand Down Expand Up @@ -134,9 +142,6 @@ func (s *Save) copySaveContent(saveID, gameID uuid.UUID) error {
if err != nil {
return err
}
if err = os.RemoveAll(gamePath); err != nil {
return err
}
if err = utils.CopyDir(savePath, gamePath); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions backend/utils/copy-dir.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package utils

import (
"os"

cp "github.com/otiai10/copy"
)

func CopyDir(src, dst string) error {
// hard copy (remove target dir and then copy)
if err := os.RemoveAll(dst); err != nil {
return err
}
return cp.Copy(src, dst)
}
55 changes: 55 additions & 0 deletions frontend/src/components/QuickSaveChip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Button, Chip } from "@material-tailwind/react";
import clsx from "clsx";
import { FaFolderOpen } from "react-icons/fa6";
import WithTooltip from "@/components/WithTooltip";

type QuickSaveChipProps = {
enabled: boolean;
onClose: () => void;
onOpen: () => void;
};

const QuickSaveChip = (props: QuickSaveChipProps) => {
return (
<div className="flex gap-4">
<Chip
className={clsx({
"[&_button]:pointer-events-none [&_button]:opacity-50":
!props.enabled,
})}
variant="ghost"
color={props.enabled ? "green" : "red"}
size="sm"
value={
<div className="flex items-center gap-2">
<span>Quick Save</span>

<WithTooltip content="Open" placement="top">
<Button
onClick={() => props.onOpen()}
className="p-0.5"
variant="text"
>
<FaFolderOpen size={12} />
</Button>
</WithTooltip>
</div>
}
onClose={() => props.onClose()}
icon={
<span
className={clsx(
"mx-auto mt-1 block h-2 w-2 rounded-full content-['']",
{
"bg-red-900": !props.enabled,
"bg-green-900": props.enabled,
},
)}
/>
}
/>
</div>
);
};

export default QuickSaveChip;
77 changes: 77 additions & 0 deletions frontend/src/components/Save.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Button, Typography } from "@material-tailwind/react";
import { FaDownload, FaFolderOpen, FaTrash, FaUpload } from "react-icons/fa6";
import { type SaveSingle } from "@/hooks/useSave";
import WithTooltip from "@/components/WithTooltip";

type SavePropsCallback = () => Promise<void> | void;

type SaveProps = {
data: SaveSingle;
onLoad: SavePropsCallback;
onSave: SavePropsCallback;
onDelete: SavePropsCallback;
onOpenDirectory: SavePropsCallback;
};

const Save = (props: SaveProps) => {
const formatDate = (dateString: string) => {
const date = new Date(dateString);
const intlDate = new Intl.DateTimeFormat("en-US").format(date);
return intlDate;
};

return (
<div
className="grid grid-cols-12 items-center justify-between break-all even:bg-blue-gray-50/50"
key={props.data.Name}
>
<div className="col-span-7 flex items-center gap-2">
<div>
<WithTooltip content="Load" placement="top">
<Button
onClick={() => props.onLoad()}
className="p-3"
variant="text"
>
<FaUpload size={15} />
</Button>
</WithTooltip>

<WithTooltip content="Save" placement="top">
<Button
onClick={() => props.onSave()}
className="p-3"
variant="text"
>
<FaDownload size={15} />
</Button>
</WithTooltip>
</div>

<Typography>{props.data.Name}</Typography>
</div>

<Typography className="col-span-3 px-2">
{formatDate(props.data.CreatedAt)}
</Typography>

<WithTooltip content="Open" placement="top">
<Button
onClick={() => props.onOpenDirectory()}
className="p-3"
variant="text"
>
<FaFolderOpen size={15} />
</Button>
</WithTooltip>

<WithTooltip content="Delete" placement="top">
<Button onClick={() => props.onDelete()} className="p-3" variant="text">
<FaTrash size={15} />
</Button>
</WithTooltip>
</div>
);
};

export default Save;
26 changes: 26 additions & 0 deletions frontend/src/components/WithTooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Tooltip, Typography } from "@material-tailwind/react";
import { type ReactElement } from "react";

type WithTooltipProps = {
children: ReactElement;
content: string;
placement: string;
};

const WithTooltip = (props: WithTooltipProps) => {
return (
<Tooltip
content={
<Typography className="text-black" variant="small">
{props.content}
</Typography>
}
placement={props.placement}
className="border border-blue-gray-50 bg-white shadow-xl shadow-black/10"
>
{props.children}
</Tooltip>
);
};

export default WithTooltip;
7 changes: 7 additions & 0 deletions frontend/src/hooks/useSave.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GetSaves,
LoadQuickSave,
LoadSave,
OverwriteSave,
RemoveQuickSave,
RemoveSave,
} from "@wailsjs/go/backend/Save";
Expand Down Expand Up @@ -70,6 +71,11 @@ const useSave = (props?: Pick<SaveSingle, "GameID">) => {
mutationFn: (s: Pick<SaveSingle, "GameID">) => LoadQuickSave(s.GameID),
});

const { mutateAsync: overwriteSave } = useMutation({
mutationFn: (s: Pick<SaveSingle, "ID" | "GameID">) =>
OverwriteSave(s.ID, s.GameID),
});

return {
querySaves,
queryQuickSave,
Expand All @@ -79,6 +85,7 @@ const useSave = (props?: Pick<SaveSingle, "GameID">) => {
removeQuickSave,
loadSave,
loadQuickSave,
overwriteSave,
};
};

Expand Down
Loading

0 comments on commit 7e6d4f7

Please sign in to comment.