mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add swipe-back gesture navigation for mobile
This commit is contained in:
1 parent
8aabb79328
commit
08e3414677
7 files changed
+234
-12
No files matched your search
@@ -1,9 +1,10 @@
|
||||
import { useEffect, useState, memo, useCallback } from 'react'
|
||||
import { useEffect, useState, memo, useCallback, useRef } from 'react'
|
||||
import { FileBrowser } from './FileBrowser'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { PathDisplay } from '@/components/ui/path-display'
|
||||
import { X } from 'lucide-react'
|
||||
import { GPU_ACCELERATED_STYLE, MODAL_TRANSITION_MS } from '@/lib/utils'
|
||||
import { useSwipeBack } from '@/hooks/useMobile'
|
||||
|
||||
interface FileBrowserSheetProps {
|
||||
isOpen: boolean
|
||||
@@ -18,6 +19,15 @@ export const FileBrowserSheet = memo(function FileBrowserSheet({ isOpen, onClose
|
||||
const [isEditing, setIsEditing] = useState(false)
|
||||
const [displayPath, setDisplayPath] = useState<string>('/')
|
||||
const [shouldRender, setShouldRender] = useState(false)
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const { bind, swipeStyles } = useSwipeBack(onClose, {
|
||||
enabled: isOpen && !isEditing,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
return bind(containerRef.current)
|
||||
}, [bind])
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
@@ -76,6 +86,7 @@ export const FileBrowserSheet = memo(function FileBrowserSheet({ isOpen, onClose
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
className="fixed inset-0 z-50"
|
||||
style={{
|
||||
opacity: isOpen ? 1 : 0,
|
||||
@@ -85,7 +96,7 @@ export const FileBrowserSheet = memo(function FileBrowserSheet({ isOpen, onClose
|
||||
>
|
||||
<div
|
||||
className="absolute inset-0 bg-background flex flex-col"
|
||||
style={GPU_ACCELERATED_STYLE}
|
||||
style={{ ...GPU_ACCELERATED_STYLE, ...swipeStyles }}
|
||||
>
|
||||
<div className="flex-shrink-0 border-b border-border bg-background backdrop-blur-sm px-4 py-1">
|
||||
<div className="flex items-center justify-between">
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
import { Dialog, DialogContent } from '@/components/ui/dialog'
|
||||
import { GitChangesPanel } from './GitChangesPanel'
|
||||
import { FileDiffView } from './FileDiffView'
|
||||
import { FilePreviewDialog } from './FilePreviewDialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { X, GitBranch } from 'lucide-react'
|
||||
import { useMobile } from '@/hooks/useMobile'
|
||||
import { useMobile, useSwipeBack } from '@/hooks/useMobile'
|
||||
import { useQueryClient } from '@tanstack/react-query'
|
||||
|
||||
interface GitChangesSheetProps {
|
||||
@@ -22,6 +22,16 @@ export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch, repoLo
|
||||
const [previewLineNumber, setPreviewLineNumber] = useState<number | undefined>()
|
||||
const isMobile = useMobile()
|
||||
const queryClient = useQueryClient()
|
||||
const contentRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const { bind: bindSwipe, swipeStyles } = useSwipeBack(
|
||||
selectedFile ? () => setSelectedFile(undefined) : onClose,
|
||||
{ enabled: isOpen && !previewFilePath }
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
return bindSwipe(contentRef.current)
|
||||
}, [bindSwipe])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOpen) {
|
||||
@@ -62,8 +72,10 @@ export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch, repoLo
|
||||
return (
|
||||
<Dialog open={isOpen} onOpenChange={handleOpenChange}>
|
||||
<DialogContent
|
||||
ref={contentRef}
|
||||
className="w-screen h-screen max-w-none max-h-none p-0 gap-0 bg-background border-0 flex flex-col"
|
||||
hideCloseButton
|
||||
style={swipeStyles}
|
||||
>
|
||||
<div className="flex items-center justify-between px-4 sm:py-3 border-b border-border flex-shrink-0">
|
||||
<div className="flex items-center gap-2">
|
||||
|
||||
@@ -2,6 +2,7 @@ import { memo, useCallback, useState, useEffect, useRef } from "react";
|
||||
import { FilePreview } from "./FilePreview";
|
||||
import type { FileInfo } from "@/types/files";
|
||||
import { GPU_ACCELERATED_STYLE, MODAL_TRANSITION_MS } from "@/lib/utils";
|
||||
import { useSwipeBack } from "@/hooks/useMobile";
|
||||
|
||||
interface MobileFilePreviewModalProps {
|
||||
isOpen: boolean;
|
||||
@@ -18,6 +19,15 @@ export const MobileFilePreviewModal = memo(function MobileFilePreviewModal({
|
||||
}: MobileFilePreviewModalProps) {
|
||||
const [localFile, setLocalFile] = useState<FileInfo | null>(null);
|
||||
const isClosingRef = useRef(false);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const { bind, swipeStyles } = useSwipeBack(onClose, {
|
||||
enabled: isOpen,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
return bind(containerRef.current);
|
||||
}, [bind]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen && file && !file.isDirectory) {
|
||||
@@ -42,8 +52,9 @@ export const MobileFilePreviewModal = memo(function MobileFilePreviewModal({
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
className="fixed inset-0 z-50 bg-background"
|
||||
style={{ isolation: 'isolate', ...GPU_ACCELERATED_STYLE }}
|
||||
style={{ isolation: 'isolate', ...GPU_ACCELERATED_STYLE, ...swipeStyles }}
|
||||
>
|
||||
<div
|
||||
className={`h-full overflow-hidden bg-background ${showFilePreviewHeader ? "" : "pb-8"}`}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import { useState, useRef, useEffect, useCallback } from 'react'
|
||||
import { GeneralSettings } from '@/components/settings/GeneralSettings'
|
||||
import { KeyboardShortcuts } from '@/components/settings/KeyboardShortcuts'
|
||||
import { OpenCodeConfigManager } from '@/components/settings/OpenCodeConfigManager'
|
||||
@@ -7,6 +7,7 @@ import { Dialog, DialogContent } from '@/components/ui/dialog'
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||
import { Settings2, Keyboard, Code, ChevronLeft, X, Key } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { useSwipeBack } from '@/hooks/useMobile'
|
||||
|
||||
interface SettingsDialogProps {
|
||||
open: boolean
|
||||
@@ -17,6 +18,24 @@ type SettingsView = 'menu' | 'general' | 'shortcuts' | 'opencode' | 'providers'
|
||||
|
||||
export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
|
||||
const [mobileView, setMobileView] = useState<SettingsView>('menu')
|
||||
const contentRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleSwipeBack = useCallback(() => {
|
||||
if (mobileView === 'menu') {
|
||||
setMobileView('menu')
|
||||
onOpenChange(false)
|
||||
} else {
|
||||
setMobileView('menu')
|
||||
}
|
||||
}, [mobileView, onOpenChange])
|
||||
|
||||
const { bind: bindSwipe, swipeStyles } = useSwipeBack(handleSwipeBack, {
|
||||
enabled: open,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
return bindSwipe(contentRef.current)
|
||||
}, [bindSwipe])
|
||||
|
||||
const menuItems = [
|
||||
{ id: 'general', icon: Settings2, label: 'General Settings', description: 'App preferences and behavior' },
|
||||
@@ -32,7 +51,11 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleClose}>
|
||||
<DialogContent className="max-w-4xl w-full h-[100vh] sm:h-auto sm:w-[95vw] sm:max-h-[90vh] bg-gradient-to-br from-background via-background to-background border-border p-0 sm:rounded-lg overflow-hidden !flex !flex-col left-0 top-0 translate-x-0 translate-y-0 max-h-[100vh] sm:left-[50%] sm:top-[50%] sm:translate-x-[-50%] sm:translate-y-[-50%] [&>button:last-child]:hidden">
|
||||
<DialogContent
|
||||
ref={contentRef}
|
||||
className="max-w-4xl w-full h-[100vh] sm:h-auto sm:w-[95vw] sm:max-h-[90vh] bg-gradient-to-br from-background via-background to-background border-border p-0 sm:rounded-lg overflow-hidden !flex !flex-col left-0 top-0 translate-x-0 translate-y-0 max-h-[100vh] sm:left-[50%] sm:top-[50%] sm:translate-x-[-50%] sm:translate-y-[-50%] [&>button:last-child]:hidden"
|
||||
style={swipeStyles}
|
||||
>
|
||||
|
||||
<div className="hidden sm:flex sm:flex-col sm:h-[90vh]">
|
||||
<div className="sticky top-0 z-10 bg-gradient-to-b from-background via-background to-transparent border-b border-border backdrop-blur-sm px-6 py-4 flex-shrink-0">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||
|
||||
export function useMobile() {
|
||||
const [isMobile, setIsMobile] = useState(false)
|
||||
@@ -15,4 +15,134 @@ export function useMobile() {
|
||||
}, [])
|
||||
|
||||
return isMobile
|
||||
}
|
||||
|
||||
interface SwipeBackOptions {
|
||||
threshold?: number
|
||||
edgeWidth?: number
|
||||
enabled?: boolean
|
||||
onSwipeStart?: () => void
|
||||
onSwipeEnd?: () => void
|
||||
}
|
||||
|
||||
interface SwipeState {
|
||||
startX: number
|
||||
startY: number
|
||||
currentX: number
|
||||
isSwiping: boolean
|
||||
isEdgeSwipe: boolean
|
||||
}
|
||||
|
||||
export function useSwipeBack(
|
||||
onBack: () => void,
|
||||
options: SwipeBackOptions = {}
|
||||
) {
|
||||
const {
|
||||
threshold = 80,
|
||||
edgeWidth = 30,
|
||||
enabled = true,
|
||||
onSwipeStart,
|
||||
onSwipeEnd,
|
||||
} = options
|
||||
|
||||
const isMobile = useMobile()
|
||||
const swipeRef = useRef<SwipeState>({
|
||||
startX: 0,
|
||||
startY: 0,
|
||||
currentX: 0,
|
||||
isSwiping: false,
|
||||
isEdgeSwipe: false,
|
||||
})
|
||||
const [swipeProgress, setSwipeProgress] = useState(0)
|
||||
|
||||
const handleTouchStart = useCallback((e: TouchEvent) => {
|
||||
if (!enabled || !isMobile) return
|
||||
|
||||
const touch = e.touches[0]
|
||||
const isEdge = touch.clientX <= edgeWidth
|
||||
|
||||
swipeRef.current = {
|
||||
startX: touch.clientX,
|
||||
startY: touch.clientY,
|
||||
currentX: touch.clientX,
|
||||
isSwiping: false,
|
||||
isEdgeSwipe: isEdge,
|
||||
}
|
||||
}, [enabled, isMobile, edgeWidth])
|
||||
|
||||
const handleTouchMove = useCallback((e: TouchEvent) => {
|
||||
if (!enabled || !isMobile) return
|
||||
|
||||
const state = swipeRef.current
|
||||
if (!state.isEdgeSwipe) return
|
||||
|
||||
const touch = e.touches[0]
|
||||
const deltaX = touch.clientX - state.startX
|
||||
const deltaY = Math.abs(touch.clientY - state.startY)
|
||||
|
||||
if (!state.isSwiping && deltaX > 10 && deltaX > deltaY * 2) {
|
||||
state.isSwiping = true
|
||||
onSwipeStart?.()
|
||||
}
|
||||
|
||||
if (state.isSwiping) {
|
||||
e.preventDefault()
|
||||
state.currentX = touch.clientX
|
||||
const progress = Math.min(Math.max(deltaX / threshold, 0), 1.5)
|
||||
setSwipeProgress(progress)
|
||||
}
|
||||
}, [enabled, isMobile, threshold, onSwipeStart])
|
||||
|
||||
const handleTouchEnd = useCallback(() => {
|
||||
if (!enabled || !isMobile) return
|
||||
|
||||
const state = swipeRef.current
|
||||
|
||||
if (state.isSwiping) {
|
||||
const deltaX = state.currentX - state.startX
|
||||
|
||||
if (deltaX >= threshold) {
|
||||
onBack()
|
||||
}
|
||||
|
||||
onSwipeEnd?.()
|
||||
}
|
||||
|
||||
swipeRef.current = {
|
||||
startX: 0,
|
||||
startY: 0,
|
||||
currentX: 0,
|
||||
isSwiping: false,
|
||||
isEdgeSwipe: false,
|
||||
}
|
||||
setSwipeProgress(0)
|
||||
}, [enabled, isMobile, threshold, onBack, onSwipeEnd])
|
||||
|
||||
const bind = useCallback((element: HTMLElement | null) => {
|
||||
if (!element || !enabled || !isMobile) return
|
||||
|
||||
element.addEventListener('touchstart', handleTouchStart, { passive: true })
|
||||
element.addEventListener('touchmove', handleTouchMove, { passive: false })
|
||||
element.addEventListener('touchend', handleTouchEnd, { passive: true })
|
||||
element.addEventListener('touchcancel', handleTouchEnd, { passive: true })
|
||||
|
||||
return () => {
|
||||
element.removeEventListener('touchstart', handleTouchStart)
|
||||
element.removeEventListener('touchmove', handleTouchMove)
|
||||
element.removeEventListener('touchend', handleTouchEnd)
|
||||
element.removeEventListener('touchcancel', handleTouchEnd)
|
||||
}
|
||||
}, [enabled, isMobile, handleTouchStart, handleTouchMove, handleTouchEnd])
|
||||
|
||||
const swipeStyles = {
|
||||
transform: swipeProgress > 0 ? `translateX(${swipeProgress * 50}px)` : undefined,
|
||||
transition: swipeProgress === 0 ? 'transform 0.2s ease-out' : undefined,
|
||||
}
|
||||
|
||||
return {
|
||||
bind,
|
||||
swipeProgress,
|
||||
swipeStyles,
|
||||
isSwiping: swipeRef.current.isSwiping,
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from "react";
|
||||
import { useState, useRef, useEffect, useCallback } from "react";
|
||||
import { useParams, useNavigate } from "react-router-dom";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { getRepo } from "@/api/repos";
|
||||
@@ -9,6 +9,7 @@ import { SwitchConfigDialog } from "@/components/repo/SwitchConfigDialog";
|
||||
import { BackButton } from "@/components/ui/back-button";
|
||||
import { useCreateSession } from "@/hooks/useOpenCode";
|
||||
import { OPENCODE_API_ENDPOINT } from "@/config";
|
||||
import { useSwipeBack } from "@/hooks/useMobile";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
@@ -20,8 +21,20 @@ export function RepoDetail() {
|
||||
const queryClient = useQueryClient();
|
||||
const repoId = parseInt(id || "0");
|
||||
const [fileBrowserOpen, setFileBrowserOpen] = useState(false);
|
||||
|
||||
const [switchConfigOpen, setSwitchConfigOpen] = useState(false);
|
||||
const pageRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const handleSwipeBack = useCallback(() => {
|
||||
navigate("/");
|
||||
}, [navigate]);
|
||||
|
||||
const { bind: bindSwipe, swipeStyles } = useSwipeBack(handleSwipeBack, {
|
||||
enabled: !fileBrowserOpen && !switchConfigOpen,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
return bindSwipe(pageRef.current);
|
||||
}, [bindSwipe]);
|
||||
|
||||
const { data: repo, isLoading: repoLoading } = useQuery({
|
||||
queryKey: ["repo", repoId],
|
||||
@@ -91,7 +104,11 @@ export function RepoDetail() {
|
||||
const currentBranch = repo.currentBranch || "main";
|
||||
|
||||
return (
|
||||
<div className="h-dvh max-h-dvh overflow-hidden bg-gradient-to-br from-background via-background to-background flex flex-col">
|
||||
<div
|
||||
ref={pageRef}
|
||||
className="h-dvh max-h-dvh overflow-hidden bg-gradient-to-br from-background via-background to-background flex flex-col"
|
||||
style={swipeStyles}
|
||||
>
|
||||
<div className="flex-shrink-0 z-10 border-b border-border bg-gradient-to-b from-background via-background to-background backdrop-blur-sm px-4 py-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-3">
|
||||
|
||||
@@ -19,6 +19,7 @@ import { useKeyboardShortcuts } from "@/hooks/useKeyboardShortcuts";
|
||||
import { useSettingsDialog } from "@/hooks/useSettingsDialog";
|
||||
import { usePermissionRequests } from "@/hooks/usePermissionRequests";
|
||||
import { useAutoScroll } from "@/hooks/useAutoScroll";
|
||||
import { useSwipeBack } from "@/hooks/useMobile";
|
||||
import { useEffect, useRef, useCallback } from "react";
|
||||
import { MessageSkeleton } from "@/components/message/MessageSkeleton";
|
||||
import { exportSession, downloadMarkdown } from "@/lib/exportSession";
|
||||
@@ -31,11 +32,24 @@ export function SessionDetail() {
|
||||
const repoId = parseInt(id || "0");
|
||||
const { preferences, updateSettings } = useSettings();
|
||||
const messageContainerRef = useRef<HTMLDivElement>(null);
|
||||
const pageRef = useRef<HTMLDivElement>(null);
|
||||
const [modelDialogOpen, setModelDialogOpen] = useState(false);
|
||||
const [sessionsDialogOpen, setSessionsDialogOpen] = useState(false);
|
||||
const [fileBrowserOpen, setFileBrowserOpen] = useState(false);
|
||||
const [selectedFilePath, setSelectedFilePath] = useState<string | undefined>();
|
||||
const [showScrollButton, setShowScrollButton] = useState(false);
|
||||
|
||||
const handleSwipeBack = useCallback(() => {
|
||||
navigate(`/repos/${repoId}`);
|
||||
}, [navigate, repoId]);
|
||||
|
||||
const { bind: bindSwipe, swipeStyles } = useSwipeBack(handleSwipeBack, {
|
||||
enabled: !fileBrowserOpen && !modelDialogOpen && !sessionsDialogOpen,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
return bindSwipe(pageRef.current);
|
||||
}, [bindSwipe]);
|
||||
|
||||
const { data: repo, isLoading: repoLoading } = useQuery({
|
||||
queryKey: ["repo", repoId],
|
||||
@@ -186,7 +200,11 @@ export function SessionDetail() {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="h-dvh max-h-dvh overflow-hidden bg-gradient-to-br from-background via-background to-background flex flex-col">
|
||||
<div
|
||||
ref={pageRef}
|
||||
className="h-dvh max-h-dvh overflow-hidden bg-gradient-to-br from-background via-background to-background flex flex-col"
|
||||
style={swipeStyles}
|
||||
>
|
||||
<SessionDetailHeader
|
||||
repo={repo}
|
||||
sessionId={sessionId}
|
||||
|
||||
Reference in new issue
Block a user