mattermost-mobile/app/actions/local/team.ts
Jason Frerich 17f6aee8a5
[Gekidou MM-44927] Add Recent Searches Component (#6454)
* initial check in

* add search value to memoized dependencies in modifier component

* ignore the back press

* UI adjustments from PR feedback

* initial commit

* recent search are getting rendered from WDB

* search terms from the search bar are getting added

* can delete recent searches from WDB from recent searches Options

* will now add new ters to the table and recreate existing terms with new
timestamp

* push for scrollview

* use flatlist instead of scrolview

* s/deleteRecentTeamSearchById/removeSearchFromTeamSearchHistory/

* s/addRecentTeamSearch/addSearchToTeamSearchHistory/

* Fix search to use a flatlist and remove douplicate reference

* fix eslint

* Fix android autoscroll search field to the top

* limit the number of saved searches to 20 for a team.
return the results a team Search History sorted by createdAt

* set display to term for now

* clean up

* clean up

* extract as constant

* move styles to the top

* always update the created_at value in the database.

* remove unused function

* - remove useMemo of recent
- set or remove props on AnimatedFlatlist

* styling adjustments

* styling changes

* divider takes up 1ox so only need 15px margin to get the 16px total to
the neighboring veritcal views

* update compassIcon to match figma design

* update divider opacity to match figma design

* update styling from UX PR requests

* increase close button to touchable area of 40x40 and adjust menuitem
container

* use logError instead of console.log and trowing an error

* remove surrounding parenthesis

* There is only one record, so no need to batch.  Just call
destroyPermanently.

* call destroyPermanently directly

* when not useing the onScroll callback you don't need to set the
scrollEventThrottle

* set the max searches saved to 15

* no need to memoize

* shoud be a function call

* batch the add/update with the delete of the oldest model

* Minor improvements

* Fix bug when hitting back on search screen

* Fix long channel names in search results

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
Co-authored-by: Daniel Espino García <larkox@gmail.com>
2022-07-14 14:56:08 +02:00

87 lines
3.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import DatabaseManager from '@database/manager';
import {prepareDeleteTeam, getMyTeamById, queryTeamSearchHistoryByTeamId, removeTeamFromTeamHistory, getTeamSearchHistoryById} from '@queries/servers/team';
import {logError} from '@utils/log';
import type Model from '@nozbe/watermelondb/Model';
export async function removeUserFromTeam(serverUrl: string, teamId: string) {
try {
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const myTeam = await getMyTeamById(database, teamId);
if (myTeam) {
const team = await myTeam.team.fetch();
if (!team) {
throw new Error('Team not found');
}
const models = await prepareDeleteTeam(team);
const system = await removeTeamFromTeamHistory(operator, team.id, true);
if (system) {
models.push(...system);
}
if (models.length) {
await operator.batchRecords(models);
}
}
return {error: undefined};
} catch (error) {
logError('Failed removeUserFromTeam', error);
return {error};
}
}
export async function addSearchToTeamSearchHistory(serverUrl: string, teamId: string, terms: string) {
const MAX_TEAM_SEARCHES = 15;
try {
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const newSearch: TeamSearchHistory = {
created_at: Date.now(),
display_term: terms,
term: terms,
team_id: teamId,
};
const models: Model[] = [];
const searchModels = await operator.handleTeamSearchHistory({teamSearchHistories: [newSearch], prepareRecordsOnly: true});
const searchModel = searchModels[0];
models.push(searchModel);
// determine if need to delete the oldest entry
if (searchModel._raw._changed !== 'created_at') {
const teamSearchHistory = await queryTeamSearchHistoryByTeamId(database, teamId).fetch();
if (teamSearchHistory.length > MAX_TEAM_SEARCHES) {
const lastSearches = teamSearchHistory.slice(MAX_TEAM_SEARCHES);
for (const lastSearch of lastSearches) {
models.push(lastSearch.prepareDestroyPermanently());
}
}
}
await operator.batchRecords(models);
return {searchModel};
} catch (error) {
logError('Failed addSearchToTeamSearchHistory', error);
return {error};
}
}
export async function removeSearchFromTeamSearchHistory(serverUrl: string, id: string) {
try {
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const teamSearch = await getTeamSearchHistoryById(database, id);
if (teamSearch) {
await database.write(async () => {
await teamSearch.destroyPermanently();
});
}
return {teamSearch};
} catch (error) {
logError('Failed removeSearchFromTeamSearchHistory', error);
return {error};
}
}