Skip to content

Commit

Permalink
Update directory page options to use URL params (mastodon#31977)
Browse files Browse the repository at this point in the history
  • Loading branch information
renchap authored Sep 19, 2024
1 parent 57a38f0 commit ae03e4f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
31 changes: 31 additions & 0 deletions app/javascript/hooks/useSearchParam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useMemo, useCallback } from 'react';

import { useLocation, useHistory } from 'react-router';

export function useSearchParams() {
const { search } = useLocation();

return useMemo(() => new URLSearchParams(search), [search]);
}

export function useSearchParam(name: string, defaultValue?: string) {
const searchParams = useSearchParams();
const history = useHistory();

const value = searchParams.get(name) ?? defaultValue;

const setValue = useCallback(
(value: string | null) => {
if (value === null) {
searchParams.delete(name);
} else {
searchParams.set(name, value);
}

history.push({ search: searchParams.toString() });
},
[history, name, searchParams],
);

return [value, setValue] as const;
}
35 changes: 20 additions & 15 deletions app/javascript/mastodon/features/directory/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChangeEventHandler } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef } from 'react';

import { defineMessages, useIntl } from 'react-intl';

Expand All @@ -23,6 +23,8 @@ import { RadioButton } from 'mastodon/components/radio_button';
import ScrollContainer from 'mastodon/containers/scroll_container';
import { useAppDispatch, useAppSelector } from 'mastodon/store';

import { useSearchParam } from '../../../hooks/useSearchParam';

import { AccountCard } from './components/account_card';

const messages = defineMessages({
Expand All @@ -47,18 +49,19 @@ export const Directory: React.FC<{
const intl = useIntl();
const dispatch = useAppDispatch();

const [state, setState] = useState<{
order: string | null;
local: boolean | null;
}>({
order: null,
local: null,
});

const column = useRef<Column>(null);

const order = state.order ?? params?.order ?? 'active';
const local = state.local ?? params?.local ?? false;
const [orderParam, setOrderParam] = useSearchParam('order');
const [localParam, setLocalParam] = useSearchParam('local');

let localParamBool: boolean | undefined;

if (localParam === 'false') {
localParamBool = false;
}

const order = orderParam ?? params?.order ?? 'active';
const local = localParamBool ?? params?.local ?? true;

const handlePin = useCallback(() => {
if (columnId) {
Expand Down Expand Up @@ -101,10 +104,10 @@ export const Directory: React.FC<{
if (columnId) {
dispatch(changeColumnParams(columnId, ['order'], e.target.value));
} else {
setState((s) => ({ order: e.target.value, local: s.local }));
setOrderParam(e.target.value);
}
},
[dispatch, columnId],
[dispatch, columnId, setOrderParam],
);

const handleChangeLocal = useCallback<ChangeEventHandler<HTMLInputElement>>(
Expand All @@ -113,11 +116,13 @@ export const Directory: React.FC<{
dispatch(
changeColumnParams(columnId, ['local'], e.target.value === '1'),
);
} else if (e.target.value === '1') {
setLocalParam('true');
} else {
setState((s) => ({ local: e.target.value === '1', order: s.order }));
setLocalParam('false');
}
},
[dispatch, columnId],
[dispatch, columnId, setLocalParam],
);

const handleLoadMore = useCallback(() => {
Expand Down

0 comments on commit ae03e4f

Please sign in to comment.