[Glitch] Collections: Add default recommendations
Port fa1e16ed9fe5633d9e86468d57b2040608677c37 to glitch-soc Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
b5bd0f2297
commit
121839f262
@ -100,6 +100,10 @@ interface ComboboxProps<
|
||||
* Icon to be displayed in the text input
|
||||
*/
|
||||
icon?: TextInputProps['icon'] | null;
|
||||
/**
|
||||
* Set to true to open as soon as there is focus
|
||||
*/
|
||||
openOnFocus?: boolean;
|
||||
/**
|
||||
* Set to false to keep the menu open when an item is selected
|
||||
*/
|
||||
@ -217,8 +221,10 @@ const ComboboxWithRef = <Item extends ComboboxItem, GroupKey extends string>(
|
||||
renderGroupTitle,
|
||||
renderItem,
|
||||
onSelectItem,
|
||||
onFocus,
|
||||
onChange,
|
||||
onKeyDown,
|
||||
openOnFocus = false,
|
||||
closeOnSelect = true,
|
||||
suppressMenu = false,
|
||||
icon = SearchIcon,
|
||||
@ -288,6 +294,16 @@ const ComboboxWithRef = <Item extends ComboboxItem, GroupKey extends string>(
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleFocus: React.FocusEventHandler<HTMLInputElement> = useCallback(
|
||||
(e) => {
|
||||
if (openOnFocus) {
|
||||
setShouldMenuOpen(true);
|
||||
}
|
||||
onFocus?.(e);
|
||||
},
|
||||
[onFocus, openOnFocus],
|
||||
);
|
||||
|
||||
const handleInputChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
onChange(e);
|
||||
@ -487,6 +503,7 @@ const ComboboxWithRef = <Item extends ComboboxItem, GroupKey extends string>(
|
||||
autoComplete='off'
|
||||
spellCheck='false'
|
||||
value={value}
|
||||
onFocus={handleFocus}
|
||||
onChange={handleInputChange}
|
||||
onKeyDown={handleInputKeyDown}
|
||||
icon={icon ?? undefined}
|
||||
|
||||
@ -218,6 +218,7 @@ export const CollectionAccounts: React.FC<{
|
||||
resetAccounts,
|
||||
} = useSearchAccounts({
|
||||
withRelationships: true,
|
||||
withDefaultFollows: searchValue === '',
|
||||
// Don't suggest accounts that were already added
|
||||
filterResults: (account) =>
|
||||
!editorItems.find((item) => item.account_id === account.id),
|
||||
@ -366,6 +367,7 @@ export const CollectionAccounts: React.FC<{
|
||||
)}
|
||||
{hasPendingItems && <PendingNote />}
|
||||
<ComboboxField
|
||||
openOnFocus
|
||||
id={inputId}
|
||||
label={intl.formatMessage({
|
||||
id: 'collections.search_accounts_label',
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import type { ApiMutedAccountJSON } from '@/flavours/glitch/api_types/accounts';
|
||||
import type { ApiAccountJSON } from '@/flavours/glitch/api_types/accounts';
|
||||
import type { Account } from '@/flavours/glitch/models/account';
|
||||
import { isServerFeatureEnabled } from '@/flavours/glitch/utils/environment';
|
||||
|
||||
@ -8,11 +8,11 @@ export function areCollectionsEnabled() {
|
||||
|
||||
export const getCollectionPath = (id: string) => `/collections/${id}`;
|
||||
|
||||
export const canAccountBeAdded = (account: ApiMutedAccountJSON | Account) =>
|
||||
export const canAccountBeAdded = (account: ApiAccountJSON | Account) =>
|
||||
['automatic', 'manual'].includes(account.feature_approval.current_user);
|
||||
|
||||
export const canAccountBeAddedByFollowers = (
|
||||
account: ApiMutedAccountJSON | Account,
|
||||
account: ApiAccountJSON | Account,
|
||||
) =>
|
||||
account.feature_approval.automatic.includes('followers') ||
|
||||
account.feature_approval.manual.includes('followers');
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { useCallback, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { useDebouncedCallback } from 'use-debounce';
|
||||
|
||||
@ -8,16 +8,20 @@ import { apiRequest } from 'flavours/glitch/api';
|
||||
import type { ApiAccountJSON } from 'flavours/glitch/api_types/accounts';
|
||||
import { useAppDispatch } from 'flavours/glitch/store';
|
||||
|
||||
import { useCurrentAccountId } from './useAccountId';
|
||||
|
||||
export function useSearchAccounts({
|
||||
onSettled,
|
||||
filterResults,
|
||||
resetOnInputClear = true,
|
||||
withRelationships = false,
|
||||
withDefaultFollows = false,
|
||||
}: {
|
||||
onSettled?: (value: string) => void;
|
||||
filterResults?: (account: ApiAccountJSON) => boolean;
|
||||
resetOnInputClear?: boolean;
|
||||
withRelationships?: boolean;
|
||||
withDefaultFollows?: boolean;
|
||||
} = {}) {
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
@ -29,7 +33,7 @@ export function useSearchAccounts({
|
||||
const searchRequestRef = useRef<AbortController | null>(null);
|
||||
|
||||
const searchAccounts = useDebouncedCallback(
|
||||
(value: string) => {
|
||||
async (value: string) => {
|
||||
if (searchRequestRef.current) {
|
||||
searchRequestRef.current.abort();
|
||||
}
|
||||
@ -46,41 +50,100 @@ export function useSearchAccounts({
|
||||
|
||||
searchRequestRef.current = new AbortController();
|
||||
|
||||
void apiRequest<ApiAccountJSON[]>('GET', 'v1/accounts/search', {
|
||||
signal: searchRequestRef.current.signal,
|
||||
params: {
|
||||
q: value,
|
||||
resolve: true,
|
||||
},
|
||||
})
|
||||
.then((data) => {
|
||||
const accounts = filterResults ? data.filter(filterResults) : data;
|
||||
const accountIds = accounts.map((a) => a.id);
|
||||
dispatch(importFetchedAccounts(accounts));
|
||||
if (withRelationships) {
|
||||
dispatch(fetchRelationships(accountIds));
|
||||
}
|
||||
setAccounts(accounts);
|
||||
setLoadingState('idle');
|
||||
onSettled?.(value);
|
||||
})
|
||||
.catch(() => {
|
||||
setLoadingState('error');
|
||||
onSettled?.(value);
|
||||
});
|
||||
try {
|
||||
const data = await apiRequest<ApiAccountJSON[]>(
|
||||
'GET',
|
||||
'v1/accounts/search',
|
||||
{
|
||||
signal: searchRequestRef.current.signal,
|
||||
params: {
|
||||
q: value,
|
||||
resolve: true,
|
||||
},
|
||||
},
|
||||
);
|
||||
const accounts = filterResults ? data.filter(filterResults) : data;
|
||||
const accountIds = accounts.map((a) => a.id);
|
||||
dispatch(importFetchedAccounts(accounts));
|
||||
if (withRelationships) {
|
||||
dispatch(fetchRelationships(accountIds));
|
||||
}
|
||||
setAccounts(accounts);
|
||||
setLoadingState('idle');
|
||||
onSettled?.(value);
|
||||
} catch {
|
||||
setLoadingState('error');
|
||||
onSettled?.(value);
|
||||
}
|
||||
},
|
||||
500,
|
||||
{ leading: true, trailing: true },
|
||||
);
|
||||
|
||||
const startSearch = useCallback(
|
||||
(value: string) => {
|
||||
void searchAccounts(value);
|
||||
},
|
||||
[searchAccounts],
|
||||
);
|
||||
|
||||
const resetAccounts = useCallback(() => {
|
||||
setAccounts([]);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
searchAccounts,
|
||||
resetAccounts,
|
||||
const currentUserId = useCurrentAccountId();
|
||||
const [defaultAccounts, setDefaultAccounts] = useState<
|
||||
ApiAccountJSON[] | null
|
||||
>(null);
|
||||
useEffect(() => {
|
||||
if (
|
||||
!currentUserId ||
|
||||
loadingState !== 'idle' ||
|
||||
defaultAccounts !== null ||
|
||||
!withDefaultFollows
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
async function doRequest() {
|
||||
setLoadingState('loading');
|
||||
try {
|
||||
const data = await apiRequest<ApiAccountJSON[]>(
|
||||
'GET',
|
||||
`v1/accounts/${currentUserId}/following`,
|
||||
{ params: { limit: 40 } },
|
||||
);
|
||||
const accounts = filterResults ? data.filter(filterResults) : data;
|
||||
const accountIds = accounts.map((a) => a.id);
|
||||
dispatch(importFetchedAccounts(accounts));
|
||||
if (withRelationships) {
|
||||
dispatch(fetchRelationships(accountIds));
|
||||
}
|
||||
setDefaultAccounts(accounts);
|
||||
setLoadingState('idle');
|
||||
} catch {
|
||||
setLoadingState('error');
|
||||
}
|
||||
}
|
||||
void doRequest();
|
||||
}, [
|
||||
currentUserId,
|
||||
accounts,
|
||||
dispatch,
|
||||
filterResults,
|
||||
loadingState,
|
||||
withRelationships,
|
||||
defaultAccounts,
|
||||
withDefaultFollows,
|
||||
]);
|
||||
|
||||
return {
|
||||
searchAccounts: startSearch,
|
||||
resetAccounts,
|
||||
accounts:
|
||||
accounts.length === 0 && withDefaultFollows
|
||||
? (defaultAccounts ?? [])
|
||||
: accounts,
|
||||
isLoading: loadingState === 'loading',
|
||||
isError: loadingState === 'error',
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user