Skip to content

Commit

Permalink
Advanced search: save command when going back
Browse files Browse the repository at this point in the history
  • Loading branch information
moysa committed Oct 11, 2024
1 parent 6df1d1c commit 80352c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/contexts/AdvancedSearchContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type AdvancedSearchContextStore = {
filteringReasons: string[],
errors: string[],
paging: PaginationInfo,
searchCommand: string,
actions: {
findUsers: (query: string, pubkey?: string) => void,
findUserByNupub: (npub: string) => void,
Expand All @@ -68,6 +69,7 @@ export type AdvancedSearchContextStore = {
getRecomendedUsers: (profiles?: PrimalUser[]) => void,
findFilteredUserByNpub: (npub: string) => void,
clearSearch: () => void,
setSearchCommand: (command: string) => void,
},
}

Expand All @@ -85,6 +87,7 @@ const initialData = {
mentionedNotes: {},
filteringReasons: [],
errors: [],
searchCommand: '',
paging: { ...emptyPaging() },
};

Expand Down Expand Up @@ -390,6 +393,8 @@ export function AdvancedSearchProvider(props: { children: JSX.Element }) {
}
}

const setSearchCommand = (command: string) => updateStore('searchCommand', () => command);

// STORES ---------------------------------------

const [store, updateStore] = createStore<AdvancedSearchContextStore>({
Expand All @@ -404,6 +409,7 @@ const [store, updateStore] = createStore<AdvancedSearchContextStore>({
getRecomendedUsers,
findFilteredUserByNpub,
clearSearch,
setSearchCommand,
},
});

Expand Down
16 changes: 14 additions & 2 deletions src/pages/AdvancedSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TextField } from '@kobalte/core/text-field';
import { Slider } from "@kobalte/core/slider";
import { Dialog } from '@kobalte/core/dialog';

import { A, useNavigate } from '@solidjs/router';
import { A, useLocation, useNavigate } from '@solidjs/router';
import { batch, Component, createEffect, For, onMount, Show } from 'solid-js';
import { createStore } from 'solid-js/store';
import Avatar from '../components/Avatar/Avatar';
Expand All @@ -29,6 +29,7 @@ import ButtonSecondary from '../components/Buttons/ButtonSecondary';
import ButtonLink from '../components/Buttons/ButtonLink';
import { wordsPerMinute } from '../constants';
import { useSearchContext } from '../contexts/SearchContext';
import { useAdvancedSearchContext } from '../contexts/AdvancedSearchContext';

export type SearchState = {
includes: string,
Expand Down Expand Up @@ -142,7 +143,7 @@ const scopes: Record<string, () => string> = {
};

const kinds: Record<string, () => string> = {
'Notes': () => 'kind:1',
'Notes': () => '',
'Reads': () => 'kind:30023',
'Note Replies': () => 'kind:1 filter:replies',
'Reads Comments': () => 'kind:30023 filter:replies',
Expand All @@ -166,6 +167,8 @@ const sortings: Record<string, () => string> = {
const AdvancedSearch: Component = () => {
const navigate = useNavigate();
const search = useSearchContext();
const advSearch = useAdvancedSearchContext();
const location = useLocation();

const [state, setState] = createStore<SearchState>({
includes: '',
Expand Down Expand Up @@ -213,6 +216,15 @@ const AdvancedSearch: Component = () => {
e.preventDefault();
}

createEffect(() => {
if (state.command.length === 0) {
setState('command', () => advSearch?.searchCommand || '');
return;
}

advSearch?.actions.setSearchCommand(state.command);
})

createEffect(() => {
if (state.timeframe !== 'Custom') {
setState('customTimeframe', () => ({ since: '', until: '' }));
Expand Down
11 changes: 9 additions & 2 deletions src/pages/AdvancedSearchResults.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useNavigate, useParams } from '@solidjs/router';
import { Component, createSignal, For, Match, onMount, Show, Switch } from 'solid-js';
import { Component, createEffect, createSignal, For, Match, on, onMount, Show, Switch } from 'solid-js';
import Loader from '../components/Loader/Loader';
import Note from '../components/Note/Note';
import PageTitle from '../components/PageTitle/PageTitle';
Expand Down Expand Up @@ -36,6 +36,13 @@ const AdvancedSearchResults: Component = () => {
search?.actions.findContent(queryString());
});


createEffect(on(queryString, (v, p) => {
if (!v || v === p) return;

search?.actions.setSearchCommand(v);
}))

const feedType = () => [Kind.LongForm].includes(kind()) ?
'reads' : 'home';

Expand All @@ -57,7 +64,7 @@ const AdvancedSearchResults: Component = () => {
/>

<ButtonLink
onClick={() => navigate('/asearch')}
onClick={() => navigate('/asearch', { state: { query: queryString() } })}
>
Back to Advanced search
</ButtonLink>
Expand Down

0 comments on commit 80352c6

Please sign in to comment.