Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve snippet organization in SQL Editor #27881

Merged
merged 43 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
30bd0f3
Init changes for sql fodlers
joshenlim Jul 4, 2024
ff1aa0a
Merge branch 'master' into feat/sql-folders-new
joshenlim Jul 4, 2024
cfb9c6b
Merge master resolve conflicts
joshenlim Jul 4, 2024
67fac44
Added upsert logic in sql-editor-v2 valtio, hooked up with templates …
joshenlim Jul 4, 2024
a994bcf
Do up logic for creating new snippets by typing in /new or by clickin…
joshenlim Jul 4, 2024
61a57a9
Do up logic for updating and deleting snippets
joshenlim Jul 4, 2024
a49d1c3
Do up logic for favourites and shared snippets
joshenlim Jul 5, 2024
a839c50
Do up logic for favourites and shared snippets
joshenlim Jul 5, 2024
31b05e8
Fix
joshenlim Jul 5, 2024
36d4fda
Fix saving indicator, add empty states for favorites and shared snippets
joshenlim Jul 5, 2024
5754e44
Implement sorting
joshenlim Jul 5, 2024
d02f065
Some minor QOL improvements
joshenlim Jul 5, 2024
96cb9ab
Minor fix on empty state for private snippets
joshenlim Jul 5, 2024
57ad789
Add delete folder mutation
joshenlim Jul 5, 2024
47164d7
Implement create and update folder
joshenlim Jul 8, 2024
60da9f7
Fix reinstate with AI renaming for new snippets under folder
joshenlim Jul 8, 2024
f2f3ebc
Support controlled multi select behaviour in private snippets
joshenlim Jul 8, 2024
169f05b
Undo changes to tree-view-multi-select
joshenlim Jul 8, 2024
ff09863
Support bulk deletes
joshenlim Jul 8, 2024
a0b9e54
Support moving queries + rendering queries in folders
joshenlim Jul 9, 2024
33cd45c
Support deleting folders and creating a new folder when moving a query
joshenlim Jul 9, 2024
0bf4769
Fix bug where renaming query removes content
joshenlim Jul 9, 2024
d198936
Add initial loading state in sql editor nav + handle fallback if cann…
joshenlim Jul 9, 2024
34f55f7
Fix some spelling
joshenlim Jul 9, 2024
6c39eed
Fix TS issue in sql folders mutation keys
joshenlim Jul 9, 2024
0db5380
Fix toggling favorite
joshenlim Jul 9, 2024
ba0b1ec
Lint
joshenlim Jul 9, 2024
f29d3c4
Revert fallback behaviour in ]id] for now
joshenlim Jul 9, 2024
5b7d5f5
Fix favorites and shared snippets not showing
joshenlim Jul 9, 2024
44e7e7f
Fix moving currently opened snippet leads to loading
joshenlim Jul 12, 2024
ed8da76
Support bulk moving
joshenlim Jul 12, 2024
824810b
Improve multi select logic a little
joshenlim Jul 12, 2024
f4aaaf5
Merge master resolve conflicts
joshenlim Jul 12, 2024
3df8008
Nit lint
joshenlim Jul 12, 2024
6362073
Merge branch 'master' into feat/sql-folders-new
joshenlim Jul 15, 2024
8bc49ba
Reinstate AI retitling for untitled snippets when running query
joshenlim Jul 15, 2024
e6bf586
Remove hardcode in useAFlag
joshenlim Jul 15, 2024
d77384a
Support creating new snippet in a folder directly
joshenlim Jul 15, 2024
f88da3e
Fix sharing snippets that are within a folder
joshenlim Jul 15, 2024
2a9aa88
Fix sharing snippets within a folder
joshenlim Jul 15, 2024
a2f774f
Fix favorite
joshenlim Jul 15, 2024
b200b98
Add loading state when fetching folder contents
joshenlim Jul 16, 2024
d431629
Fix favoriting snippets in folders
joshenlim Jul 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ const TOTPFactors = () => {
<Button
size="tiny"
type="default"
// className="px-1"
// icon={<IconTrash />}
onClick={() => setFactorToBeDeleted(factor.id)}
>
Remove
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const QueryError = ({
<div className="flex gap-2">
<CollapsibleTrigger_Shadcn_ asChild>
<Button
size={'tiny'}
size="tiny"
type="outline"
className={cn('group', styles['ai-icon__container--allow-hover-effect'])}
>
Expand All @@ -78,7 +78,7 @@ const QueryError = ({
</CollapsibleTrigger_Shadcn_>
{!hasHipaaAddon && isAiAssistantEnabled && (
<Button
size={'tiny'}
size="tiny"
type="default"
className={cn(
'group',
Expand Down
12 changes: 4 additions & 8 deletions apps/studio/components/interfaces/Auth/Users/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import NoPermission from 'components/ui/NoPermission'
import { authKeys } from 'data/auth/keys'
import { useUsersQuery } from 'data/auth/users-query'
import { useCheckPermissions, usePermissionsLoaded } from 'hooks/misc/useCheckPermissions'
import { Button, IconRefreshCw, IconSearch, IconX, Input, Listbox } from 'ui'
import { Search, X } from 'lucide-react'
import { Button, IconRefreshCw, Input, Listbox } from 'ui'
import AddUserDropdown from './AddUserDropdown'
import UsersList from './UsersList'

Expand Down Expand Up @@ -84,15 +85,10 @@ const Users = () => {
name="email"
id="email"
placeholder="Search by email or phone number"
icon={<IconSearch size="tiny" />}
icon={<Search size={14} />}
actions={[
search && (
<Button
size="tiny"
type="text"
icon={<IconX size="tiny" />}
onClick={() => clearSearch()}
/>
<Button size="tiny" type="text" icon={<X />} onClick={() => clearSearch()} />
),
]}
/>
Expand Down
6 changes: 3 additions & 3 deletions apps/studio/components/interfaces/HomePageActions.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { IS_PLATFORM } from 'common'
import { Filter, Search } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/router'

import { IS_PLATFORM } from 'common'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { PROJECT_STATUS } from 'lib/constants'
import { EMPTY_ARR } from 'lib/void'
import { Filter, Search } from 'lucide-react'
import {
Button,
Checkbox_Shadcn_,
Expand All @@ -20,7 +21,6 @@ import {
PopoverTrigger_Shadcn_,
Popover_Shadcn_,
} from 'ui'
import { PROJECT_STATUS } from 'lib/constants'

interface HomePageActionsProps {
organizations: { name: string; slug: string }[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const pricingMetricBytes = [
]

export const formatUsage = (pricingMetric: PricingMetric, usage: number) => {
console.log({ pricingMetric, usage })
if (pricingMetricBytes.includes(pricingMetric)) {
return +(usage / 1e9).toFixed(2).toLocaleString()
} else {
Expand Down
10 changes: 4 additions & 6 deletions apps/studio/components/interfaces/SQLEditor/DiffActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import {
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
IconChevronDown,
IconCornerDownLeft,
IconLoader,
} from 'ui'

import { ChevronDown, CornerDownLeft, Loader2 } from 'lucide-react'
import { DiffType } from './SQLEditor.types'
import { getDiffTypeButtonLabel, getDiffTypeDropdownLabel } from './SQLEditor.utils'

Expand All @@ -34,8 +32,8 @@ export const DiffActionBar = ({
className="rounded-r-none"
type="primary"
size="tiny"
icon={loading && <IconLoader className="animate-spin" size={14} />}
iconRight={<IconCornerDownLeft size={12} strokeWidth={1.5} />}
icon={loading && <Loader2 className="animate-spin" />}
iconRight={<CornerDownLeft size={12} strokeWidth={1.5} />}
onClick={onAccept}
>
{getDiffTypeButtonLabel(selectedDiffType)}
Expand All @@ -45,7 +43,7 @@ export const DiffActionBar = ({
<Button
type="primary"
className="rounded-l-none border-l-0 px-[4px] py-[5px] flex"
icon={<IconChevronDown />}
icon={<ChevronDown />}
/>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" side="bottom">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { ModalProps } from '@ui/components/Modal/Modal'
import { snakeCase } from 'lodash'
import { ExternalLink } from 'lucide-react'
import Link from 'next/link'
import { useState } from 'react'
import { Button, CodeBlock, IconExternalLink, Modal, Tabs } from 'ui'

import type { ModalProps } from '@ui/components/Modal/Modal'
import TwoOptionToggle from 'components/ui/TwoOptionToggle'
import { useFlag } from 'hooks/ui/useFlag'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
import { useSqlEditorV2StateSnapshot } from 'state/sql-editor-v2'
import { Button, CodeBlock, Modal, Tabs } from 'ui'
import { Markdown } from '../Markdown'
import {
generateFileCliCommand,
Expand All @@ -19,8 +22,11 @@ export interface DownloadSnippetModalProps extends ModalProps {

const DownloadSnippetModal = ({ id, ...props }: DownloadSnippetModalProps) => {
const snap = useSqlEditorStateSnapshot()
const snippet = snap.snippets[id].snippet
const migrationName = snakeCase(snippet.name)
const snapV2 = useSqlEditorV2StateSnapshot()
const enableFolders = useFlag('sqlFolderOrganization')

const snippet = enableFolders ? snapV2.snippets[id]?.snippet : snap.snippets[id].snippet
const migrationName = snakeCase(snippet?.name)

const [selectedView, setSelectedView] = useState<'CLI' | 'NPM'>('CLI')

Expand Down Expand Up @@ -60,7 +66,7 @@ const DownloadSnippetModal = ({ id, ...props }: DownloadSnippetModalProps) => {
header={<p>Download snippet as local migration file via the Supabase CLI.</p>}
{...props}
>
<div className="flex flex-col items-start justify-between gap-4 relative">
<div className="flex flex-col items-start justify-between gap-4 relative pt-2">
<Tabs type="underlined" listClassNames="pl-5">
{SNIPPETS.map((snippet) => {
return (
Expand All @@ -75,7 +81,7 @@ const DownloadSnippetModal = ({ id, ...props }: DownloadSnippetModalProps) => {
/>
</div>
<TwoOptionToggle
width={75}
width={50}
options={['CLI', 'NPM']}
activeOption={selectedView}
borderOverride="border-muted"
Expand All @@ -100,7 +106,7 @@ const DownloadSnippetModal = ({ id, ...props }: DownloadSnippetModalProps) => {
<Modal.Content className="w-full flex items-center justify-between pt-0">
<p className="text-xs text-lighter">Run this command from your project directory</p>
<div className="flex justify-between items-center gap-x-2">
<Button asChild type="default" icon={<IconExternalLink size={14} strokeWidth={1.5} />}>
<Button asChild type="default" icon={<ExternalLink strokeWidth={1.5} />}>
<Link
href="https://supabase.com/docs/guides/cli/local-development#database-migrations"
target="_blank"
Expand All @@ -110,7 +116,7 @@ const DownloadSnippetModal = ({ id, ...props }: DownloadSnippetModalProps) => {
</Link>
</Button>

<Button asChild type="default" icon={<IconExternalLink size={14} strokeWidth={1.5} />}>
<Button asChild type="default" icon={<ExternalLink size={14} strokeWidth={1.5} />}>
<Link
href="https://supabase.com/docs/guides/cli/local-development"
target="_blank"
Expand Down
56 changes: 39 additions & 17 deletions apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { MutableRefObject, useEffect, useRef } from 'react'
import type { SqlSnippet } from 'data/content/sql-snippets-query'
import { useLocalStorageQuery } from 'hooks/misc/useLocalStorage'
import { useSelectedProject } from 'hooks/misc/useSelectedProject'
import { useFlag } from 'hooks/ui/useFlag'
import { LOCAL_STORAGE_KEYS } from 'lib/constants'
import { useProfile } from 'lib/profile'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
import { useSqlEditorV2StateSnapshot } from 'state/sql-editor-v2'
import { cn } from 'ui'
import { untitledSnippetTitle } from './SQLEditor.constants'
import type { IStandaloneCodeEditor } from './SQLEditor.types'
import { createSqlSnippetSkeleton } from './SQLEditor.utils'
import { createSqlSnippetSkeleton, createSqlSnippetSkeletonV2 } from './SQLEditor.utils'

export type MonacoEditorProps = {
id: string
Expand All @@ -34,18 +36,21 @@ const MonacoEditor = ({
executeQuery,
onHasSelection,
}: MonacoEditorProps) => {
const { ref, content } = useParams()
const router = useRouter()
const { profile } = useProfile()
const { ref, content } = useParams()
const project = useSelectedProject()

const snap = useSqlEditorStateSnapshot({ sync: true })
const snapV2 = useSqlEditorV2StateSnapshot()
const enableFolders = useFlag('sqlFolderOrganization')

const [intellisenseEnabled] = useLocalStorageQuery(
LOCAL_STORAGE_KEYS.SQL_EDITOR_INTELLISENSE,
true
)

const snap = useSqlEditorStateSnapshot({ sync: true })
const snippet = snap.snippets[id]
const snippet = enableFolders ? snapV2.snippets[id] : snap.snippets[id]

const executeQueryRef = useRef(executeQuery)
executeQueryRef.current = executeQuery
Expand Down Expand Up @@ -92,26 +97,43 @@ const MonacoEditor = ({
}
}

// [Joshen] Also needs updating here
const debouncedSetSql = debounce((id, value) => {
snap.setSql(id, value)
if (enableFolders) snapV2.setSql(id, value)
else snap.setSql(id, value)
}, 1000)

function handleEditorChange(value: string | undefined) {
const snippetCheck = enableFolders ? snapV2.snippets[id] : snap.snippets[id]

if (id && value) {
if (snap.snippets[id]) {
if (snippetCheck) {
debouncedSetSql(id, value)
} else {
const snippet = createSqlSnippetSkeleton({
id,
name: untitledSnippetTitle,
sql: value,
owner_id: profile?.id,
project_id: project?.id,
})
if (ref) {
snap.addSnippet(snippet as SqlSnippet, ref)
snap.addNeedsSaving(snippet.id!)
router.push(`/project/${ref}/sql/${snippet.id}`, undefined, { shallow: true })
if (ref && profile !== undefined && project !== undefined) {
if (enableFolders) {
const snippet = createSqlSnippetSkeletonV2({
id,
name: untitledSnippetTitle,
sql: value,
owner_id: profile?.id,
project_id: project?.id,
})
snapV2.addSnippet({ projectRef: ref, snippet })
snapV2.addNeedsSaving(snippet.id)
router.push(`/project/${ref}/sql/${snippet.id}`, undefined, { shallow: true })
} else {
const snippet = createSqlSnippetSkeleton({
id,
name: untitledSnippetTitle,
sql: value,
owner_id: profile.id,
project_id: project.id,
})
snap.addSnippet(snippet as SqlSnippet, ref)
snap.addNeedsSaving(snippet.id!)
router.push(`/project/${ref}/sql/${snippet.id}`, undefined, { shallow: true })
}
}
}
}
Expand Down
Loading