Feat/improve new views (#2298)
* POC new recoil injected scoped states * Finished useViewScopedState refactor * Finished refactor * Renamed mappers * Fixed update view fields bug * Post merge * Complete refactor * Fix tests --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -2,37 +2,28 @@ import { useApolloClient } from '@apollo/client';
|
||||
import { useRecoilCallback } from 'recoil';
|
||||
|
||||
import { useFindOneObjectMetadataItem } from '@/metadata/hooks/useFindOneObjectMetadataItem';
|
||||
import { currentViewIdScopedState } from '@/views/states/currentViewIdScopedState';
|
||||
import { savedViewFieldByKeyScopedFamilySelector } from '@/views/states/selectors/savedViewFieldByKeyScopedFamilySelector';
|
||||
import { viewObjectIdScopeState } from '@/views/states/viewObjectIdScopeState';
|
||||
import { ViewField } from '@/views/types/ViewField';
|
||||
import { getViewScopedStateValuesFromSnapshot } from '@/views/utils/getViewScopedStateValuesFromSnapshot';
|
||||
|
||||
export const useViewFields = (viewScopeId: string) => {
|
||||
const { updateOneMutation, createOneMutation, findManyQuery } =
|
||||
useFindOneObjectMetadataItem({
|
||||
objectNameSingular: 'viewFieldV2',
|
||||
});
|
||||
|
||||
const apolloClient = useApolloClient();
|
||||
|
||||
const persistViewFields = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
async (viewFieldsToPersist: ViewField[], viewId?: string) => {
|
||||
const currentViewId = snapshot
|
||||
.getLoadable(currentViewIdScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
const { viewObjectId, currentViewId, savedViewFieldsByKey } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId,
|
||||
viewId,
|
||||
});
|
||||
|
||||
const viewObjectId = snapshot
|
||||
.getLoadable(viewObjectIdScopeState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
|
||||
const savedViewFieldsByKey = snapshot
|
||||
.getLoadable(
|
||||
savedViewFieldByKeyScopedFamilySelector({
|
||||
viewScopeId: viewScopeId,
|
||||
viewId: viewId ?? currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
const viewIdToPersist = viewId ?? currentViewId;
|
||||
|
||||
if (!currentViewId || !savedViewFieldsByKey || !viewObjectId) {
|
||||
return;
|
||||
@ -50,7 +41,7 @@ export const useViewFields = (viewScopeId: string) => {
|
||||
variables: {
|
||||
input: {
|
||||
fieldId: viewField.fieldId,
|
||||
viewId: viewId,
|
||||
viewId: viewIdToPersist,
|
||||
isVisible: viewField.isVisible,
|
||||
size: viewField.size,
|
||||
position: viewField.position,
|
||||
@ -87,7 +78,6 @@ export const useViewFields = (viewScopeId: string) => {
|
||||
const viewFieldsToCreate = viewFieldsToPersist.filter(
|
||||
(viewField) => !savedViewFieldsByKey[viewField.fieldId],
|
||||
);
|
||||
await _createViewFields(viewFieldsToCreate);
|
||||
|
||||
const viewFieldsToUpdate = viewFieldsToPersist.filter(
|
||||
(viewFieldToPersit) =>
|
||||
@ -100,8 +90,17 @@ export const useViewFields = (viewScopeId: string) => {
|
||||
viewFieldToPersit.isVisible),
|
||||
);
|
||||
|
||||
await _createViewFields(viewFieldsToCreate);
|
||||
|
||||
await _updateViewFields(viewFieldsToUpdate);
|
||||
},
|
||||
[
|
||||
apolloClient,
|
||||
createOneMutation,
|
||||
findManyQuery,
|
||||
updateOneMutation,
|
||||
viewScopeId,
|
||||
],
|
||||
);
|
||||
|
||||
return { persistViewFields };
|
||||
|
||||
@ -4,14 +4,11 @@ import { useRecoilCallback } from 'recoil';
|
||||
|
||||
import { useFindOneObjectMetadataItem } from '@/metadata/hooks/useFindOneObjectMetadataItem';
|
||||
import { Filter } from '@/ui/object/object-filter-dropdown/types/Filter';
|
||||
import { currentViewFiltersScopedFamilyState } from '@/views/states/currentViewFiltersScopedFamilyState';
|
||||
import { currentViewIdScopedState } from '@/views/states/currentViewIdScopedState';
|
||||
import { onViewFiltersChangeScopedState } from '@/views/states/onViewFiltersChangeScopedState';
|
||||
import { savedViewFiltersScopedFamilyState } from '@/views/states/savedViewFiltersScopedFamilyState';
|
||||
import { savedViewFiltersByKeyScopedFamilySelector } from '@/views/states/selectors/savedViewFiltersByKeyScopedFamilySelector';
|
||||
import { ViewFilter } from '@/views/types/ViewFilter';
|
||||
import { getViewScopedStateValuesFromSnapshot } from '@/views/utils/getViewScopedStateValuesFromSnapshot';
|
||||
|
||||
import { useViewSetStates } from '../useViewSetStates';
|
||||
import { useViewScopedStates } from './useViewScopedStates';
|
||||
|
||||
export const useViewFilters = (viewScopeId: string) => {
|
||||
const {
|
||||
@ -23,17 +20,27 @@ export const useViewFilters = (viewScopeId: string) => {
|
||||
objectNameSingular: 'viewFilterV2',
|
||||
});
|
||||
const apolloClient = useApolloClient();
|
||||
const { setCurrentViewFilters } = useViewSetStates(viewScopeId);
|
||||
|
||||
const { currentViewFiltersState } = useViewScopedStates();
|
||||
|
||||
const persistViewFilters = useRecoilCallback(
|
||||
({ snapshot, set }) =>
|
||||
async (viewId?: string) => {
|
||||
const currentViewId = snapshot
|
||||
.getLoadable(currentViewIdScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
const { currentViewId, currentViewFilters, savedViewFiltersByKey } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId,
|
||||
});
|
||||
|
||||
if (!currentViewId) {
|
||||
return;
|
||||
}
|
||||
if (!currentViewFilters) {
|
||||
return;
|
||||
}
|
||||
if (!savedViewFiltersByKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const createViewFilters = (viewFiltersToCreate: ViewFilter[]) => {
|
||||
if (!viewFiltersToCreate.length) return;
|
||||
@ -92,31 +99,6 @@ export const useViewFilters = (viewScopeId: string) => {
|
||||
);
|
||||
};
|
||||
|
||||
const currentViewFilters = snapshot
|
||||
.getLoadable(
|
||||
currentViewFiltersScopedFamilyState({
|
||||
scopeId: viewScopeId,
|
||||
familyKey: currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
const savedViewFiltersByKey = snapshot
|
||||
.getLoadable(
|
||||
savedViewFiltersByKeyScopedFamilySelector({
|
||||
scopeId: viewScopeId,
|
||||
viewId: viewId ?? currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
if (!currentViewFilters) {
|
||||
return;
|
||||
}
|
||||
if (!savedViewFiltersByKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const filtersToCreate = currentViewFilters.filter(
|
||||
(filter) => !savedViewFiltersByKey[filter.fieldId],
|
||||
);
|
||||
@ -158,37 +140,26 @@ export const useViewFilters = (viewScopeId: string) => {
|
||||
);
|
||||
|
||||
const upsertViewFilter = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
({ snapshot, set }) =>
|
||||
(filterToUpsert: Filter) => {
|
||||
const currentViewId = snapshot
|
||||
.getLoadable(currentViewIdScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
const { currentViewId, savedViewFiltersByKey, onViewFiltersChange } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId,
|
||||
});
|
||||
|
||||
if (!currentViewId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const savedViewFiltersByKey = snapshot
|
||||
.getLoadable(
|
||||
savedViewFiltersByKeyScopedFamilySelector({
|
||||
scopeId: viewScopeId,
|
||||
viewId: currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
if (!savedViewFiltersByKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const onViewFiltersChange = snapshot
|
||||
.getLoadable(onViewFiltersChangeScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
|
||||
const existingSavedFilterId =
|
||||
savedViewFiltersByKey[filterToUpsert.fieldId]?.id;
|
||||
|
||||
setCurrentViewFilters?.((filters) => {
|
||||
set(currentViewFiltersState, (filters) => {
|
||||
const newViewFilters = produce(filters, (filtersDraft) => {
|
||||
const existingFilterIndex = filtersDraft.findIndex(
|
||||
(filter) => filter.fieldId === filterToUpsert.fieldId,
|
||||
@ -211,38 +182,29 @@ export const useViewFilters = (viewScopeId: string) => {
|
||||
return newViewFilters;
|
||||
});
|
||||
},
|
||||
[currentViewFiltersState, viewScopeId],
|
||||
);
|
||||
|
||||
const removeViewFilter = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
({ snapshot, set }) =>
|
||||
(fieldId: string) => {
|
||||
const currentViewId = snapshot
|
||||
.getLoadable(currentViewIdScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
const { currentViewId, currentViewFilters, onViewFiltersChange } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId,
|
||||
});
|
||||
|
||||
if (!currentViewId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const onViewFiltersChange = snapshot
|
||||
.getLoadable(onViewFiltersChangeScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
|
||||
const currentViewFilters = snapshot
|
||||
.getLoadable(
|
||||
currentViewFiltersScopedFamilyState({
|
||||
scopeId: viewScopeId,
|
||||
familyKey: currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
const newViewFilters = currentViewFilters.filter((filter) => {
|
||||
return filter.fieldId !== fieldId;
|
||||
});
|
||||
setCurrentViewFilters?.(newViewFilters);
|
||||
set(currentViewFiltersState, newViewFilters);
|
||||
onViewFiltersChange?.(newViewFilters);
|
||||
},
|
||||
[currentViewFiltersState, viewScopeId],
|
||||
);
|
||||
|
||||
return { persistViewFilters, removeViewFilter, upsertViewFilter };
|
||||
|
||||
@ -0,0 +1,84 @@
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId';
|
||||
import { getScopedState } from '@/ui/utilities/recoil-scope/utils/getScopedState';
|
||||
|
||||
import { UNDEFINED_FAMILY_ITEM_ID } from '../../constants';
|
||||
import { ViewScopeInternalContext } from '../../scopes/scope-internal-context/ViewScopeInternalContext';
|
||||
import { currentViewIdScopedState } from '../../states/currentViewIdScopedState';
|
||||
import { getViewScopedStates } from '../../utils/internal/getViewScopedStates';
|
||||
|
||||
export const useViewScopedStates = (args?: { customViewScopeId?: string }) => {
|
||||
const { customViewScopeId } = args ?? {};
|
||||
|
||||
const scopeId = useAvailableScopeIdOrThrow(
|
||||
ViewScopeInternalContext,
|
||||
customViewScopeId,
|
||||
);
|
||||
|
||||
// View
|
||||
const [currentViewId] = useRecoilState(
|
||||
getScopedState(currentViewIdScopedState, scopeId),
|
||||
);
|
||||
|
||||
const viewId = currentViewId ?? UNDEFINED_FAMILY_ITEM_ID;
|
||||
|
||||
const {
|
||||
availableFieldDefinitionsState,
|
||||
availableFilterDefinitionsState,
|
||||
availableSortDefinitionsState,
|
||||
canPersistFiltersSelector,
|
||||
canPersistSortsSelector,
|
||||
currentViewFieldsState,
|
||||
currentViewFiltersState,
|
||||
currentViewIdState,
|
||||
currentViewSelector,
|
||||
currentViewSortsState,
|
||||
entityCountInCurrentViewState,
|
||||
isViewBarExpandedState,
|
||||
onViewFieldsChangeState,
|
||||
onViewFiltersChangeState,
|
||||
onViewSortsChangeState,
|
||||
savedViewFieldsByKeySelector,
|
||||
savedViewFieldsState,
|
||||
savedViewFiltersByKeySelector,
|
||||
savedViewFiltersState,
|
||||
savedViewSortsByKeySelector,
|
||||
savedViewSortsState,
|
||||
viewEditModeState,
|
||||
viewObjectIdState,
|
||||
viewTypeState,
|
||||
viewsState,
|
||||
} = getViewScopedStates({
|
||||
viewScopeId: scopeId,
|
||||
viewId,
|
||||
});
|
||||
|
||||
return {
|
||||
availableFieldDefinitionsState,
|
||||
availableFilterDefinitionsState,
|
||||
availableSortDefinitionsState,
|
||||
canPersistFiltersSelector,
|
||||
canPersistSortsSelector,
|
||||
currentViewFieldsState,
|
||||
currentViewFiltersState,
|
||||
currentViewIdState,
|
||||
currentViewSelector,
|
||||
currentViewSortsState,
|
||||
entityCountInCurrentViewState,
|
||||
isViewBarExpandedState,
|
||||
onViewFieldsChangeState,
|
||||
onViewFiltersChangeState,
|
||||
onViewSortsChangeState,
|
||||
savedViewFieldsByKeySelector,
|
||||
savedViewFieldsState,
|
||||
savedViewFiltersByKeySelector,
|
||||
savedViewFiltersState,
|
||||
savedViewSortsByKeySelector,
|
||||
savedViewSortsState,
|
||||
viewEditModeState,
|
||||
viewObjectIdState,
|
||||
viewTypeState,
|
||||
viewsState,
|
||||
};
|
||||
};
|
||||
@ -4,14 +4,11 @@ import { useRecoilCallback } from 'recoil';
|
||||
|
||||
import { useFindOneObjectMetadataItem } from '@/metadata/hooks/useFindOneObjectMetadataItem';
|
||||
import { Sort } from '@/ui/object/object-sort-dropdown/types/Sort';
|
||||
import { currentViewIdScopedState } from '@/views/states/currentViewIdScopedState';
|
||||
import { currentViewSortsScopedFamilyState } from '@/views/states/currentViewSortsScopedFamilyState';
|
||||
import { onViewSortsChangeScopedState } from '@/views/states/onViewSortsChangeScopedState';
|
||||
import { savedViewSortsScopedFamilyState } from '@/views/states/savedViewSortsScopedFamilyState';
|
||||
import { savedViewSortsByKeyScopedFamilySelector } from '@/views/states/selectors/savedViewSortsByKeyScopedFamilySelector';
|
||||
import { ViewSort } from '@/views/types/ViewSort';
|
||||
import { getViewScopedStateValuesFromSnapshot } from '@/views/utils/getViewScopedStateValuesFromSnapshot';
|
||||
|
||||
import { useViewSetStates } from '../useViewSetStates';
|
||||
import { useViewScopedStates } from './useViewScopedStates';
|
||||
|
||||
export const useViewSorts = (viewScopeId: string) => {
|
||||
const {
|
||||
@ -23,18 +20,29 @@ export const useViewSorts = (viewScopeId: string) => {
|
||||
objectNameSingular: 'viewSortV2',
|
||||
});
|
||||
const apolloClient = useApolloClient();
|
||||
const { setCurrentViewSorts } = useViewSetStates(viewScopeId);
|
||||
|
||||
const { currentViewSortsState } = useViewScopedStates();
|
||||
|
||||
const persistViewSorts = useRecoilCallback(
|
||||
({ snapshot, set }) =>
|
||||
async (viewId?: string) => {
|
||||
const currentViewId = snapshot
|
||||
.getLoadable(currentViewIdScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
const { currentViewId, currentViewSorts, savedViewSortsByKey } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId,
|
||||
});
|
||||
|
||||
if (!currentViewId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!currentViewSorts) {
|
||||
return;
|
||||
}
|
||||
if (!savedViewSortsByKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const createViewSorts = (viewSortsToCreate: ViewSort[]) => {
|
||||
if (!viewSortsToCreate.length) return;
|
||||
|
||||
@ -88,31 +96,6 @@ export const useViewSorts = (viewScopeId: string) => {
|
||||
);
|
||||
};
|
||||
|
||||
const currentViewSorts = snapshot
|
||||
.getLoadable(
|
||||
currentViewSortsScopedFamilyState({
|
||||
scopeId: viewScopeId,
|
||||
familyKey: currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
const savedViewSortsByKey = snapshot
|
||||
.getLoadable(
|
||||
savedViewSortsByKeyScopedFamilySelector({
|
||||
scopeId: viewScopeId,
|
||||
viewId: viewId ?? currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
if (!currentViewSorts) {
|
||||
return;
|
||||
}
|
||||
if (!savedViewSortsByKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sortsToCreate = currentViewSorts.filter(
|
||||
(sort) => !savedViewSortsByKey[sort.fieldId],
|
||||
);
|
||||
@ -153,37 +136,26 @@ export const useViewSorts = (viewScopeId: string) => {
|
||||
);
|
||||
|
||||
const upsertViewSort = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
({ snapshot, set }) =>
|
||||
(sortToUpsert: Sort) => {
|
||||
const currentViewId = snapshot
|
||||
.getLoadable(currentViewIdScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
const { currentViewId, onViewSortsChange, savedViewSortsByKey } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId,
|
||||
});
|
||||
|
||||
if (!currentViewId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const savedViewSortsByKey = snapshot
|
||||
.getLoadable(
|
||||
savedViewSortsByKeyScopedFamilySelector({
|
||||
scopeId: viewScopeId,
|
||||
viewId: currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
if (!savedViewSortsByKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const onViewSortsChange = snapshot
|
||||
.getLoadable(onViewSortsChangeScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
|
||||
const existingSavedSortId =
|
||||
savedViewSortsByKey[sortToUpsert.fieldId]?.id;
|
||||
|
||||
setCurrentViewSorts?.((sorts) => {
|
||||
set(currentViewSortsState, (sorts) => {
|
||||
const newViewSorts = produce(sorts, (sortsDraft) => {
|
||||
const existingSortIndex = sortsDraft.findIndex(
|
||||
(sort) => sort.fieldId === sortToUpsert.fieldId,
|
||||
@ -203,38 +175,29 @@ export const useViewSorts = (viewScopeId: string) => {
|
||||
return newViewSorts;
|
||||
});
|
||||
},
|
||||
[currentViewSortsState, viewScopeId],
|
||||
);
|
||||
|
||||
const removeViewSort = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
({ snapshot, set }) =>
|
||||
(fieldId: string) => {
|
||||
const currentViewId = snapshot
|
||||
.getLoadable(currentViewIdScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
const { currentViewId, onViewSortsChange, currentViewSorts } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId,
|
||||
});
|
||||
|
||||
if (!currentViewId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const onViewSortsChange = snapshot
|
||||
.getLoadable(onViewSortsChangeScopedState({ scopeId: viewScopeId }))
|
||||
.getValue();
|
||||
|
||||
const currentViewSorts = snapshot
|
||||
.getLoadable(
|
||||
currentViewSortsScopedFamilyState({
|
||||
scopeId: viewScopeId,
|
||||
familyKey: currentViewId,
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
const newViewSorts = currentViewSorts.filter((filter) => {
|
||||
return filter.fieldId !== fieldId;
|
||||
});
|
||||
setCurrentViewSorts?.(newViewSorts);
|
||||
set(currentViewSortsState, newViewSorts);
|
||||
onViewSortsChange?.(newViewSorts);
|
||||
},
|
||||
[currentViewSortsState, viewScopeId],
|
||||
);
|
||||
|
||||
return { persistViewSorts, upsertViewSort, removeViewSort };
|
||||
|
||||
@ -2,9 +2,8 @@ import { useApolloClient } from '@apollo/client';
|
||||
import { useRecoilCallback } from 'recoil';
|
||||
|
||||
import { useFindOneObjectMetadataItem } from '@/metadata/hooks/useFindOneObjectMetadataItem';
|
||||
import { viewObjectIdScopeState } from '@/views/states/viewObjectIdScopeState';
|
||||
import { viewTypeScopedState } from '@/views/states/viewTypeScopedState';
|
||||
import { View } from '@/views/types/View';
|
||||
import { getViewScopedStateValuesFromSnapshot } from '@/views/utils/getViewScopedStateValuesFromSnapshot';
|
||||
|
||||
export const useViews = (scopeId: string) => {
|
||||
const {
|
||||
@ -20,13 +19,12 @@ export const useViews = (scopeId: string) => {
|
||||
const createView = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
async (view: Pick<View, 'id' | 'name'>) => {
|
||||
const viewObjectId = await snapshot
|
||||
.getLoadable(viewObjectIdScopeState({ scopeId }))
|
||||
.getValue();
|
||||
|
||||
const viewType = await snapshot
|
||||
.getLoadable(viewTypeScopedState({ scopeId }))
|
||||
.getValue();
|
||||
const { viewObjectId, viewType } = getViewScopedStateValuesFromSnapshot(
|
||||
{
|
||||
snapshot,
|
||||
viewScopeId: scopeId,
|
||||
},
|
||||
);
|
||||
|
||||
if (!viewObjectId || !viewType) {
|
||||
return;
|
||||
@ -43,6 +41,7 @@ export const useViews = (scopeId: string) => {
|
||||
refetchQueries: [findManyQuery],
|
||||
});
|
||||
},
|
||||
[scopeId, apolloClient, createOneMutation, findManyQuery],
|
||||
);
|
||||
|
||||
const updateView = async (view: View) => {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { useRecoilCallback } from 'recoil';
|
||||
import { useRecoilCallback, useRecoilState, useSetRecoilState } from 'recoil';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId';
|
||||
@ -8,22 +8,15 @@ import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-i
|
||||
import { ViewScopeInternalContext } from '../scopes/scope-internal-context/ViewScopeInternalContext';
|
||||
import { currentViewFieldsScopedFamilyState } from '../states/currentViewFieldsScopedFamilyState';
|
||||
import { currentViewFiltersScopedFamilyState } from '../states/currentViewFiltersScopedFamilyState';
|
||||
import { currentViewIdScopedState } from '../states/currentViewIdScopedState';
|
||||
import { currentViewSortsScopedFamilyState } from '../states/currentViewSortsScopedFamilyState';
|
||||
import { onViewFieldsChangeScopedState } from '../states/onViewFieldsChangeScopedState';
|
||||
import { onViewFiltersChangeScopedState } from '../states/onViewFiltersChangeScopedState';
|
||||
import { onViewSortsChangeScopedState } from '../states/onViewSortsChangeScopedState';
|
||||
import { savedViewFiltersScopedFamilyState } from '../states/savedViewFiltersScopedFamilyState';
|
||||
import { savedViewSortsScopedFamilyState } from '../states/savedViewSortsScopedFamilyState';
|
||||
import { currentViewScopedSelector } from '../states/selectors/currentViewScopedSelector';
|
||||
import { viewEditModeScopedState } from '../states/viewEditModeScopedState';
|
||||
import { viewsScopedState } from '../states/viewsScopedState';
|
||||
import { getViewScopedStatesFromSnapshot } from '../utils/getViewScopedStatesFromSnapshot';
|
||||
import { getViewScopedStateValuesFromSnapshot } from '../utils/getViewScopedStateValuesFromSnapshot';
|
||||
|
||||
import { useViewFields } from './internal/useViewFields';
|
||||
import { useViewFilters } from './internal/useViewFilters';
|
||||
import { useViews } from './internal/useViews';
|
||||
import { useViewScopedStates } from './internal/useViewScopedStates';
|
||||
import { useViewSorts } from './internal/useViewSorts';
|
||||
import { useViewSetStates } from './useViewSetStates';
|
||||
|
||||
type UseViewProps = {
|
||||
viewScopeId?: string;
|
||||
@ -36,32 +29,19 @@ export const useView = (props?: UseViewProps) => {
|
||||
);
|
||||
|
||||
const {
|
||||
setCurrentViewId,
|
||||
currentViewId,
|
||||
|
||||
setViews,
|
||||
setViewEditMode,
|
||||
setViewObjectId,
|
||||
setViewType,
|
||||
setEntityCountInCurrentView,
|
||||
setIsViewBarExpanded,
|
||||
|
||||
setAvailableSortDefinitions,
|
||||
setCurrentViewSorts,
|
||||
setSavedViewSorts,
|
||||
|
||||
setAvailableFilterDefinitions,
|
||||
setCurrentViewFilters,
|
||||
setSavedViewFilters,
|
||||
|
||||
setAvailableFieldDefinitions,
|
||||
setCurrentViewFields,
|
||||
setSavedViewFields,
|
||||
|
||||
setOnViewFieldsChange,
|
||||
setOnViewFiltersChange,
|
||||
setOnViewSortsChange,
|
||||
} = useViewSetStates(scopeId);
|
||||
currentViewFiltersState,
|
||||
currentViewIdState,
|
||||
currentViewSortsState,
|
||||
viewEditModeState,
|
||||
availableFieldDefinitionsState,
|
||||
availableFilterDefinitionsState,
|
||||
availableSortDefinitionsState,
|
||||
entityCountInCurrentViewState,
|
||||
viewObjectIdState,
|
||||
viewTypeState,
|
||||
} = useViewScopedStates({
|
||||
customViewScopeId: scopeId,
|
||||
});
|
||||
|
||||
const { persistViewSorts, upsertViewSort, removeViewSort } =
|
||||
useViewSorts(scopeId);
|
||||
@ -73,6 +53,28 @@ export const useView = (props?: UseViewProps) => {
|
||||
updateView: internalUpdateView,
|
||||
deleteView: internalDeleteView,
|
||||
} = useViews(scopeId);
|
||||
|
||||
const [currentViewId, setCurrentViewId] = useRecoilState(currentViewIdState);
|
||||
const setAvailableFieldDefinitions = useSetRecoilState(
|
||||
availableFieldDefinitionsState,
|
||||
);
|
||||
|
||||
const setAvailableSortDefinitions = useSetRecoilState(
|
||||
availableSortDefinitionsState,
|
||||
);
|
||||
|
||||
const setAvailableFilterDefinitions = useSetRecoilState(
|
||||
availableFilterDefinitionsState,
|
||||
);
|
||||
|
||||
const setEntityCountInCurrentView = useSetRecoilState(
|
||||
entityCountInCurrentViewState,
|
||||
);
|
||||
|
||||
const setViewEditMode = useSetRecoilState(viewEditModeState);
|
||||
const setViewObjectId = useSetRecoilState(viewObjectIdState);
|
||||
const setViewType = useSetRecoilState(viewTypeState);
|
||||
|
||||
const [_, setSearchParams] = useSearchParams();
|
||||
|
||||
const changeViewInUrl = useCallback(
|
||||
@ -82,72 +84,62 @@ export const useView = (props?: UseViewProps) => {
|
||||
[setSearchParams],
|
||||
);
|
||||
|
||||
const loadView = useRecoilCallback(({ snapshot }) => (viewId: string) => {
|
||||
setCurrentViewId?.(viewId);
|
||||
const currentViewFields = snapshot
|
||||
.getLoadable(
|
||||
currentViewFieldsScopedFamilyState({ scopeId, familyKey: viewId }),
|
||||
)
|
||||
.getValue();
|
||||
const loadView = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
(viewId: string) => {
|
||||
setCurrentViewId?.(viewId);
|
||||
|
||||
const onViewFieldsChange = snapshot
|
||||
.getLoadable(onViewFieldsChangeScopedState({ scopeId }))
|
||||
.getValue();
|
||||
const {
|
||||
currentViewFields,
|
||||
onViewFieldsChange,
|
||||
currentViewFilters,
|
||||
onViewFiltersChange,
|
||||
currentViewSorts,
|
||||
onViewSortsChange,
|
||||
} = getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId: scopeId,
|
||||
viewId,
|
||||
});
|
||||
|
||||
onViewFieldsChange?.(currentViewFields);
|
||||
onViewFieldsChange?.(currentViewFields);
|
||||
onViewFiltersChange?.(currentViewFilters);
|
||||
onViewSortsChange?.(currentViewSorts);
|
||||
},
|
||||
[setCurrentViewId, scopeId],
|
||||
);
|
||||
|
||||
const currentViewFilters = snapshot
|
||||
.getLoadable(
|
||||
currentViewFiltersScopedFamilyState({ scopeId, familyKey: viewId }),
|
||||
)
|
||||
.getValue();
|
||||
const resetViewBar = useRecoilCallback(
|
||||
({ snapshot, set }) =>
|
||||
() => {
|
||||
const {
|
||||
savedViewFilters,
|
||||
savedViewSorts,
|
||||
onViewFiltersChange,
|
||||
onViewSortsChange,
|
||||
} = getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId: scopeId,
|
||||
});
|
||||
|
||||
const onViewFiltersChange = snapshot
|
||||
.getLoadable(onViewFiltersChangeScopedState({ scopeId }))
|
||||
.getValue();
|
||||
if (savedViewFilters) {
|
||||
set(currentViewFiltersState, savedViewFilters);
|
||||
onViewFiltersChange?.(savedViewFilters);
|
||||
}
|
||||
if (savedViewSorts) {
|
||||
set(currentViewSortsState, savedViewSorts);
|
||||
onViewSortsChange?.(savedViewSorts);
|
||||
}
|
||||
|
||||
onViewFiltersChange?.(currentViewFilters);
|
||||
|
||||
const currentViewSorts = snapshot
|
||||
.getLoadable(
|
||||
currentViewSortsScopedFamilyState({ scopeId, familyKey: viewId }),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
const onViewSortsChange = snapshot
|
||||
.getLoadable(onViewSortsChangeScopedState({ scopeId }))
|
||||
.getValue();
|
||||
|
||||
onViewSortsChange?.(currentViewSorts);
|
||||
});
|
||||
|
||||
const resetViewBar = useRecoilCallback(({ snapshot }) => () => {
|
||||
const savedViewFilters = snapshot
|
||||
.getLoadable(
|
||||
savedViewFiltersScopedFamilyState({
|
||||
scopeId,
|
||||
familyKey: currentViewId || '',
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
const savedViewSorts = snapshot
|
||||
.getLoadable(
|
||||
savedViewSortsScopedFamilyState({
|
||||
scopeId,
|
||||
familyKey: currentViewId || '',
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
if (savedViewFilters) {
|
||||
setCurrentViewFilters?.(savedViewFilters);
|
||||
}
|
||||
if (savedViewSorts) {
|
||||
setCurrentViewSorts?.(savedViewSorts);
|
||||
}
|
||||
setViewEditMode?.('none');
|
||||
});
|
||||
set(viewEditModeState, 'none');
|
||||
},
|
||||
[
|
||||
currentViewFiltersState,
|
||||
currentViewSortsState,
|
||||
scopeId,
|
||||
viewEditModeState,
|
||||
],
|
||||
);
|
||||
|
||||
const createView = useRecoilCallback(
|
||||
({ snapshot, set }) =>
|
||||
@ -155,29 +147,17 @@ export const useView = (props?: UseViewProps) => {
|
||||
const newViewId = v4();
|
||||
await internalCreateView({ id: newViewId, name });
|
||||
|
||||
const currentViewFields = snapshot
|
||||
.getLoadable(
|
||||
currentViewFieldsScopedFamilyState({
|
||||
scopeId,
|
||||
familyKey: currentViewId || '',
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
const { currentViewFields, currentViewFilters, currentViewSorts } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId: scopeId,
|
||||
});
|
||||
|
||||
set(
|
||||
currentViewFieldsScopedFamilyState({ scopeId, familyKey: newViewId }),
|
||||
currentViewFields,
|
||||
);
|
||||
|
||||
const currentViewFilters = snapshot
|
||||
.getLoadable(
|
||||
currentViewFiltersScopedFamilyState({
|
||||
scopeId,
|
||||
familyKey: currentViewId || '',
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
set(
|
||||
currentViewFiltersScopedFamilyState({
|
||||
scopeId,
|
||||
@ -186,15 +166,6 @@ export const useView = (props?: UseViewProps) => {
|
||||
currentViewFilters,
|
||||
);
|
||||
|
||||
const currentViewSorts = snapshot
|
||||
.getLoadable(
|
||||
currentViewSortsScopedFamilyState({
|
||||
scopeId,
|
||||
familyKey: currentViewId || '',
|
||||
}),
|
||||
)
|
||||
.getValue();
|
||||
|
||||
set(
|
||||
currentViewSortsScopedFamilyState({
|
||||
scopeId,
|
||||
@ -211,7 +182,6 @@ export const useView = (props?: UseViewProps) => {
|
||||
},
|
||||
[
|
||||
changeViewInUrl,
|
||||
currentViewId,
|
||||
internalCreateView,
|
||||
persistViewFields,
|
||||
persistViewFilters,
|
||||
@ -228,19 +198,30 @@ export const useView = (props?: UseViewProps) => {
|
||||
const removeView = useRecoilCallback(
|
||||
({ set, snapshot }) =>
|
||||
async (viewIdToDelete: string) => {
|
||||
const currentViewId = await snapshot.getPromise(
|
||||
currentViewIdScopedState({ scopeId }),
|
||||
);
|
||||
const { currentViewId } = getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId: scopeId,
|
||||
});
|
||||
|
||||
if (currentViewId === viewIdToDelete)
|
||||
set(currentViewIdScopedState({ scopeId }), undefined);
|
||||
const { currentViewIdState, viewsState } =
|
||||
getViewScopedStatesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId: scopeId,
|
||||
});
|
||||
|
||||
set(viewsScopedState({ scopeId }), (previousViews) =>
|
||||
if (currentViewId === viewIdToDelete) {
|
||||
set(currentViewIdState, undefined);
|
||||
}
|
||||
|
||||
set(viewsState, (previousViews) =>
|
||||
previousViews.filter((view) => view.id !== viewIdToDelete),
|
||||
);
|
||||
|
||||
internalDeleteView(viewIdToDelete);
|
||||
|
||||
if (currentViewId === viewIdToDelete) setSearchParams();
|
||||
if (currentViewId === viewIdToDelete) {
|
||||
setSearchParams();
|
||||
}
|
||||
},
|
||||
[internalDeleteView, scopeId, setSearchParams],
|
||||
);
|
||||
@ -252,13 +233,11 @@ export const useView = (props?: UseViewProps) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const viewEditMode = snapshot
|
||||
.getLoadable(viewEditModeScopedState({ scopeId }))
|
||||
.getValue();
|
||||
|
||||
const currentView = snapshot
|
||||
.getLoadable(currentViewScopedSelector(scopeId))
|
||||
.getValue();
|
||||
const { viewEditMode, currentView } =
|
||||
getViewScopedStateValuesFromSnapshot({
|
||||
snapshot,
|
||||
viewScopeId: scopeId,
|
||||
});
|
||||
|
||||
if (!currentView) {
|
||||
return;
|
||||
@ -284,38 +263,25 @@ export const useView = (props?: UseViewProps) => {
|
||||
updateCurrentView,
|
||||
createView,
|
||||
removeView,
|
||||
setIsViewBarExpanded,
|
||||
resetViewBar,
|
||||
handleViewNameSubmit,
|
||||
|
||||
setViews,
|
||||
setViewEditMode,
|
||||
setViewObjectId,
|
||||
setViewType,
|
||||
setEntityCountInCurrentView,
|
||||
setAvailableFieldDefinitions,
|
||||
|
||||
setAvailableSortDefinitions,
|
||||
setCurrentViewSorts,
|
||||
setSavedViewSorts,
|
||||
upsertViewSort,
|
||||
removeViewSort,
|
||||
|
||||
setAvailableFilterDefinitions,
|
||||
setCurrentViewFilters,
|
||||
setSavedViewFilters,
|
||||
upsertViewFilter,
|
||||
removeViewFilter,
|
||||
|
||||
setAvailableFieldDefinitions,
|
||||
setCurrentViewFields,
|
||||
setSavedViewFields,
|
||||
|
||||
persistViewFields,
|
||||
changeViewInUrl,
|
||||
loadView,
|
||||
|
||||
setOnViewFieldsChange,
|
||||
setOnViewFiltersChange,
|
||||
setOnViewSortsChange,
|
||||
};
|
||||
};
|
||||
|
||||
@ -1,211 +0,0 @@
|
||||
import { useRecoilValue } from 'recoil';
|
||||
|
||||
import { useRecoilScopedFamilyState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedFamilyState';
|
||||
import { useRecoilScopedStateV2 } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedStateV2';
|
||||
import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId';
|
||||
|
||||
import { ViewScopeInternalContext } from '../scopes/scope-internal-context/ViewScopeInternalContext';
|
||||
import { availableFieldDefinitionsScopedState } from '../states/availableFieldDefinitionsScopedState';
|
||||
import { availableFilterDefinitionsScopedState } from '../states/availableFilterDefinitionsScopedState';
|
||||
import { availableSortDefinitionsScopedState } from '../states/availableSortDefinitionsScopedState';
|
||||
import { currentViewFieldsScopedFamilyState } from '../states/currentViewFieldsScopedFamilyState';
|
||||
import { currentViewFiltersScopedFamilyState } from '../states/currentViewFiltersScopedFamilyState';
|
||||
import { currentViewIdScopedState } from '../states/currentViewIdScopedState';
|
||||
import { currentViewSortsScopedFamilyState } from '../states/currentViewSortsScopedFamilyState';
|
||||
import { entityCountInCurrentViewScopedState } from '../states/entityCountInCurrentViewScopedState';
|
||||
import { isViewBarExpandedScopedState } from '../states/isViewBarExpandedScopedState';
|
||||
import { onViewFieldsChangeScopedState } from '../states/onViewFieldsChangeScopedState';
|
||||
import { onViewFiltersChangeScopedState } from '../states/onViewFiltersChangeScopedState';
|
||||
import { onViewSortsChangeScopedState } from '../states/onViewSortsChangeScopedState';
|
||||
import { savedViewFieldsScopedFamilyState } from '../states/savedViewFieldsScopedFamilyState';
|
||||
import { savedViewFiltersScopedFamilyState } from '../states/savedViewFiltersScopedFamilyState';
|
||||
import { savedViewSortsScopedFamilyState } from '../states/savedViewSortsScopedFamilyState';
|
||||
import { canPersistViewFiltersScopedFamilySelector } from '../states/selectors/canPersistViewFiltersScopedFamilySelector';
|
||||
import { canPersistViewSortsScopedFamilySelector } from '../states/selectors/canPersistViewSortsScopedFamilySelector';
|
||||
import { currentViewScopedSelector } from '../states/selectors/currentViewScopedSelector';
|
||||
import { savedViewFieldByKeyScopedFamilySelector } from '../states/selectors/savedViewFieldByKeyScopedFamilySelector';
|
||||
import { savedViewFiltersByKeyScopedFamilySelector } from '../states/selectors/savedViewFiltersByKeyScopedFamilySelector';
|
||||
import { savedViewSortsByKeyScopedFamilySelector } from '../states/selectors/savedViewSortsByKeyScopedFamilySelector';
|
||||
import { viewEditModeScopedState } from '../states/viewEditModeScopedState';
|
||||
import { viewObjectIdScopeState } from '../states/viewObjectIdScopeState';
|
||||
import { viewsScopedState } from '../states/viewsScopedState';
|
||||
import { viewTypeScopedState } from '../states/viewTypeScopedState';
|
||||
|
||||
export const useViewGetStates = (viewScopeId?: string, viewId?: string) => {
|
||||
const scopeId = useAvailableScopeIdOrThrow(
|
||||
ViewScopeInternalContext,
|
||||
viewScopeId,
|
||||
);
|
||||
|
||||
// View
|
||||
const [currentViewId] = useRecoilScopedStateV2(
|
||||
currentViewIdScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const familyItemId = viewId ?? currentViewId;
|
||||
|
||||
const currentView = useRecoilValue(currentViewScopedSelector(scopeId));
|
||||
|
||||
const [viewEditMode] = useRecoilScopedStateV2(
|
||||
viewEditModeScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const [views] = useRecoilScopedStateV2(viewsScopedState, scopeId);
|
||||
|
||||
const [viewObjectId] = useRecoilScopedStateV2(
|
||||
viewObjectIdScopeState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const [viewType] = useRecoilScopedStateV2(viewTypeScopedState, scopeId);
|
||||
|
||||
const [entityCountInCurrentView] = useRecoilScopedStateV2(
|
||||
entityCountInCurrentViewScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const [isViewBarExpanded] = useRecoilScopedStateV2(
|
||||
isViewBarExpandedScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
// ViewSorts
|
||||
const [currentViewSorts] = useRecoilScopedFamilyState(
|
||||
currentViewSortsScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const [savedViewSorts] = useRecoilScopedFamilyState(
|
||||
savedViewSortsScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const savedViewSortsByKey = useRecoilValue(
|
||||
savedViewSortsByKeyScopedFamilySelector({
|
||||
scopeId: scopeId,
|
||||
viewId: familyItemId,
|
||||
}),
|
||||
);
|
||||
|
||||
const [availableSortDefinitions] = useRecoilScopedStateV2(
|
||||
availableSortDefinitionsScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const canPersistSorts = useRecoilValue(
|
||||
canPersistViewSortsScopedFamilySelector({
|
||||
viewScopeId: scopeId,
|
||||
viewId: familyItemId,
|
||||
}),
|
||||
);
|
||||
|
||||
// ViewFilters
|
||||
const [currentViewFilters] = useRecoilScopedFamilyState(
|
||||
currentViewFiltersScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const [savedViewFilters] = useRecoilScopedFamilyState(
|
||||
savedViewFiltersScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const savedViewFiltersByKey = useRecoilValue(
|
||||
savedViewFiltersByKeyScopedFamilySelector({
|
||||
scopeId: scopeId,
|
||||
viewId: familyItemId,
|
||||
}),
|
||||
);
|
||||
|
||||
const [availableFilterDefinitions] = useRecoilScopedStateV2(
|
||||
availableFilterDefinitionsScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const canPersistFilters = useRecoilValue(
|
||||
canPersistViewFiltersScopedFamilySelector({
|
||||
viewScopeId: scopeId,
|
||||
viewId: familyItemId,
|
||||
}),
|
||||
);
|
||||
|
||||
// ViewFields
|
||||
const [availableFieldDefinitions] = useRecoilScopedStateV2(
|
||||
availableFieldDefinitionsScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const [currentViewFields] = useRecoilScopedFamilyState(
|
||||
currentViewFieldsScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const [savedViewFields] = useRecoilScopedFamilyState(
|
||||
savedViewFieldsScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const savedViewFieldsByKey = useRecoilValue(
|
||||
savedViewFieldByKeyScopedFamilySelector({
|
||||
viewScopeId: scopeId,
|
||||
viewId: familyItemId,
|
||||
}),
|
||||
);
|
||||
|
||||
// ViewChangeHandlers
|
||||
const [onViewSortsChange] = useRecoilScopedStateV2(
|
||||
onViewSortsChangeScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const [onViewFiltersChange] = useRecoilScopedStateV2(
|
||||
onViewFiltersChangeScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const [onViewFieldsChange] = useRecoilScopedStateV2(
|
||||
onViewFieldsChangeScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
return {
|
||||
currentViewId,
|
||||
currentView,
|
||||
isViewBarExpanded,
|
||||
|
||||
views,
|
||||
viewEditMode,
|
||||
viewObjectId,
|
||||
viewType,
|
||||
entityCountInCurrentView,
|
||||
|
||||
availableSortDefinitions,
|
||||
currentViewSorts,
|
||||
savedViewSorts,
|
||||
savedViewSortsByKey,
|
||||
canPersistSorts,
|
||||
|
||||
availableFilterDefinitions,
|
||||
currentViewFilters,
|
||||
savedViewFilters,
|
||||
savedViewFiltersByKey,
|
||||
canPersistFilters,
|
||||
|
||||
availableFieldDefinitions,
|
||||
currentViewFields,
|
||||
savedViewFieldsByKey,
|
||||
savedViewFields,
|
||||
|
||||
onViewSortsChange,
|
||||
onViewFiltersChange,
|
||||
onViewFieldsChange,
|
||||
};
|
||||
};
|
||||
@ -1,160 +0,0 @@
|
||||
import { useRecoilScopedStateV2 } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedStateV2';
|
||||
import { useSetRecoilScopedFamilyState } from '@/ui/utilities/recoil-scope/hooks/useSetRecoilScopedFamilyState';
|
||||
import { useSetRecoilScopedStateV2 } from '@/ui/utilities/recoil-scope/hooks/useSetRecoilScopedStateV2';
|
||||
import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId';
|
||||
|
||||
import { ViewScopeInternalContext } from '../scopes/scope-internal-context/ViewScopeInternalContext';
|
||||
import { availableFieldDefinitionsScopedState } from '../states/availableFieldDefinitionsScopedState';
|
||||
import { availableFilterDefinitionsScopedState } from '../states/availableFilterDefinitionsScopedState';
|
||||
import { availableSortDefinitionsScopedState } from '../states/availableSortDefinitionsScopedState';
|
||||
import { currentViewFieldsScopedFamilyState } from '../states/currentViewFieldsScopedFamilyState';
|
||||
import { currentViewFiltersScopedFamilyState } from '../states/currentViewFiltersScopedFamilyState';
|
||||
import { currentViewIdScopedState } from '../states/currentViewIdScopedState';
|
||||
import { currentViewSortsScopedFamilyState } from '../states/currentViewSortsScopedFamilyState';
|
||||
import { entityCountInCurrentViewScopedState } from '../states/entityCountInCurrentViewScopedState';
|
||||
import { isViewBarExpandedScopedState } from '../states/isViewBarExpandedScopedState';
|
||||
import { onViewFieldsChangeScopedState } from '../states/onViewFieldsChangeScopedState';
|
||||
import { onViewFiltersChangeScopedState } from '../states/onViewFiltersChangeScopedState';
|
||||
import { onViewSortsChangeScopedState } from '../states/onViewSortsChangeScopedState';
|
||||
import { savedViewFieldsScopedFamilyState } from '../states/savedViewFieldsScopedFamilyState';
|
||||
import { savedViewFiltersScopedFamilyState } from '../states/savedViewFiltersScopedFamilyState';
|
||||
import { savedViewSortsScopedFamilyState } from '../states/savedViewSortsScopedFamilyState';
|
||||
import { viewEditModeScopedState } from '../states/viewEditModeScopedState';
|
||||
import { viewObjectIdScopeState } from '../states/viewObjectIdScopeState';
|
||||
import { viewsScopedState } from '../states/viewsScopedState';
|
||||
import { viewTypeScopedState } from '../states/viewTypeScopedState';
|
||||
|
||||
export const useViewSetStates = (viewScopeId?: string, viewId?: string) => {
|
||||
const scopeId = useAvailableScopeIdOrThrow(
|
||||
ViewScopeInternalContext,
|
||||
viewScopeId,
|
||||
);
|
||||
// View
|
||||
const [currentViewId, setCurrentViewId] = useRecoilScopedStateV2(
|
||||
currentViewIdScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const setViewObjectId = useSetRecoilScopedStateV2(
|
||||
viewObjectIdScopeState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const setViewType = useSetRecoilScopedStateV2(viewTypeScopedState, scopeId);
|
||||
|
||||
const familyItemId = viewId ? viewId : currentViewId;
|
||||
|
||||
const setViewEditMode = useSetRecoilScopedStateV2(
|
||||
viewEditModeScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const setViews = useSetRecoilScopedStateV2(viewsScopedState, scopeId);
|
||||
|
||||
const setEntityCountInCurrentView = useSetRecoilScopedStateV2(
|
||||
entityCountInCurrentViewScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const setIsViewBarExpanded = useSetRecoilScopedStateV2(
|
||||
isViewBarExpandedScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
// ViewSorts
|
||||
const setCurrentViewSorts = useSetRecoilScopedFamilyState(
|
||||
currentViewSortsScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const setSavedViewSorts = useSetRecoilScopedFamilyState(
|
||||
savedViewSortsScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const setAvailableSortDefinitions = useSetRecoilScopedStateV2(
|
||||
availableSortDefinitionsScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
// ViewFilters
|
||||
const setCurrentViewFilters = useSetRecoilScopedFamilyState(
|
||||
currentViewFiltersScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const setSavedViewFilters = useSetRecoilScopedFamilyState(
|
||||
savedViewFiltersScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const setAvailableFilterDefinitions = useSetRecoilScopedStateV2(
|
||||
availableFilterDefinitionsScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
// ViewFields
|
||||
const setAvailableFieldDefinitions = useSetRecoilScopedStateV2(
|
||||
availableFieldDefinitionsScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const setCurrentViewFields = useSetRecoilScopedFamilyState(
|
||||
currentViewFieldsScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const setSavedViewFields = useSetRecoilScopedFamilyState(
|
||||
savedViewFieldsScopedFamilyState,
|
||||
scopeId,
|
||||
familyItemId,
|
||||
);
|
||||
|
||||
const setOnViewFieldsChange = useSetRecoilScopedStateV2(
|
||||
onViewFieldsChangeScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const setOnViewFiltersChange = useSetRecoilScopedStateV2(
|
||||
onViewFiltersChangeScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
const setOnViewSortsChange = useSetRecoilScopedStateV2(
|
||||
onViewSortsChangeScopedState,
|
||||
scopeId,
|
||||
);
|
||||
|
||||
return {
|
||||
currentViewId,
|
||||
setCurrentViewId,
|
||||
setIsViewBarExpanded,
|
||||
setViewObjectId,
|
||||
setViewType,
|
||||
|
||||
setViews,
|
||||
setViewEditMode,
|
||||
setEntityCountInCurrentView,
|
||||
|
||||
setAvailableSortDefinitions,
|
||||
setCurrentViewSorts,
|
||||
setSavedViewSorts,
|
||||
|
||||
setAvailableFilterDefinitions,
|
||||
setCurrentViewFilters,
|
||||
setSavedViewFilters,
|
||||
|
||||
setAvailableFieldDefinitions,
|
||||
setCurrentViewFields,
|
||||
setSavedViewFields,
|
||||
|
||||
setOnViewFieldsChange,
|
||||
setOnViewFiltersChange,
|
||||
setOnViewSortsChange,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user