Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: teams filter improvements #14315

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/cs/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
"forgot": "Zapomněli jste?",
"done": "Hotovo",
"all_done": "Hotovo!",
"all": "Všechny aplikace",
"all": "Všechny",
"yours": "Váš účet",
"available_apps": "Dostupné aplikace",
"available_apps_lower_case": "Dostupné aplikace",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
"forgot": "Vergessen?",
"done": "Erledigt",
"all_done": "Erledigt!",
"all": "Alle Apps",
"all": "Alle",
"yours": "Ihr Konto",
"available_apps": "Verfügbare Apps",
"available_apps_lower_case": "Verfügbare Apps",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/es/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
"forgot": "¿Olvidado?",
"done": "Hecho",
"all_done": "¡Todo listo!",
"all": "Todas las aplicaciones",
"all": "Todos",
"yours": "Su cuenta",
"available_apps": "Aplicaciones disponibles",
"available_apps_lower_case": "Aplicaciones disponibles",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/it/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
"forgot": "Dimenticato?",
"done": "Fatto",
"all_done": "Operazione completata!",
"all": "Tutte le app",
"all": "Tutte",
"yours": "Il tuo account",
"available_apps": "Applicazioni disponibili",
"available_apps_lower_case": "Applicazioni disponibili",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/nl/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
"forgot": "Vergeten?",
"done": "Voltooid",
"all_done": "Helemaal klaar!",
"all": "Alle apps",
"all": "Alle",
"yours": "Uw account",
"available_apps": "Beschikbare apps",
"available_apps_lower_case": "Beschikbare apps",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/pt-BR/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@
"forgot": "Esqueceu?",
"done": "Concluído",
"all_done": "Tudo pronto!",
"all": "Todos os apps",
"all": "Todos",
"yours": "Sua conta",
"available_apps": "Aplicativos disponíveis",
"available_apps_lower_case": "Aplicativos disponíveis",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/pt/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
"forgot": "Esqueceu-se?",
"done": "Concluído",
"all_done": "Tudo pronto!",
"all": "Todos as aplicações",
"all": "Todos",
"yours": "A sua conta",
"available_apps": "Aplicações disponíveis",
"available_apps_lower_case": "Aplicações disponíveis",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/ro/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
"forgot": "Ai uitat?",
"done": "Adoptat",
"all_done": "Gata!",
"all": "Toate aplicațiile",
"all": "Toate",
"yours": "Contul dvs.",
"available_apps": "Aplicații disponibile",
"available_apps_lower_case": "Aplicații disponibile",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/ru/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
"forgot": "Забыли пароль?",
"done": "Готово",
"all_done": "Готово!",
"all": "Все приложения",
"all": "Все",
"yours": "Ваш аккаунт",
"available_apps": "Доступные приложения",
"available_apps_lower_case": "Доступные приложения",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/sr/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
"forgot": "Zaboravili ste?",
"done": "Urađeno",
"all_done": "Završeno!",
"all": "Sve aplikacije",
"all": "Sve",
"yours": "Vaš nalog",
"available_apps": "Dostupne aplikacije",
"available_apps_lower_case": "Dostupne aplikacije",
Expand Down
6 changes: 4 additions & 2 deletions packages/features/filters/components/TeamsFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import { filterQuerySchema } from "../lib/getTeamsFiltersFromQuery";
export type IEventTypesFilters = RouterOutputs["viewer"]["eventTypes"]["listWithTeam"];
export type IEventTypeFilter = IEventTypesFilters[0];

export const TEAMS_FILTER_KEY = "TEAMS_FILTER_KEY";
function useFilterQuery() {
// passthrough allows additional params to not be removed
return useTypedQuery(filterQuerySchema.passthrough(), TEAMS_FILTER_KEY);
return useTypedQuery(filterQuerySchema.passthrough());
}

