-
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 15, 2025
1 parent
e3a80df
commit 7f75778
Showing
11 changed files
with
283 additions
and
26 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
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
116 changes: 116 additions & 0 deletions
116
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,116 @@ | ||
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 { useSettingsStore } from '@/state/settingsStore'; | ||
|
||
export const VariableTab = () => { | ||
const { addNewVariable, deleteVariable, update, checkDuplicate } = useSettingsStore(); | ||
const allVariables = useSettingsStore((state) => state.variables); | ||
const allDoubleKeys = useSettingsStore((state) => state.allDoubleKeys); | ||
|
||
return ( | ||
<div className="p-4 relative"> | ||
<div className="absolute top-4 right-4 left-4 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-16 left-4 bottom-4 right-4"> | ||
<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 ${allDoubleKeys.includes(variable.key) ? 'text-danger' : ''}`} | ||
placeholder="Enter variable key" | ||
onChange={(e) => { | ||
update(index, e.target.value, 'key'); | ||
checkDuplicate(e.target.value); | ||
}} | ||
/> | ||
</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} | ||
className="w-full bg-transparent outline-none" | ||
placeholder="Enter variable description" | ||
onChange={(e) => update(index, e.target.value, '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,85 @@ | ||
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; | ||
allDoubleKeys: string[]; | ||
lastInput: string | null; | ||
} | ||
|
||
interface VariableStateAction { | ||
openModal: () => void; | ||
addNewVariable: () => void; | ||
deleteVariable: (index: number) => void; | ||
update: (index: number, changeValue: string | boolean, key: keyof VariableObject) => void; | ||
save: () => void; | ||
cancel: () => void; | ||
checkDuplicate: (key: string) => void; | ||
} | ||
|
||
export const useSettingsStore = create<VariableState & VariableStateAction>()( | ||
immer((set, get) => ({ | ||
variables: [], | ||
collectionId: '', | ||
isOpen: false, | ||
allDoubleKeys: [], | ||
lastInput: null, | ||
openModal: () => { | ||
RendererEventService.instance.loadCollection().then((collection) => { | ||
set({ | ||
variables: Object.values(collection.variables), | ||
collectionId: collection.id, | ||
isOpen: true, | ||
}); | ||
}); | ||
}, | ||
addNewVariable: () => { | ||
set((state) => { | ||
state.variables.push({ key: '', value: '', description: '', isActive: false }); | ||
}); | ||
}, | ||
deleteVariable: (index: number) => { | ||
set((state) => { | ||
state.variables.splice(index, 1); | ||
}); | ||
}, | ||
update: (index: number, changeValue: string | boolean, key: keyof VariableObject) => { | ||
set((state) => { | ||
state.variables[index] = { ...state.variables[index], [key]: changeValue }; | ||
}); | ||
}, | ||
save: () => { | ||
RendererEventService.instance.setAndSaveAllVariables(get().variables); | ||
set((state) => { | ||
state.variables = []; | ||
state.isOpen = false; | ||
state.lastInput = null; | ||
}); | ||
}, | ||
cancel: () => { | ||
set((state) => { | ||
state.variables = []; | ||
state.isOpen = false; | ||
state.allDoubleKeys = []; | ||
state.lastInput = null; | ||
}); | ||
}, | ||
checkDuplicate: (key: string) => { | ||
const isDuplicate = get().variables.filter((variable) => variable.key === key).length > 1; | ||
set((state) => { | ||
if (isDuplicate && !state.allDoubleKeys.includes(key)) { | ||
state.allDoubleKeys.push(key); | ||
} else { | ||
state.allDoubleKeys = state.allDoubleKeys.filter( | ||
(doubleKey) => doubleKey !== get().lastInput | ||
); | ||
} | ||
state.lastInput = key; | ||
}); | ||
}, | ||
})) | ||
); |
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