Implements tag suggestions for collections topic field (#38307)
This commit is contained in:
1 parent
6507a61d30
commit
8bce0b99d4
11 files changed
+323
-215
No files matched your search
@@ -1,8 +1,5 @@
|
||||
import type { PayloadAction } from '@reduxjs/toolkit';
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
import { debounce } from 'lodash';
|
||||
|
||||
import { fetchAccount } from '@/mastodon/actions/accounts';
|
||||
import {
|
||||
apiDeleteFeaturedTag,
|
||||
@@ -14,7 +11,6 @@ import {
|
||||
apiPatchProfile,
|
||||
apiPostFeaturedTag,
|
||||
} from '@/mastodon/api/accounts';
|
||||
import { apiGetSearch } from '@/mastodon/api/search';
|
||||
import type { ApiAccountFieldJSON } from '@/mastodon/api_types/accounts';
|
||||
import type {
|
||||
ApiProfileJSON,
|
||||
@@ -24,7 +20,6 @@ import type {
|
||||
ApiFeaturedTagJSON,
|
||||
ApiHashtagJSON,
|
||||
} from '@/mastodon/api_types/tags';
|
||||
import type { AppDispatch } from '@/mastodon/store';
|
||||
import {
|
||||
createAppAsyncThunk,
|
||||
createAppSelector,
|
||||
@@ -59,40 +54,16 @@ export interface ProfileEditState {
|
||||
profile?: ProfileData;
|
||||
tagSuggestions?: ApiHashtagJSON[];
|
||||
isPending: boolean;
|
||||
search: {
|
||||
query: string;
|
||||
isLoading: boolean;
|
||||
results?: ApiHashtagJSON[];
|
||||
};
|
||||
}
|
||||
|
||||
const initialState: ProfileEditState = {
|
||||
isPending: false,
|
||||
search: {
|
||||
query: '',
|
||||
isLoading: false,
|
||||
},
|
||||
};
|
||||
|
||||
const profileEditSlice = createSlice({
|
||||
name: 'profileEdit',
|
||||
initialState,
|
||||
reducers: {
|
||||
setSearchQuery(state, action: PayloadAction<string>) {
|
||||
if (state.search.query === action.payload) {
|
||||
return;
|
||||
}
|
||||
|
||||
state.search.query = action.payload;
|
||||
state.search.isLoading = true;
|
||||
state.search.results = undefined;
|
||||
},
|
||||
clearSearch(state) {
|
||||
state.search.query = '';
|
||||
state.search.isLoading = false;
|
||||
state.search.results = undefined;
|
||||
},
|
||||
},
|
||||
reducers: {},
|
||||
extraReducers(builder) {
|
||||
builder.addCase(fetchProfile.fulfilled, (state, action) => {
|
||||
state.profile = action.payload;
|
||||
@@ -172,37 +143,10 @@ const profileEditSlice = createSlice({
|
||||
);
|
||||
state.isPending = false;
|
||||
});
|
||||
|
||||
builder.addCase(fetchSearchResults.pending, (state) => {
|
||||
state.search.isLoading = true;
|
||||
});
|
||||
builder.addCase(fetchSearchResults.rejected, (state) => {
|
||||
state.search.isLoading = false;
|
||||
state.search.results = undefined;
|
||||
});
|
||||
builder.addCase(fetchSearchResults.fulfilled, (state, action) => {
|
||||
state.search.isLoading = false;
|
||||
const searchResults: ApiHashtagJSON[] = [];
|
||||
const currentTags = new Set(
|
||||
(state.profile?.featuredTags ?? []).map((tag) => tag.name),
|
||||
);
|
||||
|
||||
for (const tag of action.payload) {
|
||||
if (currentTags.has(tag.name)) {
|
||||
continue;
|
||||
}
|
||||
searchResults.push(tag);
|
||||
if (searchResults.length >= 10) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
state.search.results = searchResults;
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export const profileEdit = profileEditSlice.reducer;
|
||||
export const { clearSearch } = profileEditSlice.actions;
|
||||
|
||||
const transformTag = (result: ApiFeaturedTagJSON): TagData => ({
|
||||
id: result.id,
|
||||
@@ -426,27 +370,3 @@ export const deleteFeaturedTag = createDataLoadingThunk(
|
||||
`${profileEditSlice.name}/deleteFeaturedTag`,
|
||||
({ tagId }: { tagId: string }) => apiDeleteFeaturedTag(tagId),
|
||||
);
|
||||
|
||||
const debouncedFetchSearchResults = debounce(
|
||||
async (dispatch: AppDispatch, query: string) => {
|
||||
await dispatch(fetchSearchResults({ q: query }));
|
||||
},
|
||||
300,
|
||||
);
|
||||
|
||||
export const updateSearchQuery = createAppAsyncThunk(
|
||||
`${profileEditSlice.name}/updateSearchQuery`,
|
||||
(query: string, { dispatch }) => {
|
||||
dispatch(profileEditSlice.actions.setSearchQuery(query));
|
||||
|
||||
if (query.trim().length > 0) {
|
||||
void debouncedFetchSearchResults(dispatch, query);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
export const fetchSearchResults = createDataLoadingThunk(
|
||||
`${profileEditSlice.name}/fetchSearchResults`,
|
||||
({ q }: { q: string }) => apiGetSearch({ q, type: 'hashtags', limit: 11 }),
|
||||
(result) => result.hashtags,
|
||||
);
|
||||
Reference in new issue
Block a user