Skip to content

Commit

Permalink
chore: rename show-all to include-from-all-users
Browse files Browse the repository at this point in the history
  • Loading branch information
Meierschlumpf committed Oct 20, 2024
1 parent 57f4e23 commit 6cc7384
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
16 changes: 13 additions & 3 deletions apps/nextjs/src/app/[locale]/manage/medias/_actions/show-all.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@ import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { Switch } from "@mantine/core";
import type { SwitchProps } from "@mantine/core";

import { useI18n } from "@homarr/translation/client";

type ShowAllSwitchProps = Pick<SwitchProps, "defaultChecked">;

export const ShowAllSwitch = ({ defaultChecked }: ShowAllSwitchProps) => {
export const IncludeFromAllUsersSwitch = ({ defaultChecked }: ShowAllSwitchProps) => {
const router = useRouter();
const pathName = usePathname();
const searchParams = useSearchParams();
const [checked, setChecked] = useState(defaultChecked);
const t = useI18n();

const onChange = (event: ChangeEvent<HTMLInputElement>) => {
setChecked(event.target.checked);
const params = new URLSearchParams(searchParams);
params.set("showAll", event.target.checked.toString());
params.set("includeFromAllUsers", event.target.checked.toString());
if (params.has("page")) params.set("page", "1"); // Reset page to 1
router.replace(`${pathName}?${params.toString()}`);
};

return <Switch defaultChecked={defaultChecked} checked={checked} label="Show all" onChange={onChange} />;
return (
<Switch
defaultChecked={defaultChecked}
checked={checked}
label={t("management.page.media.includeFromAllUsers")}
onChange={onChange}
/>
);
};
6 changes: 3 additions & 3 deletions apps/nextjs/src/app/[locale]/manage/medias/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { ManageContainer } from "~/components/manage/manage-container";
import { DynamicBreadcrumb } from "~/components/navigation/dynamic-breadcrumb";
import { CopyMedia } from "./_actions/copy-media";
import { DeleteMedia } from "./_actions/delete-media";
import { ShowAllSwitch } from "./_actions/show-all";
import { IncludeFromAllUsersSwitch } from "./_actions/show-all";
import { UploadMedia } from "./_actions/upload-media";

const searchParamsSchema = z.object({
search: z.string().optional(),
showAll: z
includeFromAllUsers: z
.string()
.regex(/true|false/)
.catch("false")
Expand Down Expand Up @@ -57,7 +57,7 @@ export default async function GroupsListPage(props: MediaListPageProps) {
<Group justify="space-between">
<Group>
<SearchInput placeholder={`${t("media.search")}...`} defaultValue={searchParams.search} />
{isAdmin && <ShowAllSwitch defaultChecked={searchParams.showAll} />}
{isAdmin && <IncludeFromAllUsersSwitch defaultChecked={searchParams.includeFromAllUsers} />}
</Group>

<UploadMedia />
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/router/medias/media-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export const mediaRouter = createTRPCRouter({
getPaginated: protectedProcedure
.input(
validation.common.paginated.and(
z.object({ showAll: z.boolean().default(false), search: z.string().trim().default("") }),
z.object({ includeFromAllUsers: z.boolean().default(false), search: z.string().trim().default("") }),
),
)
.query(async ({ ctx, input }) => {
const includeAll = ctx.session.user.permissions.includes("admin") && input.showAll;
const includeFromAllUsers = ctx.session.user.permissions.includes("admin") && input.includeFromAllUsers;

const where = and(
input.search.length >= 1 ? like(medias.name, `%${input.search}%`) : undefined,
includeAll ? undefined : eq(medias.creatorId, ctx.session.user.id),
includeFromAllUsers ? undefined : eq(medias.creatorId, ctx.session.user.id),
);
const dbMedias = await ctx.db.query.medias.findMany({
where,
Expand Down
3 changes: 3 additions & 0 deletions packages/translation/src/lang/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,9 @@ export default {
},
},
},
media: {
includeFromAllUsers: "Include media from all users",
},
user: {
back: "Back to users",
fieldsDisabledExternalProvider:
Expand Down

0 comments on commit 6cc7384

Please sign in to comment.