Skip to content

Commit b515d91

Browse files
Renaming is working, but sorting afterwards isn't?
1 parent 0b62c8a commit b515d91

File tree

3 files changed

+96
-46
lines changed

3 files changed

+96
-46
lines changed

src/main/main.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ const addToCollection = (
192192
collectionId: string,
193193
videoIdList: Array<string>
194194
) => {
195+
log.info(
196+
`ADDING ${videoIdList} for game ${game} to collection ${collectionId}`
197+
);
198+
195199
// If the collection isn't present, create a key and an empty array for it.
196200
if (!(collectionId in collections[game])) {
197201
collections[game][collectionId] = [];
@@ -204,6 +208,10 @@ const addToCollection = (
204208
}
205209
});
206210

211+
const collection = collections[game][collectionId];
212+
collection.sort();
213+
collections[game][collectionId] = collection;
214+
207215
const {collectionMeta} = getConfigDirectories();
208216

209217
log.info("WRITING TO " + collectionMeta);
@@ -213,6 +221,35 @@ const addToCollection = (
213221
return collections[game];
214222
};
215223

224+
const removeFromCollection = (game: string, collectionId: string, videoId: string) => {
225+
log.info(
226+
`REMOVING ${videoId} for game ${game} from collection ${collectionId}`
227+
);
228+
229+
// If the collection isn't present, return immediately.
230+
if (!(collectionId in collections[game])) {
231+
log.info("COLLECTION NOT PRESENT");
232+
return;
233+
}
234+
235+
// Filter out videoId that's being removed.
236+
collections[game][collectionId] = collections[game][collectionId].filter(
237+
(element: string) => element !== videoId
238+
);
239+
240+
const collection = collections[game][collectionId];
241+
collection.sort();
242+
collections[game][collectionId] = collection;
243+
244+
const {collectionMeta} = getConfigDirectories();
245+
246+
// Store updated file
247+
fs.writeFileSync(
248+
collectionMeta,
249+
JSON.stringify(collections, null, 5)
250+
);
251+
}
252+
216253
const importZip = async (filePath: string, game: string) => {
217254
// Extract video names from zip
218255
const zip = new StreamZip.async({ file: filePath });
@@ -848,13 +885,15 @@ ipcMain.handle('getPreviewImage', (event, { collectionId, game }) => {
848885

849886
ipcMain.handle(
850887
'renameVideo',
851-
(event, { id, newTitle, game }) => {
888+
(event, { id, newTitle, game, collectionId }) => {
852889
log.info(
853-
`RENAMING ${id} to new title ${newTitle} for game ${game}`
890+
`RENAMING ${id} to new title ${newTitle} in collection ${collectionId} for game ${game}`
854891
);
855892

893+
const newId = newTitle.replaceAll(' ', '_');
894+
856895
const {clip: videoFilePath, subtitle: subFilePath, thumbnail: thumbNailPath} = getClipPaths(id, game);
857-
const {clip: newVideoFilePath, subtitle: newSubFilePath, thumbnail: newThumbNailPath} = getClipPaths(newTitle.replaceAll(' ', '_'), game);
896+
const {clip: newVideoFilePath, subtitle: newSubFilePath, thumbnail: newThumbNailPath} = getClipPaths(newId, game);
858897

859898
log.info(`RENAMING ${videoFilePath} to ${newVideoFilePath}`);
860899
fs.renameSync(videoFilePath, newVideoFilePath);
@@ -864,6 +903,9 @@ ipcMain.handle(
864903

865904
log.info(`RENAMING ${thumbNailPath} to ${newThumbNailPath}`);
866905
fs.renameSync(thumbNailPath, newThumbNailPath);
906+
907+
removeFromCollection(game, collectionId, id);
908+
addToCollection(game, collectionId, [newId]);
867909
}
868910
);
869911

@@ -988,6 +1030,10 @@ ipcMain.handle('addToCollection', (event, { collectionId, videoId, game }) => {
9881030
collections[game][collectionId].push(videoId);
9891031
}
9901032

1033+
const collection = collections[game][collectionId];
1034+
collection.sort();
1035+
collections[game][collectionId] = collection;
1036+
9911037
const {collectionMeta} = getConfigDirectories();
9921038

9931039
// Store updated file
@@ -1013,6 +1059,10 @@ ipcMain.handle(
10131059
(element: string) => element !== videoId
10141060
);
10151061

1062+
const collection = collections[game][collectionId];
1063+
collection.sort();
1064+
collections[game][collectionId] = collection;
1065+
10161066
const {collectionMeta} = getConfigDirectories();
10171067

10181068
// Store updated file

src/renderer/components/ClipTable.jsx

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@ export default ({
2828
const [, setInterstitialState] = useAtom(interstitialAtom);
2929

3030
const renameClip = async () => {
31-
console.log('RENAMED');
31+
const collectionId = Object.keys(collections).find((key) =>
32+
collections[key].includes(renaming)
33+
);
34+
3235
await handleInterstitial(
33-
window.api.send('renameVideo', { id: renaming, game, newTitle }),
36+
window.api.send('renameVideo', {
37+
id: renaming,
38+
game,
39+
newTitle,
40+
collectionId,
41+
}),
3442
(open) => {
3543
setInterstitialState(open);
3644
}
@@ -170,34 +178,37 @@ export default ({
170178
/>
171179
</div>
172180
)}
173-
{includeRename ? (
174-
<div>
175-
{renaming !== video._id ? (
176-
<button
177-
type="button"
178-
onClick={() => {
179-
setNewTitle(
180-
video._id.replace(/_/g, ' ')
181-
);
182-
setRenaming(video._id);
183-
}}
184-
>
185-
Rename
186-
</button>
187-
) : (
188-
<button
189-
onClick={() => {
190-
renameClip();
191-
setRenaming(null);
192-
}}
193-
>
194-
Done
195-
</button>
196-
)}
197-
</div>
198-
) : null}
199-
{includeDelete ? (
200-
<div>
181+
<div>
182+
{includeRename ? (
183+
<>
184+
{renaming !== video._id ? (
185+
<button
186+
type="button"
187+
onClick={() => {
188+
setNewTitle(
189+
video._id.replace(
190+
/_/g,
191+
' '
192+
)
193+
);
194+
setRenaming(video._id);
195+
}}
196+
>
197+
Rename
198+
</button>
199+
) : (
200+
<button
201+
onClick={() => {
202+
renameClip();
203+
setRenaming(null);
204+
}}
205+
>
206+
Done
207+
</button>
208+
)}
209+
</>
210+
) : null}
211+
{includeDelete ? (
201212
<button
202213
type="button"
203214
onClick={() => {
@@ -206,8 +217,8 @@ export default ({
206217
>
207218
Delete
208219
</button>
209-
</div>
210-
) : null}
220+
) : null}
221+
</div>
211222
</div>
212223
);
213224
})}

src/renderer/routes/VideoList.jsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@ let VideoList = () => {
6262
return <Interstitial isOpen={true} children={<p>Loading Media</p>} />;
6363
}
6464

65-
const deleteFile = async (id, game, isActive) => {
66-
await handleInterstitial(
67-
window.api.send('deleteVideo', { id, game, isActive }),
68-
(open) => {
69-
setInterstitialState(open);
70-
}
71-
);
72-
toast('Deleted video', { type: 'info' });
73-
loadVideos();
74-
};
75-
7665
let sortedVideos = Object.keys(collections).reduce((prev, curr) => {
7766
let collection = collections[curr];
7867
collection.forEach((video) => {

0 commit comments

Comments
 (0)