metadata fixes, redux start. Lifecycle fixes for regions, etc

This commit is contained in:
michalcourson
2026-02-17 18:10:38 -05:00
parent f9fdfb629b
commit d6f4d4166b
22 changed files with 1481 additions and 248 deletions

View File

@ -0,0 +1,62 @@
import { createSlice, configureStore } from '@reduxjs/toolkit';
import { ClipMetadata, MetadataState } from './types';
const initialState: MetadataState = {
collections: {},
};
const metadataSlice = createSlice({
name: 'metadata',
initialState,
reducers: {
setAllData(state, action) {
state.collections = action.payload.collections;
},
setCollections(state, action) {
const { collection, newMetadata } = action.payload;
state.collections[collection] = newMetadata;
},
editClip(state, action) {
const { collection, clip } = action.payload;
const clips = state.collections[collection];
// console.log('Editing clip in collection:', collection, clip);
if (clips) {
const index = clips.findIndex((c) => c.filename === clip.filename);
if (index !== -1) {
clips[index] = clip;
}
}
},
addNewClips(state, action) {
const { collections } = action.payload;
Object.keys(collections).forEach((collection) => {
if (!state.collections[collection]) {
state.collections[collection] = [];
}
const existingFilenames = new Set(
state.collections[collection].map((clip) => clip.filename),
);
const newClips = collections[collection].filter(
(clip: ClipMetadata) => !existingFilenames.has(clip.filename),
);
state.collections[collection].push(...newClips);
});
},
},
});
export const store = configureStore({
reducer: metadataSlice.reducer,
});
// Can still subscribe to the store
// store.subscribe(() => console.log(store.getState()));
// Get the type of our store variable
export type AppStore = typeof store;
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<AppStore['getState']>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = AppStore['dispatch'];
export const { setCollections, addNewClips } = metadataSlice.actions;
export default metadataSlice.reducer;