-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#118 - add Variables to SettingsModal
- Loading branch information
Michael Giek
committed
Jan 13, 2025
1 parent
1c8452e
commit 3350094
Showing
11 changed files
with
275 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 40 additions & 10 deletions
50
src/renderer/components/shared/settings/SettingsModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,57 @@ | ||
import { | ||
Dialog, | ||
Dialog, DialogClose, | ||
DialogContent, | ||
DialogDescription, | ||
DialogDescription, DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from '@/components/ui/dialog'; | ||
import { FiSettings } from 'react-icons/fi'; | ||
import { VariableTab } from '@/components/shared/settings/VariableTab/VariableTab'; | ||
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'; | ||
import { useVariableStore } from '@/state/settingsStore'; | ||
import { Button } from '@/components/ui/button'; | ||
import * as React from 'react'; | ||
|
||
export const SettingsModal = () => { | ||
const { save, cancel, openModal } = useVariableStore.getState(); | ||
const isOpen = useVariableStore((state) => state.isOpen); | ||
|
||
return ( | ||
<Dialog> | ||
<DialogTrigger> | ||
<Dialog open={isOpen} onOpenChange={cancel}> | ||
<DialogTrigger onClick={openModal}> | ||
<FiSettings className="text-xl ml-2" /> | ||
</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Lorem ipsusm?</DialogTitle> | ||
<DialogDescription> | ||
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos blanditiis tenetur | ||
<DialogContent | ||
style={{ minWidth: '60vh'}} | ||
> | ||
<DialogHeader className={'mt-auto'}> | ||
<DialogTitle>Settings</DialogTitle> | ||
<DialogDescription className={'disabled'}> | ||
|
||
</DialogDescription> | ||
</DialogHeader> | ||
<Tabs defaultValue="variables" className={'h-[calc(50vh)]'}> | ||
<TabsList> | ||
<TabsTrigger value="variables">Variables</TabsTrigger> | ||
</TabsList> | ||
<TabsContent value="variables" className="max-h-[50vh] overflow-y-auto"> | ||
<VariableTab/> | ||
</TabsContent> | ||
</Tabs> | ||
<DialogFooter style={{margin: '-20px'}}> | ||
<DialogClose asChild className={'bottom-0 mr-3'}> | ||
<div> | ||
<Button className={'mt-4 mr-2 mb-4'} onClick={save} variant="secondary"> | ||
<span className="leading-4 font-bold">Save</span> | ||
</Button> | ||
<Button className={'mt-4 mr-2 ,b-4'} onClick={cancel} variant="destructive"> | ||
<span className="leading-4 font-bold">Cancel</span> | ||
</Button> | ||
</div> | ||
</DialogClose> | ||
</DialogFooter> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
}; | ||
}; |
129 changes: 129 additions & 0 deletions
129
src/renderer/components/shared/settings/VariableTab/VariableTab.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { Divider } from '@/components/shared/Divider'; | ||
import { Button } from '@/components/ui/button'; | ||
import { AddIcon, CheckedIcon, DeleteIcon } from '@/components/icons'; | ||
import { | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from '@/components/ui/table'; | ||
import { cn } from '@/lib/utils'; | ||
import { useVariableStore } from '@/state/settingsStore'; | ||
|
||
export function VariableTab() { | ||
|
||
const { addNewVariable, deleteVariable, update } = useVariableStore(); | ||
const allVariables = useVariableStore((state) => state.variables); | ||
|
||
return ( | ||
<div className={'p-4 relative'}> | ||
<div className={'absolute top-[16px] right-[16px] left-[16px] z-10'}> | ||
<div className={'flex'}> | ||
<Button | ||
className={'hover:bg-transparent gap-1 h-fit'} | ||
size={'sm'} | ||
variant={'ghost'} | ||
onClick={addNewVariable} | ||
> | ||
<AddIcon /> | ||
Add Variable | ||
</Button> | ||
</div> | ||
|
||
<Divider className={'mt-2'} /> | ||
</div> | ||
|
||
<div className="absolute top-[68px] left-[16px] bottom-[16px] right-[16px]"> | ||
<Table className="table-auto w-full"> | ||
<TableHeader> | ||
<TableRow> | ||
<TableHead className="w-auto">Key</TableHead> | ||
<TableHead className="w-auto">Value</TableHead> | ||
<TableHead className="w-full"> Description </TableHead> | ||
<TableHead className="w-16"> {/* Action Column */} </TableHead> | ||
</TableRow> | ||
</TableHeader> | ||
|
||
<TableBody> | ||
{allVariables.map((variable, index) => ( | ||
<TableRow key={index}> | ||
<TableCell className="w-1/4 break-all"> | ||
<input | ||
type="text" | ||
value={variable.key} | ||
className="w-full bg-transparent outline-none" | ||
placeholder="Enter variable key" | ||
onChange={(e) => update(index, e.target.value, 'key')} | ||
/> | ||
</TableCell> | ||
|
||
<TableCell className="w-1/4 break-all"> | ||
<input | ||
type="text" | ||
value={variable.value} | ||
className="w-full bg-transparent outline-none" | ||
placeholder="Enter variable value" | ||
onChange={(e) => update(index, e.target.value, 'value')} | ||
/> | ||
</TableCell> | ||
|
||
<TableCell className="w-full break-all"> | ||
<input | ||
type="text" | ||
value={variable.description} | ||
onChange={(e) => update(index, e.target.value, 'description')} | ||
className="w-full bg-transparent outline-none" | ||
placeholder="Enter variable description" | ||
/> | ||
</TableCell> | ||
|
||
<TableCell className="w-16 text-right"> | ||
<div className="flex items-center justify-center gap-2"> | ||
<div className={'relative h-4 z-10 cursor-pointer'}> | ||
<input | ||
type="checkbox" | ||
checked={variable.isActive} | ||
onChange={(e) => update(index, e.target.checked, 'isActive')} | ||
className={cn( | ||
'form-checkbox h-4 w-4 appearance-none border rounded-[2px] ', | ||
variable.isActive | ||
? 'border-[rgba(107,194,224,1)] bg-[rgba(25,54,65,1)]' | ||
: 'border-[rgba(238,238,238,1)] bg-transparent' | ||
)} | ||
/> | ||
|
||
{variable.isActive && ( | ||
<div | ||
className={ | ||
'absolute left-0 top-0 h-4 w-4 flex items-center justify-center pointer-events-none rotate-6' | ||
} | ||
> | ||
<CheckedIcon | ||
size={16} | ||
viewBox={'0 0 16 16'} | ||
color={'rgba(107,194,224,1)'} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
|
||
<Button | ||
variant="ghost" | ||
size="icon" | ||
className="hover:bg-transparent hover:text-[rgba(107,194,224,1)] active:text-[#12B1E7] h-6 w-6" | ||
onClick={() => deleteVariable(index)} | ||
> | ||
<DeleteIcon /> | ||
</Button> | ||
</div> | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { create } from 'zustand'; | ||
import { immer } from 'zustand/middleware/immer'; | ||
import { VariableObject } from 'shim/variables'; | ||
import { RendererEventService } from '@/services/event/renderer-event-service'; | ||
|
||
interface VariableState { | ||
variables: VariableObject[]; | ||
collectionId: string; | ||
isOpen: boolean; | ||
} | ||
|
||
interface VariableStateAction { | ||
openModal: () => void; | ||
addNewVariable: () => void; | ||
deleteVariable: (index: number) => void; | ||
update: (index: number, changeValue: string | boolean, key: keyof VariableObject) => void; | ||
save: () => void; | ||
cancel: () => void; | ||
} | ||
|
||
export const useVariableStore = create<VariableState & VariableStateAction>()( | ||
immer((set, get) => ({ | ||
variables: [], | ||
collectionId: '', | ||
isOpen: false, | ||
openModal: () => { | ||
RendererEventService.instance.loadCollection().then((collection) => { | ||
const variables = Object.values(collection.variables); | ||
set({ | ||
variables: variables, | ||
collectionId: collection.id, | ||
isOpen: true, | ||
}); | ||
}); | ||
}, | ||
addNewVariable: () => { | ||
set((state) => { | ||
console.log('addNewVariable', state.variables); | ||
state.variables.push({ | ||
key: '', | ||
value: '', | ||
description: '', | ||
isActive: false, | ||
}); | ||
}); | ||
}, | ||
deleteVariable: (index: number) => { | ||
console.log('deleteVariable', index); | ||
set((state) => { | ||
state.variables.splice(index, 1); | ||
}); | ||
}, | ||
update: async (index: number, changeValue: string | boolean, key: keyof VariableObject) => | ||
set((state) => { | ||
console.log('update', index, changeValue, key); | ||
state.variables[index] = { | ||
...state.variables[index], | ||
[key]: changeValue, | ||
}; | ||
}), | ||
save: async () => { | ||
RendererEventService.instance.setAndSaveAllVariables(get().variables); | ||
set((state) => { | ||
state.variables = []; | ||
state.isOpen = false; | ||
}); | ||
}, | ||
cancel: async () => | ||
set((state) => { | ||
state.variables = []; | ||
state.isOpen = false; | ||
}), | ||
})) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters