content.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { SimpleCodeBlock } from 'ui-patterns/SimpleCodeBlock'
  2. import type { ContentFileProps } from '@/components/interfaces/Connect/Connect.types'
  3. import {
  4. ConnectTabContent,
  5. ConnectTabs,
  6. ConnectTabTrigger,
  7. ConnectTabTriggers,
  8. } from '@/components/interfaces/Connect/ConnectTabs'
  9. const ContentFile = ({ projectKeys }: ContentFileProps) => {
  10. return (
  11. <ConnectTabs>
  12. <ConnectTabTriggers>
  13. <ConnectTabTrigger value=".env.local" />
  14. <ConnectTabTrigger value="page.tsx" />
  15. <ConnectTabTrigger value="utils/briven/server.ts" />
  16. <ConnectTabTrigger value="utils/briven/client.ts" />
  17. <ConnectTabTrigger value="utils/briven/middleware.ts" />
  18. </ConnectTabTriggers>
  19. <ConnectTabContent value=".env.local">
  20. <SimpleCodeBlock className="bash" parentClassName="min-h-72">
  21. {[
  22. '',
  23. `NEXT_PUBLIC_BRIVEN_URL=${projectKeys.apiUrl ?? 'your-project-url'}`,
  24. projectKeys?.publishableKey
  25. ? `NEXT_PUBLIC_BRIVEN_PUBLISHABLE_KEY=${projectKeys.publishableKey}`
  26. : `NEXT_PUBLIC_BRIVEN_ANON_KEY=${projectKeys.anonKey ?? 'your-anon-key'}`,
  27. '',
  28. ].join('\n')}
  29. </SimpleCodeBlock>
  30. </ConnectTabContent>
  31. <ConnectTabContent value="page.tsx">
  32. <SimpleCodeBlock className="tsx" parentClassName="min-h-72">
  33. {`
  34. import { createClient } from '@/utils/briven/server'
  35. import { cookies } from 'next/headers'
  36. export default async function Page() {
  37. const cookieStore = await cookies()
  38. const briven = createClient(cookieStore)
  39. const { data: todos } = await briven.from('todos').select()
  40. return (
  41. <ul>
  42. {todos?.map((todo) => (
  43. <li>{todo}</li>
  44. ))}
  45. </ul>
  46. )
  47. }
  48. `}
  49. </SimpleCodeBlock>
  50. </ConnectTabContent>
  51. <ConnectTabContent value="utils/briven/server.ts">
  52. <SimpleCodeBlock className="ts" parentClassName="min-h-72">
  53. {`
  54. import { createServerClient, type CookieOptions } from "@supabase/ssr";
  55. import { cookies } from "next/headers";
  56. const brivenUrl = process.env.NEXT_PUBLIC_BRIVEN_URL;
  57. const brivenKey = process.env.${projectKeys?.publishableKey ? 'NEXT_PUBLIC_BRIVEN_PUBLISHABLE_KEY' : 'NEXT_PUBLIC_BRIVEN_ANON_KEY'};
  58. export const createClient = (cookieStore: ReturnType<typeof cookies>) => {
  59. return createServerClient(
  60. brivenUrl!,
  61. brivenKey!,
  62. {
  63. cookies: {
  64. getAll() {
  65. return cookieStore.getAll()
  66. },
  67. setAll(cookiesToSet) {
  68. try {
  69. cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options))
  70. } catch {
  71. // The \`setAll\` method was called from a Server Component.
  72. // This can be ignored if you have middleware refreshing
  73. // user sessions.
  74. }
  75. },
  76. },
  77. },
  78. );
  79. };
  80. `}
  81. </SimpleCodeBlock>
  82. </ConnectTabContent>
  83. <ConnectTabContent value="utils/briven/client.ts">
  84. <SimpleCodeBlock className="ts" parentClassName="min-h-72">
  85. {`
  86. import { createBrowserClient } from "@supabase/ssr";
  87. const brivenUrl = process.env.NEXT_PUBLIC_BRIVEN_URL;
  88. const brivenKey = process.env.${projectKeys?.publishableKey ? 'NEXT_PUBLIC_BRIVEN_PUBLISHABLE_KEY' : 'NEXT_PUBLIC_BRIVEN_ANON_KEY'};
  89. export const createClient = () =>
  90. createBrowserClient(
  91. brivenUrl!,
  92. brivenKey!,
  93. );
  94. `}
  95. </SimpleCodeBlock>
  96. </ConnectTabContent>
  97. <ConnectTabContent value="utils/briven/middleware.ts">
  98. <SimpleCodeBlock className="ts" parentClassName="min-h-72">
  99. {`
  100. import { createServerClient, type CookieOptions } from "@supabase/ssr";
  101. import { type NextRequest, NextResponse } from "next/server";
  102. const brivenUrl = process.env.NEXT_PUBLIC_BRIVEN_URL;
  103. const brivenKey = process.env.${projectKeys?.publishableKey ? 'NEXT_PUBLIC_BRIVEN_PUBLISHABLE_KEY' : 'NEXT_PUBLIC_BRIVEN_ANON_KEY'};
  104. export const createClient = (request: NextRequest) => {
  105. // Create an unmodified response
  106. let brivenResponse = NextResponse.next({
  107. request: {
  108. headers: request.headers,
  109. },
  110. });
  111. const briven = createServerClient(
  112. brivenUrl!,
  113. brivenKey!,
  114. {
  115. cookies: {
  116. getAll() {
  117. return request.cookies.getAll()
  118. },
  119. setAll(cookiesToSet) {
  120. cookiesToSet.forEach(({ name, value, options }) => request.cookies.set(name, value))
  121. brivenResponse = NextResponse.next({
  122. request,
  123. })
  124. cookiesToSet.forEach(({ name, value, options }) =>
  125. brivenResponse.cookies.set(name, value, options)
  126. )
  127. },
  128. },
  129. },
  130. );
  131. return brivenResponse
  132. };
  133. `}
  134. </SimpleCodeBlock>
  135. </ConnectTabContent>
  136. </ConnectTabs>
  137. )
  138. }
  139. // [Joshen] Used as a dynamic import
  140. // eslint-disable-next-line no-restricted-exports
  141. export default ContentFile