2022-04-12 10:50:01 +00:00
|
|
|
import {
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxAction,
|
|
|
|
|
} from "@appsmith/constants/ReduxActionConstants";
|
2021-03-08 08:24:12 +00:00
|
|
|
import {
|
|
|
|
|
all,
|
|
|
|
|
call,
|
|
|
|
|
put,
|
|
|
|
|
takeLatest,
|
|
|
|
|
select,
|
|
|
|
|
putResolve,
|
|
|
|
|
take,
|
|
|
|
|
} from "redux-saga/effects";
|
|
|
|
|
import { setRecentAppEntities, fetchRecentAppEntities } from "utils/storage";
|
|
|
|
|
import {
|
|
|
|
|
restoreRecentEntitiesSuccess,
|
|
|
|
|
setRecentEntities,
|
|
|
|
|
} from "actions/globalSearchActions";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2021-10-18 14:03:44 +00:00
|
|
|
import {
|
|
|
|
|
getCurrentApplicationId,
|
|
|
|
|
getIsEditorInitialized,
|
|
|
|
|
} from "selectors/editorSelectors";
|
2021-03-08 08:24:12 +00:00
|
|
|
import { RecentEntity } from "components/editorComponents/GlobalSearch/utils";
|
2021-07-05 05:49:43 +00:00
|
|
|
import log from "loglevel";
|
2021-10-18 14:03:44 +00:00
|
|
|
import { getCurrentGitBranch } from "selectors/gitSyncSelectors";
|
2022-12-15 04:06:13 +00:00
|
|
|
import { FocusEntity, FocusEntityInfo } from "navigation/FocusEntity";
|
2021-10-18 14:03:44 +00:00
|
|
|
|
|
|
|
|
const getRecentEntitiesKey = (applicationId: string, branch?: string) =>
|
|
|
|
|
branch ? `${applicationId}-${branch}` : applicationId;
|
2021-03-08 08:24:12 +00:00
|
|
|
|
2022-12-15 04:06:13 +00:00
|
|
|
export function* updateRecentEntitySaga(entityInfo: FocusEntityInfo) {
|
2021-03-08 08:24:12 +00:00
|
|
|
try {
|
2022-06-21 13:57:34 +00:00
|
|
|
const branch: string | undefined = yield select(getCurrentGitBranch);
|
2021-10-18 14:03:44 +00:00
|
|
|
|
2022-06-21 13:57:34 +00:00
|
|
|
const applicationId: string = yield select(getCurrentApplicationId);
|
2021-10-18 14:03:44 +00:00
|
|
|
|
2022-06-21 13:57:34 +00:00
|
|
|
const recentEntitiesRestored: boolean = yield select(
|
2021-03-08 08:24:12 +00:00
|
|
|
(state: AppState) => state.ui.globalSearch.recentEntitiesRestored,
|
|
|
|
|
);
|
2022-06-21 13:57:34 +00:00
|
|
|
const isEditorInitialised: boolean = yield select(getIsEditorInitialized);
|
2021-03-08 08:24:12 +00:00
|
|
|
|
|
|
|
|
const waitForEffects = [];
|
|
|
|
|
|
|
|
|
|
if (!isEditorInitialised) {
|
|
|
|
|
waitForEffects.push(take(ReduxActionTypes.INITIALIZE_EDITOR_SUCCESS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!recentEntitiesRestored) {
|
|
|
|
|
waitForEffects.push(
|
|
|
|
|
take(ReduxActionTypes.RESTORE_RECENT_ENTITIES_SUCCESS),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield all(waitForEffects);
|
|
|
|
|
|
2022-12-15 04:06:13 +00:00
|
|
|
const { entity, id, pageId } = entityInfo;
|
2022-06-21 13:57:34 +00:00
|
|
|
let recentEntities: RecentEntity[] = yield select(
|
2021-03-08 08:24:12 +00:00
|
|
|
(state: AppState) => state.ui.globalSearch.recentEntities,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
recentEntities = recentEntities.slice();
|
|
|
|
|
|
2021-04-26 16:45:03 +00:00
|
|
|
recentEntities = recentEntities.filter(
|
2022-12-15 04:06:13 +00:00
|
|
|
(recentEntity: { type: FocusEntity; id: string }) =>
|
|
|
|
|
recentEntity.id !== id,
|
2021-03-08 08:24:12 +00:00
|
|
|
);
|
2022-12-15 04:06:13 +00:00
|
|
|
recentEntities.unshift(<RecentEntity>{ type: entity, id, pageId });
|
2021-04-26 16:45:03 +00:00
|
|
|
recentEntities = recentEntities.slice(0, 6);
|
2021-03-08 08:24:12 +00:00
|
|
|
|
|
|
|
|
yield put(setRecentEntities(recentEntities));
|
2021-10-18 14:03:44 +00:00
|
|
|
if (applicationId) {
|
2021-03-08 08:24:12 +00:00
|
|
|
yield call(
|
|
|
|
|
setRecentAppEntities,
|
|
|
|
|
recentEntities,
|
2021-10-18 14:03:44 +00:00
|
|
|
getRecentEntitiesKey(applicationId, branch),
|
2021-03-08 08:24:12 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2021-07-05 05:49:43 +00:00
|
|
|
log.error(e);
|
2021-03-08 08:24:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 14:03:44 +00:00
|
|
|
export function* restoreRecentEntities(
|
|
|
|
|
actionPayload: ReduxAction<{ applicationId: string; branch?: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const {
|
|
|
|
|
payload: { applicationId, branch },
|
|
|
|
|
} = actionPayload;
|
2022-06-21 13:57:34 +00:00
|
|
|
const recentAppEntities: RecentEntity[] = yield call(
|
2021-10-18 14:03:44 +00:00
|
|
|
fetchRecentAppEntities,
|
|
|
|
|
getRecentEntitiesKey(applicationId, branch),
|
|
|
|
|
);
|
2021-03-08 08:24:12 +00:00
|
|
|
yield putResolve(setRecentEntities(recentAppEntities));
|
|
|
|
|
yield put(restoreRecentEntitiesSuccess());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function* globalSearchSagas() {
|
|
|
|
|
yield all([
|
|
|
|
|
takeLatest(
|
|
|
|
|
ReduxActionTypes.RESTORE_RECENT_ENTITIES_REQUEST,
|
|
|
|
|
restoreRecentEntities,
|
|
|
|
|
),
|
|
|
|
|
]);
|
|
|
|
|
}
|