mirror of
https://github.com/ershisan99/DevToysWeb.git
synced 2025-12-16 20:49:23 +00:00
renewal
recreate project by using https://github.com/shadcn/next-template App: - support dark mode - add toggle theme button - add clear search button - add search button - add current page indicator - add tool group pages - add settings tool - add 1 tab format option to Json format tool - add paste button to some tools - add file button to some tools - add copy button to some tools - add clear button to some tools - change favicon - change search hit rate - change each page title - change icons from Material Icons to Lucide - change sidebar scroll area - change editor from Ace to Monaco - change parsable separators of number base converter - change default value of format option of number base converter - change default values of some tool forms - change some styles - remove disabled tools - remove real-time search - fix uri encoding tool Dev: - MUI + Emotion -> Radix UI + Tailwind CSS - Next.js 12 Pages -> Next.js 13 App Router - React 17 -> React 18 - many other packages upgraded - use useState instead of recoil - use Next.js typedRoutes instead of pathpida - clean npm scripts - format import statements by Prettier - no component separations between container and presenter - effective component memoizations - add vscode settings - many refactors
This commit is contained in:
20
components/sidebar/all-tools.tsx
Normal file
20
components/sidebar/all-tools.tsx
Normal file
@@ -0,0 +1,20 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname } from "next/navigation";
|
||||
|
||||
import { singleTools } from "@/config/tools";
|
||||
|
||||
import { ToolLink } from "./tool-link";
|
||||
|
||||
export function AllTools() {
|
||||
const pathname = usePathname();
|
||||
|
||||
return (
|
||||
<ToolLink
|
||||
Icon={singleTools.allTools.Icon}
|
||||
shortTitle={singleTools.allTools.shortTitle}
|
||||
href={singleTools.allTools.href}
|
||||
highlight={pathname === singleTools.allTools.href ? "both" : "none"}
|
||||
/>
|
||||
);
|
||||
}
|
||||
64
components/sidebar/search-bar.tsx
Normal file
64
components/sidebar/search-bar.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo, useRef, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import { cn } from "@/lib/style";
|
||||
import { Button, ButtonProps } from "@/components/ui/button";
|
||||
import { Input, InputProps } from "@/components/ui/input";
|
||||
import { icons } from "@/components/icons";
|
||||
|
||||
export function SearchBar() {
|
||||
const router = useRouter();
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [text, setText] = useState("");
|
||||
|
||||
const search = () => {
|
||||
if (text.trim()) {
|
||||
router.push(`/search?q=${text.trim()}`);
|
||||
}
|
||||
};
|
||||
|
||||
const changeText: InputProps["onChange"] = ({ currentTarget }) => setText(currentTarget.value);
|
||||
|
||||
const searchIfEnter: InputProps["onKeyDown"] = ({ code }) => {
|
||||
if (code === "Enter") {
|
||||
search();
|
||||
}
|
||||
};
|
||||
|
||||
const clearText: ButtonProps["onClick"] = () => {
|
||||
setText("");
|
||||
inputRef.current?.focus();
|
||||
};
|
||||
|
||||
const clearIcon = useMemo(() => <icons.X className="p-1 text-muted-foreground" />, []);
|
||||
|
||||
const searchIcon = useMemo(
|
||||
() => <icons.Search className="-scale-x-100 p-1 text-muted-foreground" />,
|
||||
[]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="relative flex w-full items-center">
|
||||
<Input
|
||||
ref={inputRef}
|
||||
className="flex-1 pr-16 font-sans"
|
||||
value={text}
|
||||
onChange={changeText}
|
||||
onKeyDown={searchIfEnter}
|
||||
placeholder="Type to search for tools…"
|
||||
/>
|
||||
<div className="absolute right-1 flex gap-1">
|
||||
<Button className={cn("h-6 p-0", !text && "hidden")} variant="ghost" onClick={clearText}>
|
||||
{clearIcon}
|
||||
<span className="sr-only">Clear search text</span>
|
||||
</Button>
|
||||
<Button className="h-6 p-0" variant="ghost" onClick={search} aria-label="search">
|
||||
{searchIcon}
|
||||
<span className="sr-only">Search tools</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
20
components/sidebar/settings.tsx
Normal file
20
components/sidebar/settings.tsx
Normal file
@@ -0,0 +1,20 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname } from "next/navigation";
|
||||
|
||||
import { singleTools } from "@/config/tools";
|
||||
|
||||
import { ToolLink } from "./tool-link";
|
||||
|
||||
export function Settings() {
|
||||
const pathname = usePathname();
|
||||
|
||||
return (
|
||||
<ToolLink
|
||||
Icon={singleTools.settings.Icon}
|
||||
shortTitle={singleTools.settings.shortTitle}
|
||||
href={singleTools.settings.href}
|
||||
highlight={pathname === singleTools.settings.href ? "both" : "none"}
|
||||
/>
|
||||
);
|
||||
}
|
||||
69
components/sidebar/tool-group.tsx
Normal file
69
components/sidebar/tool-group.tsx
Normal file
@@ -0,0 +1,69 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useMemo, useRef } from "react";
|
||||
import { usePathname } from "next/navigation";
|
||||
import * as Accordion from "@radix-ui/react-accordion";
|
||||
|
||||
import { ToolGroup as IToolGroup } from "@/config/tools";
|
||||
import { icons } from "@/components/icons";
|
||||
|
||||
import { ToolLink } from "./tool-link";
|
||||
|
||||
type Props = IToolGroup & {
|
||||
isOpend: boolean;
|
||||
};
|
||||
|
||||
// FIXME: css outline messed up
|
||||
export function ToolGroup({ Icon, title, href, tools, isOpend }: Props) {
|
||||
const pathname = usePathname();
|
||||
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||
|
||||
const onClick = useCallback(() => triggerRef.current?.click(), []);
|
||||
|
||||
const chevronIcon = useMemo(
|
||||
() => <icons.ChevronDown className="h-4 w-4 transition-transform duration-200" />,
|
||||
[]
|
||||
);
|
||||
|
||||
return (
|
||||
<Accordion.AccordionItem value={href}>
|
||||
<Accordion.Header asChild>
|
||||
<div className="relative flex">
|
||||
<ToolLink
|
||||
className="flex-1"
|
||||
{...{ Icon, href, onClick }}
|
||||
shortTitle={title}
|
||||
highlight={
|
||||
pathname === href
|
||||
? "both"
|
||||
: !isOpend && pathname.startsWith(`${href}/`)
|
||||
? "indicatorOnly"
|
||||
: "none"
|
||||
}
|
||||
/>
|
||||
<Accordion.Trigger
|
||||
ref={triggerRef}
|
||||
className="absolute right-0 flex h-10 w-10 items-center justify-center rounded transition-all duration-0 hover:bg-accent [&[data-state=open]>svg]:rotate-180"
|
||||
aria-label="toggle open/close state of the tool group"
|
||||
>
|
||||
{chevronIcon}
|
||||
</Accordion.Trigger>
|
||||
</div>
|
||||
</Accordion.Header>
|
||||
<Accordion.AccordionContent className="overflow-hidden transition-all data-[state=closed]:animate-accordion-up data-[state=open]:animate-accordion-down">
|
||||
<ul>
|
||||
{Object.values(tools).map(tool => (
|
||||
<li className="mt-1" key={tool.href}>
|
||||
<ToolLink
|
||||
// -outline-offset-1: ugly hack for Chrome outlines
|
||||
className="pl-8 -outline-offset-1"
|
||||
{...tool}
|
||||
highlight={isOpend && pathname === tool.href ? "both" : "none"}
|
||||
/>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Accordion.AccordionContent>
|
||||
</Accordion.AccordionItem>
|
||||
);
|
||||
}
|
||||
38
components/sidebar/tool-groups.tsx
Normal file
38
components/sidebar/tool-groups.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { usePathname } from "next/navigation";
|
||||
import * as Accordion from "@radix-ui/react-accordion";
|
||||
|
||||
import { toolGroups } from "@/config/tools";
|
||||
|
||||
import { ToolGroup } from "./tool-group";
|
||||
|
||||
const isGroupedTool = (path: string) =>
|
||||
Object.values(toolGroups)
|
||||
.map(({ href }) => href as string)
|
||||
.some(group => path.startsWith(`${group}/`));
|
||||
|
||||
export function ToolGroups() {
|
||||
const pathname = usePathname();
|
||||
const [expandedGroups, setExpandedGroups] = useState<string[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isGroupedTool(pathname)) {
|
||||
const group = `/${pathname.split("/")[1]}`;
|
||||
setExpandedGroups(prev => Array.from(new Set([...prev, group])));
|
||||
}
|
||||
}, [pathname]);
|
||||
|
||||
return (
|
||||
<Accordion.Root type="multiple" value={expandedGroups} onValueChange={setExpandedGroups}>
|
||||
<ul className="space-y-1">
|
||||
{Object.values(toolGroups).map(group => (
|
||||
<li key={group.href}>
|
||||
<ToolGroup {...group} isOpend={expandedGroups.includes(group.href)} />
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Accordion.Root>
|
||||
);
|
||||
}
|
||||
38
components/sidebar/tool-link.tsx
Normal file
38
components/sidebar/tool-link.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
"use client";
|
||||
|
||||
import { memo, useMemo } from "react";
|
||||
import Link, { LinkProps } from "next/link";
|
||||
|
||||
import { Tool } from "@/config/tools";
|
||||
import { cn } from "@/lib/style";
|
||||
import { Indicator } from "@/components/indicator";
|
||||
|
||||
type Props = Pick<Tool, "Icon" | "shortTitle"> &
|
||||
Pick<LinkProps<unknown>, "className" | "href" | "onClick"> & {
|
||||
highlight: "both" | "indicatorOnly" | "none";
|
||||
};
|
||||
|
||||
function RawToolLink({ Icon, shortTitle: title, href, onClick, className, highlight }: Props) {
|
||||
const icon = useMemo(() => <Icon size={16} />, [Icon]);
|
||||
|
||||
return (
|
||||
<Link
|
||||
className={cn(
|
||||
"flex h-10 items-center gap-3 rounded hover:bg-accent",
|
||||
highlight === "both" && "bg-accent",
|
||||
className
|
||||
)}
|
||||
{...{ href, onClick }}
|
||||
>
|
||||
<span className={cn("invisible flex items-center", highlight !== "none" && "visible")}>
|
||||
<Indicator />
|
||||
</span>
|
||||
<span className="flex select-none items-center">
|
||||
{icon}
|
||||
<span className="ml-4">{title}</span>
|
||||
</span>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
|
||||
export const ToolLink = memo(RawToolLink);
|
||||
Reference in New Issue
Block a user