Skip to content

Commit

Permalink
feat: dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
whes1015 committed Dec 4, 2024
1 parent 9cf51f4 commit bc0559d
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 31 deletions.
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@fontsource/noto-sans-tc": "^5.1.0",
"@icons-pack/react-simple-icons": "^10.2.0",
"@radix-ui/react-avatar": "^1.1.1",
"@radix-ui/react-dialog": "^1.1.2",
"@radix-ui/react-dropdown-menu": "^2.1.2",
"@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-slot": "^1.1.0",
Expand Down
99 changes: 69 additions & 30 deletions src/components/install.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use client';

import { useState, useEffect } from 'react';
import { useEffect, useState } from 'react';
import { AlertCircle, Download, X } from 'lucide-react';

import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';

import type { Plugin } from '@/modal/plugin';
Expand All @@ -10,21 +19,27 @@ interface InstallButtonsProps {
plugin: Plugin;
}

interface NavigatorWithProtocols extends Navigator {
msProtocolsHandler?: boolean;
}

export function InstallButtons({ plugin }: InstallButtonsProps) {
const [mounted, setMounted] = useState(false);
const [isAppInstalled, setIsAppInstalled] = useState(false);
const [showDialog, setShowDialog] = useState(false);

useEffect(() => {
setMounted(true);

const checkAppInstalled = () => {
try {
if ((navigator as any).msProtocolsHandler) {
const nav = navigator as NavigatorWithProtocols;
if (nav.msProtocolsHandler) {
setIsAppInstalled(true);
return;
}

if ('registerProtocolHandler' in navigator) {
if ('registerProtocolHandler' in nav) {
setIsAppInstalled(true);
return;
}
Expand All @@ -45,41 +60,65 @@ export function InstallButtons({ plugin }: InstallButtonsProps) {
const tremUrl = `trem-lite://plugin/install:${plugin.name}@${downloadUrl}`;

const handleTremOpen = () => {
setShowDialog(true);
};

const handleConfirm = () => {
window.location.href = tremUrl;
setShowDialog(false);
};

return (
<div className="space-y-2">
{mounted && isAppInstalled
? (
<>
<Button
className="w-full"
onClick={handleTremOpen}
>
下載至 TREM-Lite
</Button>
<Button
className="w-full"
variant="outline"
asChild
>
<>
<div className="space-y-2">
{mounted && isAppInstalled
? (
<>
<Button className="w-full" onClick={handleTremOpen}>
<Download className="mr-2 h-4 w-4" />
下載至 TREM-Lite
</Button>
<Button className="w-full" variant="outline" asChild>
<a href={downloadUrl} download>
<Download className="mr-2 h-4 w-4" />
下載最新版本
</a>
</Button>
</>
)
: (
<Button className="w-full" asChild>
<a href={downloadUrl} download>
<Download className="mr-2 h-4 w-4" />
下載最新版本
</a>
</Button>
</>
)
: (
<Button
className="w-full"
asChild
>
<a href={downloadUrl} download>
下載最新版本
</a>
)}
</div>

<Dialog open={showDialog} onOpenChange={setShowDialog}>
<DialogContent>
<DialogHeader>
<div className="flex items-center">
<AlertCircle className="mr-2 h-5 w-5 text-yellow-500" />
<DialogTitle>安裝擴充前請確認</DialogTitle>
</div>
<DialogDescription>
請確保 TREM Lite 已經啟動才能安裝擴充。是否繼續安裝?
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onClick={() => setShowDialog(false)}>
<X className="mr-2 h-4 w-4" />
取消
</Button>
<Button onClick={handleConfirm}>
<Download className="mr-2 h-4 w-4" />
確認安裝
</Button>
)}
</div>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}
2 changes: 1 addition & 1 deletion src/components/plugin_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default function PluginList({ plugins: initialPlugins }: { plugins: Plugi
</div>
</div>

{/* 插件列表 */}
{/* 擴充列表 */}
<GlowCapture>
<div className={`
grid grid-cols-1 gap-4
Expand Down
189 changes: 189 additions & 0 deletions src/components/ui/dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
'use client';

import * as React from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import { X } from 'lucide-react';

import { cn } from '@/lib/utils';

import type { ClassValue } from 'clsx';

const Dialog = DialogPrimitive.Root;
const DialogTrigger = DialogPrimitive.Trigger;
const DialogClose = DialogPrimitive.Close;

interface DialogPortalProps extends DialogPrimitive.DialogPortalProps {
className?: string;
children?: React.ReactNode;
}

const DialogPortal: React.FC<DialogPortalProps> = ({ className, ...props }) => (
<DialogPrimitive.Portal className={cn(className)} {...props} />
);
DialogPortal.displayName = DialogPrimitive.Portal.displayName;

interface DialogOverlayProps extends DialogPrimitive.DialogOverlayProps {
className?: string;
}

const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
DialogOverlayProps
>(({ className, ...props }, ref) => (
<DialogPrimitive.Overlay
ref={ref}
className={cn(
`
fixed inset-0 z-50 bg-black/80
data-[state=closed]:animate-out data-[state=closed]:fade-out-0
data-[state=open]:animate-in data-[state=open]:fade-in-0
`,
className,
)}
{...props}
/>
));
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;

interface DialogContentProps extends DialogPrimitive.DialogContentProps {
className?: string;
children?: React.ReactNode;
}

const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
DialogContentProps
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
`
fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg
translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6
shadow-lg duration-200
data-[state=closed]:animate-out data-[state=closed]:fade-out-0
data-[state=closed]:zoom-out-95
data-[state=closed]:slide-out-to-left-1/2
data-[state=closed]:slide-out-to-top-[48%]
data-[state=open]:animate-in data-[state=open]:fade-in-0
data-[state=open]:zoom-in-95 data-[state=open]:slide-in-from-left-1/2
data-[state=open]:slide-in-from-top-[48%]
sm:rounded-lg
`,
className,
)}
{...props}
>
{children}
<DialogPrimitive.Close
className={cn(
`
absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background
transition-opacity
data-[state=open]:bg-accent data-[state=open]:text-muted-foreground
disabled:pointer-events-none
focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2
hover:opacity-100
`,
)}
>
<X className="h-4 w-4" />
<span className="sr-only">關閉</span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
));
DialogContent.displayName = DialogPrimitive.Content.displayName;

interface DialogHeaderProps extends React.HTMLAttributes<HTMLDivElement> {
className?: string;
}

const DialogHeader = React.forwardRef<HTMLDivElement, DialogHeaderProps>(
({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
`
flex flex-col space-y-1.5 text-center
sm:text-left
`,
className,
)}
{...props}
/>
),
);
DialogHeader.displayName = 'DialogHeader';

interface DialogFooterProps extends React.HTMLAttributes<HTMLDivElement> {
className?: string;
}

const DialogFooter = React.forwardRef<HTMLDivElement, DialogFooterProps>(
({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
`
flex flex-col-reverse
sm:flex-row sm:justify-end sm:space-x-2
`,
className,
)}
{...props}
/>
),
);
DialogFooter.displayName = 'DialogFooter';

interface DialogTitleProps extends DialogPrimitive.DialogTitleProps {
className?: string;
}

const DialogTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
DialogTitleProps
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
'text-lg font-semibold leading-none tracking-tight',
className,
)}
{...props}
/>
));
DialogTitle.displayName = DialogPrimitive.Title.displayName;

interface DialogDescriptionProps
extends DialogPrimitive.DialogDescriptionProps {
className?: string;
}

const DialogDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
DialogDescriptionProps
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn('text-sm text-muted-foreground', className)}
{...props}
/>
));
DialogDescription.displayName = DialogPrimitive.Description.displayName;

export {
Dialog,
DialogPortal,
DialogOverlay,
DialogClose,
DialogTrigger,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
};

0 comments on commit bc0559d

Please sign in to comment.