Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: app crash when reach last index of undo stack #111

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/keyboard/Keyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export default function Keyboard() {
);

let selectedBinding = useMemo(() => {
if (keymap == null || selectedKeyPosition == null) {
if (keymap == null || selectedKeyPosition == null || !keymap.layers[selectedLayerIndex]) {
return null;
}

Expand Down Expand Up @@ -490,6 +490,19 @@ export default function Keyboard() {
[conn, undoRedo, keymap]
);


useEffect(() => {
if (!keymap?.layers) return;

const layers = keymap.layers.length - 1 ?? 0;

if (selectedLayerIndex > layers) {
setSelectedLayerIndex(layers);
}
}, [keymap, selectedLayerIndex]);



return (
<div className="grid grid-cols-[auto_1fr] grid-rows-[1fr_minmax(10em,auto)] bg-base-300 max-w-full min-w-0 min-h-0">
<div className="p-2 flex flex-col gap-2 bg-base-200 row-span-2">
Expand Down
22 changes: 11 additions & 11 deletions src/keyboard/LayerPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface LayerPickerProps {
onLayerNameChanged?: (
id: number,
oldName: string,
newName: string
newName: string,
) => void | Promise<void>;
}

Expand All @@ -53,7 +53,7 @@ const EditLabelModal = ({
handleSaveNewLabel: (
id: number,
oldName: string,
newName: string | null
newName: string | null,
) => void;
}) => {
const ref = useModalRef(open);
Expand Down Expand Up @@ -115,7 +115,7 @@ export const LayerPicker = ({
...props
}: LayerPickerProps) => {
const [editLabelData, setEditLabelData] = useState<EditLabelData | null>(
null
null,
);

const layer_items = useMemo(() => {
Expand All @@ -135,10 +135,10 @@ export const LayerPicker = ({

onLayerClicked?.(layer_items.findIndex((l) => s.has(l.id)));
},
[onLayerClicked, layer_items]
[onLayerClicked, layer_items],
);

let { dragAndDropHooks } = useDragAndDrop({
const { dragAndDropHooks } = useDragAndDrop({
renderDropIndicator(target) {
return (
<DropIndicator
Expand All @@ -150,8 +150,8 @@ export const LayerPicker = ({
getItems: (keys) =>
[...keys].map((key) => ({ "text/plain": key.toLocaleString() })),
onReorder(e) {
let startIndex = layer_items.findIndex((l) => e.keys.has(l.id));
let endIndex = layer_items.findIndex((l) => l.id === e.target.key);
const startIndex = layer_items.findIndex((l) => e.keys.has(l.id));
const endIndex = layer_items.findIndex((l) => l.id === e.target.key);
onLayerMoved?.(startIndex, endIndex);
},
});
Expand All @@ -162,17 +162,17 @@ export const LayerPicker = ({
onLayerNameChanged?.(id, oldName, newName);
}
},
[onLayerNameChanged]
[onLayerNameChanged],
);

return (
<div className="flex flex-col min-w-40">
<div className="grid grid-cols-[1fr_auto_auto] items-center">
<div className="grid grid-cols-[1fr_auto_auto] items-center gap-1">
<Label className="after:content-[':'] text-sm">Layers</Label>
{onRemoveClicked && (
<button
type="button"
className="hover:text-primary-content hover:bg-primary rounded-sm"
className="hover:text-primary-content hover:bg-primary rounded-sm disabled:opacity-50 disabled:pointer-events-none"
disabled={!canRemove}
onClick={onRemoveClicked}
>
Expand All @@ -183,7 +183,7 @@ export const LayerPicker = ({
<button
type="button"
disabled={!canAdd}
className="hover:text-primary-content ml-1 hover:bg-primary rounded-sm disabled:text-gray-500 disabled:hover:bg-base-300 disabled:cursor-not-allowed"
pongstr marked this conversation as resolved.
Show resolved Hide resolved
className="hover:text-primary-content hover:bg-primary rounded-sm disabled:opacity-50 disabled:pointer-events-none"
onClick={onAddClicked}
>
<Plus className="size-4" />
Expand Down