export const TeamsFilter = ({
Expand All @@ -33,11 +32,14 @@ export const TeamsFilter = ({
}) => {
const { t } = useLocale();
const session = useSession();

const { data: query, pushItemToKey, removeItemByKeyAndValue, removeAllQueryParams } = useFilterQuery();

const { data: teams } = trpc.viewer.teams.list.useQuery(undefined, {
// Teams don't change that frequently
refetchOnWindowFocus: false,
});

const getCheckedOptionsNames = () => {
const checkedOptions: string[] = [];
const teamIds = query.teamIds;
Expand Down
45 changes: 1 addition & 44 deletions packages/lib/hooks/useTypedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { usePathname, useRouter } from "next/navigation";
import { useCallback, useMemo, useEffect } from "react";
import { z } from "zod";

import { localStorage } from "../webstorage";
import { useRouterQuery } from "./useRouterQuery";

type OptionalKeys<T> = {
Expand Down Expand Up @@ -33,7 +32,7 @@ export const queryStringArray = z
.preprocess((a) => z.string().parse(a).split(","), z.string().array())
.or(z.string().array());

export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageKey: string | null = null) {
export function useTypedQuery<T extends z.AnyZodObject>(schema: T) {
type Output = z.infer<typeof schema>;
type FullOutput = Required<Output>;
type OutputKeys = Required<keyof FullOutput>;
Expand All @@ -44,7 +43,6 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
const unparsedQuery = useRouterQuery();
const pathname = usePathname();
const parsedQuerySchema = schema.safeParse(unparsedQuery);
const useLocalStorage = !!localStorageKey;
let parsedQuery: Output = useMemo(() => {
return {} as Output;
}, []);
Expand Down Expand Up @@ -74,39 +72,6 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
// eslint-disable-next-line react-hooks/exhaustive-deps
[parsedQuery, router]
);
//If the value for {localStorageKey} exist in localStorage set it to queryParams
useEffect(() => {
if (!useLocalStorage) return;
const storedValueString = localStorage.getItem(localStorageKey);
if (!storedValueString || storedValueString == "{}") return;
const storedValue: Output = JSON.parse(storedValueString);
if (storedValue) {
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(storedValue)) {
searchParams.set(String(key), String(value));
}
router.replace(`${pathname}?${searchParams.toString()}`);
}
}, []);

function setLocalStorage<J extends OutputKeys>(key: J, value: Output[J]) {
if (!useLocalStorage) return;
const storedValue: Output = JSON.parse(localStorage.getItem(localStorageKey) || "{}");
const newValue = { ...storedValue, [key]: value };
localStorage.setItem(localStorageKey, JSON.stringify(newValue));
}
function removeByKeyFromLocalStorage(key: OutputOptionalKeys) {
if (!useLocalStorage) return;
const storedValue: Output = JSON.parse(localStorage.getItem(localStorageKey) || "{}");
const newValue = storedValue;
delete newValue[key];
localStorage.setItem(localStorageKey, JSON.stringify(newValue));
}
//Remove {localStorageKey} from the localStorage
function removeAllValuesFromLocalStorage() {
if (!useLocalStorage) return;
localStorage.removeItem(localStorageKey);
}

// Delete a key from the query
function removeByKey(key: OutputOptionalKeys) {
Expand All @@ -122,13 +87,9 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
if (existingValue.includes(value)) return; // prevent adding the same value to the array
// @ts-expect-error this is too much for TS it seems
setQuery(key, [...existingValue, value]);
// @ts-expect-error this is too much for TS it seems
setLocalStorage(key, [...existingValue, value]);
} else {
// @ts-expect-error this is too much for TS it seems
setQuery(key, [value]);
// @ts-expect-error this is too much for TS it seems
setLocalStorage(key, [value]);
}
}

Expand All @@ -139,20 +100,16 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
// @ts-expect-error this is too much for TS it seems
const newValue = existingValue.filter((item) => item !== value);
setQuery(key, newValue);
setLocalStorage(key, newValue);
} else {
// @ts-expect-error this is too much for TS it seems
removeByKey(key);
//@ts-expect-error this is too much for TS it seems
removeByKeyFromLocalStorage(key);
}
}

// Remove all query params from the URL
function removeAllQueryParams() {
if (pathname !== null) {
router.replace(pathname);
removeAllValuesFromLocalStorage();
}
}

Expand Down
Loading