The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
add package manager selection (#34558)
Saxon Fletcher38e1173c75e9
diff --git a/apps/ui-library/components/block-item.tsx b/apps/ui-library/components/block-item.tsxindex fe6d00963d6..178ca591d50 100644--- a/apps/ui-library/components/block-item.tsx+++ b/apps/ui-library/components/block-item.tsx@@ -1,6 +1,10 @@-import { Command } from '@/components/command'import { OpenInV0Button } from '@/components/open-in-v0-button'+import dynamic from 'next/dynamic'++//The dynamic import is to prevent the command component from being rendered on the server and cause hydration errors+const Command = dynamic(() => import('./command').then((mod) => mod.Command), { ssr: false })+interface BlockItemProps {name: string}diff --git a/apps/ui-library/components/command.tsx b/apps/ui-library/components/command.tsxindex 010126a6ff0..726f116b875 100644--- a/apps/ui-library/components/command.tsx+++ b/apps/ui-library/components/command.tsx@@ -1,25 +1,45 @@'use client'import { motion } from 'framer-motion'+import { Tabs_Shadcn_, TabsContent_Shadcn_, TabsList_Shadcn_, TabsTrigger_Shadcn_ } from 'ui'import { CommandCopyButton } from './command-copy-button'+import { useLocalStorage } from './use-local-storage'interface CommandCopyProps {name: stringhighlight?: boolean}+type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun'++const LOCAL_STORAGE_KEY = 'package-manager-copy-command'+export function Command({ name, highlight }: CommandCopyProps) {- const command = `npx shadcn@latest add ${- process.env.NEXT_PUBLIC_VERCEL_TARGET_ENV === 'production'- ? `https://supabase.com`- : process.env.NEXT_PUBLIC_VERCEL_TARGET_ENV === 'preview'- ? `https://${process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL}`- : 'http://localhost:3004'- }${process.env.NEXT_PUBLIC_BASE_PATH ?? ''}/r/${name}.json`+ const [value, setValue] = useLocalStorage(LOCAL_STORAGE_KEY, 'npm')++ const getBaseUrl = () => {+ if (process.env.NEXT_PUBLIC_VERCEL_TARGET_ENV === 'production') {+ return `https://supabase.com`+ } else if (process.env.NEXT_PUBLIC_VERCEL_TARGET_ENV === 'preview') {+ return `https://${process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL}`+ } else {+ return 'http://localhost:3004'+ }+ }++ const baseUrl = getBaseUrl()+ const componentPath = `${process.env.NEXT_PUBLIC_BASE_PATH ?? ''}/r/${name}.json`++ const commands: Record<PackageManager, string> = {+ npm: `npx shadcn@latest add ${baseUrl}${componentPath}`,+ pnpm: `pnpm dlx shadcn@latest add ${baseUrl}${componentPath}`,+ yarn: `npx shadcn@latest add ${baseUrl}${componentPath}`,+ bun: `bunx --bun shadcn@latest add ${baseUrl}${componentPath}`,+ }return (- <>- <div className="w-full group relative flex items-center rounded-lg bg-surface-100 px-4 py-2 overflow-hidden">+ <Tabs_Shadcn_ value={value} onValueChange={setValue} className="w-full">+ <div className="w-full group relative rounded-lg bg-surface-100 px-4 py-2 overflow-hidden">{highlight && (<motion.divclassName="absolute inset-0 bg-gradient-to-l from-transparent via-white to-transparent opacity-10 z-0"@@ -33,14 +53,31 @@ export function Command({ name, highlight }: CommandCopyProps) {}}/>)}- <div className="flex-1 font-mono text-sm text-foreground relative z-10">- <span className="mr-2 text-[#888]">$</span>- {command}- </div>- <div className="relative z-10">- <CommandCopyButton command={command} />++ <div className="flex flex-col">+ <TabsList_Shadcn_ className="gap-2 relative mb-2 z-10">+ {(Object.keys(commands) as PackageManager[]).map((manager) => (+ <TabsTrigger_Shadcn_ key={manager} value={manager} className="text-xs">+ {manager}+ </TabsTrigger_Shadcn_>+ ))}+ </TabsList_Shadcn_>++ {(Object.keys(commands) as PackageManager[]).map((manager) => (+ <TabsContent_Shadcn_ key={manager} value={manager} className="m-0">+ <div className="flex items-center">+ <div className="flex-1 font-mono text-sm text-foreground relative z-10">+ <span className="mr-2 text-[#888]">$</span>+ {commands[manager]}+ </div>+ <div className="relative z-10">+ <CommandCopyButton command={commands[manager]} />+ </div>+ </div>+ </TabsContent_Shadcn_>+ ))}</div></div>- </>+ </Tabs_Shadcn_>)}diff --git a/apps/ui-library/components/use-local-storage.tsx b/apps/ui-library/components/use-local-storage.tsxnew file mode 100644index 00000000000..0ef31e76346--- /dev/null+++ b/apps/ui-library/components/use-local-storage.tsx@@ -0,0 +1,47 @@+// Reference: https://usehooks.com/useLocalStorage/++import { useCallback, useState } from 'react'++export function useLocalStorage<T>(key: string, initialValue: T) {+ // State to store our value+ // Pass initial state function to useState so logic is only executed once+ const [storedValue, setStoredValue] = useState<T>(() => {+ if (typeof window === 'undefined') {+ return initialValue+ }++ try {+ // Get from local storage by key+ const item = window.localStorage.getItem(key)+ // Parse stored json or if none return initialValue+ return item ? JSON.parse(item) : initialValue+ } catch (error) {+ // If error also return initialValue+ console.log(error)+ return initialValue+ }+ })++ // Return a wrapped version of useState's setter function that ...+ // ... persists the new value to localStorage.+ const setValue = useCallback(+ (value: T | ((val: T) => T)) => {+ try {+ // Allow value to be a function so we have same API as useState+ const valueToStore = value instanceof Function ? value(storedValue) : value+ // Save state+ setStoredValue(valueToStore)+ // Save to local storage+ if (typeof window !== 'undefined') {+ window.localStorage.setItem(key, JSON.stringify(valueToStore))+ }+ } catch (error) {+ // A more advanced implementation would handle the error case+ console.log(error)+ }+ },+ [key, storedValue]+ )++ return [storedValue, setValue] as const+}