Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

1.4 KB
Raw Blame
import type { PayloadAction, Selector } from '@reduxjs/toolkit';
import { createSelector, createSlice } from '@reduxjs/toolkit';
import { createMemoizedSelector } from 'app/store/createMemoizedSelector';
import type { RootState } from 'app/store/store';
import type { SliceConfig } from 'app/store/types';
import { type GetQueueItemIdsArgs, zSQLiteDirection } from 'services/api/types';
import z from 'zod';

const zQueueState = z.object({
  sortOrder: zSQLiteDirection,
});
type QueueState = z.infer<typeof zQueueState>;

const getInitialState = (): QueueState => ({
  sortOrder: 'DESC',
});

const slice = createSlice({
  name: 'queue',
  initialState: getInitialState(),
  reducers: {
    sortOrderChanged: (state, action: PayloadAction<'ASC' | 'DESC'>) => {
      state.sortOrder = action.payload;
    },
  },
});

export const { sortOrderChanged } = slice.actions;

export const queueSliceConfig: SliceConfig<typeof slice> = {
  slice,
  schema: zQueueState,
  getInitialState,
};

const selectQueueSlice = (state: RootState) => state.queue;
const createQueueSelector = <T>(selector: Selector<QueueState, T>) => createSelector(selectQueueSlice, selector);
export const selectQueueSortOrder = createQueueSelector((queue) => queue.sortOrder);
export const selectGetQueueItemIdsArgs = createMemoizedSelector(
  [selectQueueSortOrder],
  (order_dir): GetQueueItemIdsArgs => ({
    order_dir,
  })
);