From cea48a1545250429b78235b2ad00b8243923e2b2 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Mon, 29 Apr 2024 14:59:59 +0100 Subject: feat(ui/dotfiles): add vars (#1989) --- ui/src/components/dotfiles/Vars.tsx | 194 ++++++++++++++++++++++++++++++++++++ ui/src/pages/Dotfiles.tsx | 90 ++++++++++++++++- ui/src/state/models.ts | 6 ++ ui/src/state/store.ts | 9 ++ 4 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 ui/src/components/dotfiles/Vars.tsx (limited to 'ui/src') diff --git a/ui/src/components/dotfiles/Vars.tsx b/ui/src/components/dotfiles/Vars.tsx new file mode 100644 index 00000000..00317b23 --- /dev/null +++ b/ui/src/components/dotfiles/Vars.tsx @@ -0,0 +1,194 @@ +import { useEffect, useState } from "react"; + +import DataTable from "@/components/ui/data-table"; +import { Button } from "@/components/ui/button"; +import { MoreHorizontal } from "lucide-react"; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuLabel, + DropdownMenuTrigger, +} from "@/components/ui/dropdown-menu"; + +import { ColumnDef } from "@tanstack/react-table"; + +import { invoke } from "@tauri-apps/api/core"; +import Drawer from "@/components/Drawer"; + +import { Var } from "@/state/models"; +import { useStore } from "@/state/store"; + +function deleteVar(name: string, refreshVars: () => void) { + invoke("delete_var", { name: name }) + .then(() => { + refreshVars(); + }) + .catch(() => { + console.error("Failed to delete var"); + }); +} + +function AddVar({ onAdd: onAdd }: { onAdd?: () => void }) { + let [name, setName] = useState(""); + let [value, setValue] = useState(""); + let [exp, setExport] = useState(false); + + // simple form to add vars + return ( +
+

Add var

+

Add a new var to your shell

+ +
{ + e.preventDefault(); + + invoke("set_var", { name: name, value: value, export: exp }) + .then(() => { + console.log("Added var"); + + if (onAdd) onAdd(); + }) + .catch(() => { + console.error("Failed to add var"); + }); + }} + > + setName(e.target.value)} + placeholder="Var name" + /> + + setValue(e.target.value)} + placeholder="Var value" + /> + +
+ +
+ + +
+
+ ); +} + +export default function Vars() { + const vars = useStore((state) => state.vars); + const refreshVars = useStore((state) => state.refreshVars); + + let [varDrawerOpen, setVarDrawerOpen] = useState(false); + + const columns: ColumnDef[] = [ + { + accessorKey: "name", + header: "Name", + }, + { + accessorKey: "value", + header: "Value", + }, + { + id: "actions", + cell: ({ row }: any) => { + const shell_var = row.original; + + return ( + + + + + + Actions + deleteVar(shell_var.name, refreshVars)} + > + Delete + + + + ); + }, + }, + ]; + + useEffect(() => { + refreshVars(); + }, []); + + return ( +
+
+
+

+ Vars +

+

+ Configure environment variables here +

+
+
+ + Add + + } + > + { + refreshVars(); + setVarDrawerOpen(false); + }} + /> + +
+
+
+
+
+ +
+
+
+
+ ); +} diff --git a/ui/src/pages/Dotfiles.tsx b/ui/src/pages/Dotfiles.tsx index 6b0870b3..29b6b54a 100644 --- a/ui/src/pages/Dotfiles.tsx +++ b/ui/src/pages/Dotfiles.tsx @@ -1,6 +1,35 @@ +import { useState } from "react"; import Aliases from "@/components/dotfiles/Aliases"; +import Vars from "@/components/dotfiles/Vars"; -function Header() { +enum Section { + Aliases, + Vars, + Scripts, +} + +function renderDotfiles(current: Section) { + switch (current) { + case Section.Aliases: + return ; + case Section.Vars: + return ; + case Section.Scripts: + return
; + } +} + +interface HeaderProps { + current: Section; + setCurrent: (section: Section) => void; +} + +interface TabsProps { + current: Section; + setCurrent: (section: Section) => void; +} + +function Header({ current, setCurrent }: HeaderProps) { return (
@@ -8,17 +37,72 @@ function Header() { Dotfiles
+ + +
+ ); +} + +function classNames(...classes) { + return classes.filter(Boolean).join(" "); +} + +function Tabs({ current, setCurrent }: TabsProps) { + let tabs = [ + { + name: "Aliases", + isCurrent: () => current === Section.Aliases, + section: Section.Aliases, + }, + { + name: "Vars", + isCurrent: () => current === Section.Vars, + section: Section.Vars, + }, + { + name: "Scripts", + isCurrent: () => current === Section.Scripts, + section: Section.Scripts, + }, + ]; + + return ( +
+
+ +
); } export default function Dotfiles() { + let [current, setCurrent] = useState(Section.Aliases); + console.log(current); + return (
-
+
Manage your shell aliases, variables and paths - + {renderDotfiles(current)}
); diff --git a/ui/src/state/models.ts b/ui/src/state/models.ts index f11ce651..5afcb804 100644 --- a/ui/src/state/models.ts +++ b/ui/src/state/models.ts @@ -32,3 +32,9 @@ export interface Alias { name: string; value: string; } + +export interface Var { + name: string; + value: string; + export: bool; +} diff --git a/ui/src/state/store.ts b/ui/src/state/store.ts index 08410ba8..7e237d70 100644 --- a/ui/src/state/store.ts +++ b/ui/src/state/store.ts @@ -19,10 +19,12 @@ interface AtuinState { user: User; homeInfo: HomeInfo; aliases: Alias[]; + vars: Var[]; shellHistory: ShellHistory[]; refreshHomeInfo: () => void; refreshAliases: () => void; + refreshVars: () => void; refreshShellHistory: (query?: string) => void; } @@ -30,6 +32,7 @@ export const useStore = create()((set) => ({ user: DefaultUser, homeInfo: DefaultHomeInfo, aliases: [], + vars: [], shellHistory: [], refreshAliases: () => { @@ -38,6 +41,12 @@ export const useStore = create()((set) => ({ }); }, + refreshVars: () => { + invoke("vars").then((vars: any) => { + set({ vars: vars }); + }); + }, + refreshShellHistory: (query?: string) => { if (query) { invoke("search", { query: query }) -- cgit v1.3.1