mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Part 2 of a 6-PR stack migrating all `next/link` imports in `apps/studio` onto TanStack Router's `Link` (or plain `<a>` for known external URLs) and removing `compat/next/link.tsx`. | Part | PR | Scope | | --- | --- | --- | | 1 | #54304 | `data/prefetchers/PrefetchableLink.tsx` and `components/ui/**` | | **2** | **#54305 ← this PR** | **`components/layouts/**` and top-level shell components** | | 3 | #54306 | Database, TableGridEditor, Storage, SQLEditor, BranchManagement, Branching, Functions, Realtime | | 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues, Linter, Reports, QueryPerformance, QuerySources, ConfigDrift, DiskManagement, Compute | | 5 | #54308 | Remaining `components/interfaces/**` (Org, Billing, Auth, Support, Home, SignIn, ConnectSheet, etc.) | | 6 | #54309 | Remove `compat/next/link.tsx` shim, Vite alias, and update basePath tests/docs | Migrates all `apps/studio/components/layouts/**` files and top-level navigation shell components off `next/link`. **Changed:** - Replaced `next/link` across `components/layouts/**` (`AccountLayout`, `AppLayout`, `AuthLayout`, `BranchLayout`, `DatabaseLayout`, `EdgeFunctionsLayout`, `ExplorerLayout`, `IssuesLayout`, `LogsLayout`, `Navigation`, `ObservabilityLayout`, `ProjectLayout`, `ProjectNeedsSecuring`, `SQLEditorLayout`, `SignInLayout`, `TableEditorLayout`, `Tabs`) and top-level shell components (`Sidebar.tsx`, `UserDropdown.tsx`, `LocalDropdown.tsx`, `HomePageActions.tsx`) - Removed `vi.mock('next/link', ...)` from `LocalDropdown.test.tsx` and `ProjectNeedsSecuring.test.tsx` - Added accessible labels where external icon/logo links switched from `<Link>` to plain `<a>` (`AdminStudioButton.tsx`, `ForgotPasswordLayout.tsx`) Addresses [FE-5108](https://linear.app/supabase/issue/FE-5108/replace-the-nextlink-shim-with-tanstack-link) ## To test - [ ] Navigate across top header, left sidebar, user/local dropdown, and project/org/branch switchers - [ ] Verify mobile navigation menu items and recent-tab links navigate with expected query params - [ ] Verify sign-in and forgot-password layout links work cleanly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
218 lines
7.9 KiB
TypeScript
218 lines
7.9 KiB
TypeScript
import { Link, useNavigate } from '@tanstack/react-router'
|
|
import { useDebounce } from '@uidotdev/usehooks'
|
|
import { LOCAL_STORAGE_KEYS, useFlag, useParams } from 'common'
|
|
import { FilePlusIcon, FolderPlusIcon, PlusIcon, ScrollTextIcon, XIcon } from 'lucide-react'
|
|
import { useEffect, useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Button,
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipTrigger,
|
|
} from 'ui'
|
|
import {
|
|
InnerSideBarFilters,
|
|
InnerSideBarFilterSearchInput,
|
|
InnerSideBarFilterSortDropdown,
|
|
InnerSideBarFilterSortDropdownItem,
|
|
} from 'ui-patterns/InnerSideMenu'
|
|
|
|
import { SearchList } from './SQLEditorNavV2/SearchList'
|
|
import { SQLEditorNav } from './SQLEditorNavV2/SQLEditorNav'
|
|
import { type SqlSnippetSource } from '@/components/interfaces/SQLEditor/querySource'
|
|
import {
|
|
FGA_PERMISSIONS,
|
|
useAsyncCheckUserContentPermissions,
|
|
} from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useLocalStorage } from '@/hooks/misc/useLocalStorage'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { getErrorMessage } from '@/lib/get-error-message'
|
|
import { navigateToUrl } from '@/lib/navigation'
|
|
import { useProfile } from '@/lib/profile'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor/sql-editor-state'
|
|
|
|
export const SQLEditorMenu = () => {
|
|
const navigate = useNavigate()
|
|
const { ref } = useParams()
|
|
const { profile } = useProfile()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const snapV2 = useSqlEditorV2StateSnapshot()
|
|
const track = useTrack()
|
|
|
|
const sqlEditorLogsSource = useFlag('sqlEditorLogsSource')
|
|
const otelLegacyLogs = useFlag('otelLegacyLogs')
|
|
const canCreateLogsSnippet = sqlEditorLogsSource && otelLegacyLogs
|
|
|
|
const [search, setSearch] = useState('')
|
|
const [showSearch, setShowSearch] = useState(false)
|
|
const [sort, setSort] = useLocalStorage<'name' | 'inserted_at'>(
|
|
LOCAL_STORAGE_KEYS.SQL_EDITOR_SORT(ref ?? ''),
|
|
'inserted_at'
|
|
)
|
|
|
|
const debouncedSearch = useDebounce(search, 500)
|
|
|
|
const { can: canCreateSQLSnippet } = useAsyncCheckUserContentPermissions(
|
|
FGA_PERMISSIONS.PROJECT.SNIPPETS_WRITE,
|
|
{
|
|
mode: 'create',
|
|
type: 'sql',
|
|
}
|
|
)
|
|
|
|
const createNewFolder = () => {
|
|
if (!ref) return console.error('Project ref is required')
|
|
setSearch('')
|
|
setShowSearch(false)
|
|
snapV2.addNewFolder({ projectRef: ref })
|
|
}
|
|
|
|
const handleNewQuery = async (source: SqlSnippetSource = 'database') => {
|
|
if (!ref) return console.error('Project ref is required')
|
|
if (!project) return console.error('Project is required')
|
|
if (!profile) return console.error('Profile is required')
|
|
if (!canCreateSQLSnippet) {
|
|
return toast('Your queries will not be saved as you do not have sufficient permissions')
|
|
}
|
|
try {
|
|
const suffix = source === 'logs' ? '&source=logs' : ''
|
|
void navigateToUrl(navigate, `/project/${ref}/sql/new?skip=true${suffix}`)
|
|
setSearch('')
|
|
setShowSearch(false)
|
|
} catch (error) {
|
|
toast.error(`Failed to create new query: ${getErrorMessage(error)}`)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
setShowSearch(debouncedSearch.length > 0)
|
|
}, [debouncedSearch])
|
|
|
|
return (
|
|
<div className="h-full flex flex-col justify-between">
|
|
<div className="flex flex-col gap-y-4 grow">
|
|
<div className="mt-4 mx-4 flex items-center justify-between gap-x-2">
|
|
<InnerSideBarFilters className="w-full p-0 gap-0">
|
|
<InnerSideBarFilterSearchInput
|
|
name="search-queries"
|
|
placeholder="Search queries..."
|
|
aria-labelledby="Search queries"
|
|
value={search}
|
|
onChange={(e) => {
|
|
const value = e.target.value
|
|
// A search session starts on the first keystroke into an empty input
|
|
if (search.length === 0 && value.length > 0) {
|
|
track('sql_editor_snippet_search_submitted')
|
|
}
|
|
setSearch(value)
|
|
if (value.length === 0) setShowSearch(false)
|
|
}}
|
|
onKeyDown={(e) => {
|
|
if (e.code === 'Escape') {
|
|
setSearch('')
|
|
setShowSearch(false)
|
|
}
|
|
}}
|
|
>
|
|
{showSearch ? (
|
|
<Tooltip>
|
|
<TooltipTrigger
|
|
className="absolute right-1 top-[.4rem] md:top-[.3rem] transition-colors text-foreground-light hover:text-foreground"
|
|
onClick={() => {
|
|
setSearch('')
|
|
setShowSearch(false)
|
|
}}
|
|
>
|
|
<XIcon size={18} />
|
|
</TooltipTrigger>
|
|
<TooltipContent>Clear search</TooltipContent>
|
|
</Tooltip>
|
|
) : (
|
|
<InnerSideBarFilterSortDropdown
|
|
value={sort}
|
|
onValueChange={(value) => {
|
|
const sortBy = value as 'name' | 'inserted_at'
|
|
if (sortBy !== sort) track('sql_editor_snippet_sort_updated', { sortBy })
|
|
setSort(sortBy)
|
|
}}
|
|
>
|
|
<InnerSideBarFilterSortDropdownItem key="name" value="name">
|
|
Alphabetical
|
|
</InnerSideBarFilterSortDropdownItem>
|
|
<InnerSideBarFilterSortDropdownItem key="inserted_at" value="inserted_at">
|
|
Created At
|
|
</InnerSideBarFilterSortDropdownItem>
|
|
</InnerSideBarFilterSortDropdown>
|
|
)}
|
|
</InnerSideBarFilterSearchInput>
|
|
</InnerSideBarFilters>
|
|
<DropdownMenu>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
data-testid="sql-editor-new-query-button"
|
|
icon={<PlusIcon className="text-foreground" />}
|
|
className="w-[26px]"
|
|
aria-label="Create a new query"
|
|
/>
|
|
</DropdownMenuTrigger>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="bottom">Create a new query</TooltipContent>
|
|
</Tooltip>
|
|
<DropdownMenuContent align="end" side="bottom" className="w-48">
|
|
<DropdownMenuItem
|
|
className="gap-x-2"
|
|
onClick={() => {
|
|
track('sql_editor_new_item_clicked', { itemType: 'snippet' })
|
|
handleNewQuery()
|
|
}}
|
|
>
|
|
<FilePlusIcon size={14} />
|
|
Create a new snippet
|
|
</DropdownMenuItem>
|
|
{canCreateLogsSnippet && (
|
|
<DropdownMenuItem
|
|
className="gap-x-2"
|
|
onClick={() => {
|
|
track('sql_editor_new_item_clicked', { itemType: 'logs_query' })
|
|
handleNewQuery('logs')
|
|
}}
|
|
>
|
|
<ScrollTextIcon size={14} />
|
|
Create a new logs query
|
|
</DropdownMenuItem>
|
|
)}
|
|
<DropdownMenuItem
|
|
className="gap-x-2"
|
|
onClick={() => {
|
|
track('sql_editor_new_item_clicked', { itemType: 'folder' })
|
|
createNewFolder()
|
|
}}
|
|
>
|
|
<FolderPlusIcon size={14} />
|
|
Create a new folder
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
|
|
{showSearch ? <SearchList search={debouncedSearch} /> : <SQLEditorNav sort={sort} />}
|
|
</div>
|
|
|
|
<div className="p-4 border-t sticky bottom-0 bg-studio">
|
|
<Button asChild block>
|
|
<Link to="/project/$ref/observability/connections" params={{ ref: ref ?? '_' }}>
|
|
View running queries
|
|
</Link>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|