Skip to content

Commit

Permalink
[Glitch] Change search to use query params in web UI
Browse files Browse the repository at this point in the history
Port 0636bcd to glitch-soc

Signed-off-by: Claire <[email protected]>
  • Loading branch information
Gargron authored and ClearlyClaire committed Dec 25, 2024
1 parent 0d25e4f commit 1bea796
Show file tree
Hide file tree
Showing 25 changed files with 1,379 additions and 1,270 deletions.
215 changes: 0 additions & 215 deletions app/javascript/flavours/glitch/actions/search.js

This file was deleted.

151 changes: 151 additions & 0 deletions app/javascript/flavours/glitch/actions/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { createAction } from '@reduxjs/toolkit';

import { apiGetSearch } from 'flavours/glitch/api/search';
import type { ApiSearchType } from 'flavours/glitch/api_types/search';
import type {
RecentSearch,
SearchType as RecentSearchType,
} from 'flavours/glitch/models/search';
import { searchHistory } from 'flavours/glitch/settings';
import {
createDataLoadingThunk,
createAppAsyncThunk,
} from 'flavours/glitch/store/typed_functions';

import { fetchRelationships } from './accounts';
import { importFetchedAccounts, importFetchedStatuses } from './importer';

export const SEARCH_HISTORY_UPDATE = 'SEARCH_HISTORY_UPDATE';

export const submitSearch = createDataLoadingThunk(
'search/submit',
async ({ q, type }: { q: string; type?: ApiSearchType }, { getState }) => {
const signedIn = !!getState().meta.get('me');

return apiGetSearch({
q,
type,
resolve: signedIn,
limit: 11,
});
},
(data, { dispatch }) => {
if (data.accounts.length > 0) {
dispatch(importFetchedAccounts(data.accounts));
dispatch(fetchRelationships(data.accounts.map((account) => account.id)));
}

if (data.statuses.length > 0) {
dispatch(importFetchedStatuses(data.statuses));
}

return data;
},
{
useLoadingBar: false,
},
);

export const expandSearch = createDataLoadingThunk(
'search/expand',
async ({ type }: { type: ApiSearchType }, { getState }) => {
const q = getState().search.q;
const results = getState().search.results;
const offset = results?.[type].length;

return apiGetSearch({
q,
type,
limit: 11,
offset,
});
},
(data, { dispatch }) => {
if (data.accounts.length > 0) {
dispatch(importFetchedAccounts(data.accounts));
dispatch(fetchRelationships(data.accounts.map((account) => account.id)));
}

if (data.statuses.length > 0) {
dispatch(importFetchedStatuses(data.statuses));
}

return data;
},
{
useLoadingBar: true,
},
);

export const openURL = createDataLoadingThunk(
'search/openURL',
({ url }: { url: string }, { getState }) => {
const signedIn = !!getState().meta.get('me');

return apiGetSearch({
q: url,
resolve: signedIn,
limit: 1,
});
},
(data, { dispatch }) => {
if (data.accounts.length > 0) {
dispatch(importFetchedAccounts(data.accounts));
} else if (data.statuses.length > 0) {
dispatch(importFetchedStatuses(data.statuses));
}

return data;
},
{
useLoadingBar: true,
},
);

export const clickSearchResult = createAppAsyncThunk(
'search/clickResult',
(
{ q, type }: { q: string; type?: RecentSearchType },
{ dispatch, getState },
) => {
const previous = getState().search.recent;

if (previous.some((x) => x.q === q && x.type === type)) {
return;
}

const me = getState().meta.get('me') as string;
const current = [{ type, q }, ...previous].slice(0, 4);

searchHistory.set(me, current);
dispatch(updateSearchHistory(current));
},
);

export const forgetSearchResult = createAppAsyncThunk(
'search/forgetResult',
(q: string, { dispatch, getState }) => {
const previous = getState().search.recent;
const me = getState().meta.get('me') as string;
const current = previous.filter((result) => result.q !== q);

searchHistory.set(me, current);
dispatch(updateSearchHistory(current));
},
);

export const updateSearchHistory = createAction<RecentSearch[]>(
'search/updateHistory',
);

export const hydrateSearch = createAppAsyncThunk(
'search/hydrate',
(_args, { dispatch, getState }) => {
const me = getState().meta.get('me') as string;
const history = searchHistory.get(me) as RecentSearch[] | null;

if (history !== null) {
dispatch(updateSearchHistory(history));
}
},
);
16 changes: 16 additions & 0 deletions app/javascript/flavours/glitch/api/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { apiRequestGet } from 'flavours/glitch/api';
import type {
ApiSearchType,
ApiSearchResultsJSON,
} from 'flavours/glitch/api_types/search';

export const apiGetSearch = (params: {
q: string;
resolve?: boolean;
type?: ApiSearchType;
limit?: number;
offset?: number;
}) =>
apiRequestGet<ApiSearchResultsJSON>('v2/search', {
...params,
});
11 changes: 11 additions & 0 deletions app/javascript/flavours/glitch/api_types/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { ApiAccountJSON } from './accounts';
import type { ApiStatusJSON } from './statuses';
import type { ApiHashtagJSON } from './tags';

export type ApiSearchType = 'accounts' | 'statuses' | 'hashtags';

export interface ApiSearchResultsJSON {
accounts: ApiAccountJSON[];
statuses: ApiStatusJSON[];
hashtags: ApiHashtagJSON[];
}
Loading

0 comments on commit 1bea796

Please sign in to comment.