Skip to content

Commit

Permalink
Renaming is working, but sorting afterwards isn't?
Browse files Browse the repository at this point in the history
  • Loading branch information
deusprogrammer committed Nov 5, 2023
1 parent 0b62c8a commit b515d91
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 46 deletions.
56 changes: 53 additions & 3 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ const addToCollection = (
collectionId: string,
videoIdList: Array<string>
) => {
log.info(
`ADDING ${videoIdList} for game ${game} to collection ${collectionId}`
);

// If the collection isn't present, create a key and an empty array for it.
if (!(collectionId in collections[game])) {
collections[game][collectionId] = [];
Expand All @@ -204,6 +208,10 @@ const addToCollection = (
}
});

const collection = collections[game][collectionId];
collection.sort();
collections[game][collectionId] = collection;

const {collectionMeta} = getConfigDirectories();

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

const removeFromCollection = (game: string, collectionId: string, videoId: string) => {
log.info(
`REMOVING ${videoId} for game ${game} from collection ${collectionId}`
);

// If the collection isn't present, return immediately.
if (!(collectionId in collections[game])) {
log.info("COLLECTION NOT PRESENT");
return;
}

// Filter out videoId that's being removed.
collections[game][collectionId] = collections[game][collectionId].filter(
(element: string) => element !== videoId
);

const collection = collections[game][collectionId];
collection.sort();
collections[game][collectionId] = collection;

const {collectionMeta} = getConfigDirectories();

// Store updated file
fs.writeFileSync(
collectionMeta,
JSON.stringify(collections, null, 5)
);
}

const importZip = async (filePath: string, game: string) => {
// Extract video names from zip
const zip = new StreamZip.async({ file: filePath });
Expand Down Expand Up @@ -848,13 +885,15 @@ ipcMain.handle('getPreviewImage', (event, { collectionId, game }) => {

ipcMain.handle(
'renameVideo',
(event, { id, newTitle, game }) => {
(event, { id, newTitle, game, collectionId }) => {
log.info(
`RENAMING ${id} to new title ${newTitle} for game ${game}`
`RENAMING ${id} to new title ${newTitle} in collection ${collectionId} for game ${game}`
);

const newId = newTitle.replaceAll(' ', '_');

const {clip: videoFilePath, subtitle: subFilePath, thumbnail: thumbNailPath} = getClipPaths(id, game);
const {clip: newVideoFilePath, subtitle: newSubFilePath, thumbnail: newThumbNailPath} = getClipPaths(newTitle.replaceAll(' ', '_'), game);
const {clip: newVideoFilePath, subtitle: newSubFilePath, thumbnail: newThumbNailPath} = getClipPaths(newId, game);

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

log.info(`RENAMING ${thumbNailPath} to ${newThumbNailPath}`);
fs.renameSync(thumbNailPath, newThumbNailPath);

removeFromCollection(game, collectionId, id);
addToCollection(game, collectionId, [newId]);
}
);

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

const collection = collections[game][collectionId];
collection.sort();
collections[game][collectionId] = collection;

const {collectionMeta} = getConfigDirectories();

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

const collection = collections[game][collectionId];
collection.sort();
collections[game][collectionId] = collection;

const {collectionMeta} = getConfigDirectories();

// Store updated file
Expand Down
75 changes: 43 additions & 32 deletions src/renderer/components/ClipTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ export default ({
const [, setInterstitialState] = useAtom(interstitialAtom);

const renameClip = async () => {
console.log('RENAMED');
const collectionId = Object.keys(collections).find((key) =>
collections[key].includes(renaming)
);

await handleInterstitial(
window.api.send('renameVideo', { id: renaming, game, newTitle }),
window.api.send('renameVideo', {
id: renaming,
game,
newTitle,
collectionId,
}),
(open) => {
setInterstitialState(open);
}
Expand Down Expand Up @@ -170,34 +178,37 @@ export default ({
/>
</div>
)}
{includeRename ? (
<div>
{renaming !== video._id ? (
<button
type="button"
onClick={() => {
setNewTitle(
video._id.replace(/_/g, ' ')
);
setRenaming(video._id);
}}
>
Rename
</button>
) : (
<button
onClick={() => {
renameClip();
setRenaming(null);
}}
>
Done
</button>
)}
</div>
) : null}
{includeDelete ? (
<div>
<div>
{includeRename ? (
<>
{renaming !== video._id ? (
<button
type="button"
onClick={() => {
setNewTitle(
video._id.replace(
/_/g,
' '
)
);
setRenaming(video._id);
}}
>
Rename
</button>
) : (
<button
onClick={() => {
renameClip();
setRenaming(null);
}}
>
Done
</button>
)}
</>
) : null}
{includeDelete ? (
<button
type="button"
onClick={() => {
Expand All @@ -206,8 +217,8 @@ export default ({
>
Delete
</button>
</div>
) : null}
) : null}
</div>
</div>
);
})}
Expand Down
11 changes: 0 additions & 11 deletions src/renderer/routes/VideoList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ let VideoList = () => {
return <Interstitial isOpen={true} children={<p>Loading Media</p>} />;
}

const deleteFile = async (id, game, isActive) => {
await handleInterstitial(
window.api.send('deleteVideo', { id, game, isActive }),
(open) => {
setInterstitialState(open);
}
);
toast('Deleted video', { type: 'info' });
loadVideos();
};

let sortedVideos = Object.keys(collections).reduce((prev, curr) => {
let collection = collections[curr];
collection.forEach((video) => {
Expand Down

0 comments on commit b515d91

Please sign in to comment.