Skip to content

Commit 4d17848

Browse files
Fix list rename
1 parent 6bb0ce0 commit 4d17848

9 files changed

+40
-40
lines changed

src/Menu.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function clipItemContextMenu(hints: string[]) {
6363
}
6464

6565
function listsMenu() {
66-
const activeList = getActiveList();
66+
const { activeList, activeListIsDefault } = getActiveList();
6767
const lists = defaultLists.concat(storage.getLists());
6868

6969
const getOptions = (list: List): MenuItemConstructorOptions => ({
@@ -87,7 +87,7 @@ function listsMenu() {
8787
label: "Rename list",
8888
type: "normal",
8989
click: () => runCommand({ id: "renameList" }),
90-
enabled: !activeList.isDefault,
90+
enabled: !activeListIsDefault,
9191
},
9292
{
9393
type: "separator",
@@ -105,11 +105,12 @@ function listsMenu() {
105105
label: "Delete list and all items",
106106
type: "normal",
107107
click: () => runCommand({ id: "removeList" }),
108-
enabled: !activeList.isDefault,
108+
enabled: !activeListIsDefault,
109109
},
110110
],
111111
},
112112
]);
113+
113114
menu.popup();
114115
}
115116

src/commands/removeAllItems.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getActiveList } from "../util/getList";
44
import { updateRenderer } from "../util/updateRenderer";
55

66
export function removeAllItems(render = true) {
7-
const activeList = getActiveList();
7+
const { activeList } = getActiveList();
88
const allItems = storage.getClipboardItems();
99

1010
const itemsToRemove = (() => {

src/commands/removeList.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { updateRenderer } from "../util/updateRenderer";
55
import { removeAllItems } from "./removeAllItems";
66

77
export function removeList() {
8-
const activeList = getActiveList();
8+
const { activeList, activeListIsDefault } = getActiveList();
99

10-
if (activeList.isDefault) {
10+
if (activeListIsDefault) {
1111
throw Error(`Can't remove default list '${activeList.name}'`);
1212
}
1313

src/commands/renameList.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ export function renameList(command: RenameListCommand) {
1111
}
1212

1313
const name = command.name.trim();
14-
const activeList = getActiveList();
15-
16-
if (!name) {
17-
throw Error("Can't rename list: Name can't be empty");
18-
}
14+
const { activeList, activeListIsDefault } = getActiveList();
1915

2016
if (activeList.name === name) {
2117
return;
2218
}
2319

24-
if (activeList.isDefault) {
20+
if (!name) {
21+
throw Error("Can't rename list: Name can't be empty");
22+
}
23+
24+
if (activeListIsDefault) {
2525
throw Error(`Can't rename default list '${activeList.name}'`);
2626
}
2727

@@ -32,7 +32,8 @@ export function renameList(command: RenameListCommand) {
3232
}
3333

3434
activeList.name = name;
35-
storage.setLists(storage.getLists());
35+
36+
storage.saveStateFile();
3637

3738
updateRenderer();
3839
}

src/commands/switchList.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getActiveList, getListByNameIgnoreCase } from "../util/getList";
44
import { updateRenderer } from "../util/updateRenderer";
55

66
export function switchList(command: SwitchListCommand) {
7-
const activeList = getActiveList();
7+
const { activeList } = getActiveList();
88
const namedList = getListByNameIgnoreCase(command.name);
99

1010
if (namedList.id === activeList.id) {

src/storage.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ export const storage = {
129129
}
130130
}
131131
},
132+
133+
saveStateFile,
132134
};
133135

134136
async function readStateFile() {
@@ -140,7 +142,7 @@ async function readStateFile() {
140142
return { ...stateDefault };
141143
}
142144

143-
function saveStateFile() {
145+
export function saveStateFile() {
144146
const { stateFile } = storagePaths.get();
145147
writeJsonFile(stateFile, _state).catch((error) => {
146148
showErrorNotification("Failed to save storage", error);

src/types/types.ts

-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ export interface List {
5252
name: string;
5353
}
5454

55-
export interface EnrichedList extends List {
56-
isDefault: boolean;
57-
}
58-
5955
export interface StorageState {
6056
windowBounds?: Electron.Rectangle;
6157
config: Config;

src/util/getList.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
import { storage } from "../storage";
2-
import { type EnrichedList, type List, defaultLists } from "../types/types";
2+
import { type List, defaultLists } from "../types/types";
33

4-
export function getList(id: string): EnrichedList {
5-
const defaultList = defaultLists.find((l) => l.id === id);
6-
7-
if (defaultList != null) {
8-
return { ...defaultList, isDefault: true };
9-
}
10-
11-
const userList = storage.getLists().find((l) => l.id === id);
12-
13-
if (userList != null) {
14-
return { ...userList, isDefault: false };
15-
}
16-
17-
throw Error(`Can't find list '${id}'`);
18-
}
19-
20-
export function getActiveList(): EnrichedList {
21-
const { activeList } = storage.getConfig();
22-
return getList(activeList);
4+
export function getActiveList(): { activeList: List; activeListIsDefault: boolean } {
5+
const [activeList, activeListIsDefault] = getListInternal(storage.getConfig().activeList);
6+
return { activeList, activeListIsDefault };
237
}
248

259
export function tryGetListByNameIgnoreCase(name: string): List | undefined {
@@ -39,3 +23,19 @@ export function getListByNameIgnoreCase(name: string): List {
3923

4024
return list;
4125
}
26+
27+
function getListInternal(id: string): [List, boolean] {
28+
const defaultList = defaultLists.find((l) => l.id === id);
29+
30+
if (defaultList != null) {
31+
return [defaultList, true];
32+
}
33+
34+
const userList = storage.getLists().find((l) => l.id === id);
35+
36+
if (userList != null) {
37+
return [userList, false];
38+
}
39+
40+
throw Error(`Can't find list '${id}'`);
41+
}

src/util/getRendererData.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function getRendererData(): RendererData {
99
const items = getListItems(isVisible);
1010
return {
1111
totalCount: items.length,
12-
activeListName: getActiveList().name,
12+
activeListName: getActiveList().activeList.name,
1313
config: storage.getConfig(),
1414
search: storage.getSearch(),
1515
showSettings: storage.getShowSettings(),

0 commit comments

Comments
 (0)