mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-23 21:59:28 -05:00
Compare commits
178 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b099219244 | |||
| af82329c4a | |||
| cfff6b1495 | |||
| 9bc4e69821 | |||
| 81b1c036f6 | |||
| 635200db78 | |||
| 793320746e | |||
| 6501041a48 | |||
| f5337e77f3 | |||
| 0192c1ed00 | |||
| 2e2b13c36b | |||
| 95dd4404d1 | |||
| 8a9912f839 | |||
| 016dc3d92a | |||
| 3a147a2b09 | |||
| e159b45911 | |||
| 96d14b98f0 | |||
| aa90d9fd1a | |||
| 3da7129413 | |||
| 75fbb23190 | |||
| 2ffe79ffd2 | |||
| 439dd0b44e | |||
| 2556f5e15d | |||
| cc0eec3bf0 | |||
| 4b009a8eb4 | |||
| 2aaddf7306 | |||
| fb5d6145d0 | |||
| 59310bac93 | |||
| 322f0be197 | |||
| 1a02f91afd | |||
| cc22ccb22d | |||
| 12763f0ef6 | |||
| d39e3ee638 | |||
| d85242a86b | |||
| ef53065abc | |||
| 805c1c6874 | |||
| 01687e8907 | |||
| 60f6ca9463 | |||
| 31d455002d | |||
| aa27d242bb | |||
| d96304d86d | |||
| 1064f68435 | |||
| 3d16e859c6 | |||
| af198c5632 | |||
| a43ed2b25c | |||
| 87bcad2b20 | |||
| b5eaa4c7fd | |||
| 995c03bc01 | |||
| b4395a48c5 | |||
| 461e3893fe | |||
| 735a9f84ec | |||
| 8cb8d734cf | |||
| 44d5530b48 | |||
| a314eb391e | |||
| 6c34c316d0 | |||
| 4f26278f16 | |||
| b975e7fa2e | |||
| 6c3052f9e4 | |||
| a771ae189a | |||
| 5bb8119ebf | |||
| 02411277d4 | |||
| 4cfb8c6d7b | |||
| e74a51a5ff | |||
| 29cc6a10fe | |||
| 029e069af6 | |||
| 01f765e969 | |||
| 9366960f18 | |||
| 697dc9cc99 | |||
| 83bc272ed2 | |||
| 59cc9c564e | |||
| 20dc147682 | |||
| 2bb7a6f277 | |||
| 81272b96e1 | |||
| deb062dd03 | |||
| 474be86d33 | |||
| e7ca66ed77 | |||
| 2b49dbecd3 | |||
| 6da4c6f352 | |||
| 659b240fca | |||
| 19c0b1d14d | |||
| b4472f48e9 | |||
| d197271771 | |||
| 37f652c70e | |||
| 645f0ab0d1 | |||
| 389a7d9e7b | |||
| c4cf468c7e | |||
| cbc3e923e4 | |||
| a96ba8b1e7 | |||
| e830871361 | |||
| 998e5c0819 | |||
| 13a56b0237 | |||
| 0b5418a03a | |||
| 0d8a338965 | |||
| d3250736a9 | |||
| e6ee6a6b0d | |||
| c0b097f929 | |||
| 78d336f8c7 | |||
| 95a7a265b9 | |||
| 136e59da68 | |||
| eb0a87cf80 | |||
| 0dcb98ac29 | |||
| 540f7aaae7 | |||
| 2d4614a0bd | |||
| 633bf18204 | |||
| 9a6cbd05b6 | |||
| 94b0248075 | |||
| 082de1042d | |||
| 8c19587baa | |||
| 433750d3fe | |||
| 61befd5ffd | |||
| 1e7817fb69 | |||
| f250bc7e88 | |||
| c7faa29437 | |||
| a51a006c26 | |||
| ce96cb0b89 | |||
| fb265d9dba | |||
| e4c155b501 | |||
| 2dc5c50f4d | |||
| bddcec0466 | |||
| 92677e1ec0 | |||
| b12228e305 | |||
| 91be2af30b | |||
| 84c668be86 | |||
| 4015c76f2b | |||
| a7b2ade4a9 | |||
| 75f44952c7 | |||
| 0df5e26381 | |||
| 89bb3bcd84 | |||
| 30fdb72c09 | |||
| cb58cf5825 | |||
| 99bd2ba256 | |||
| 9df423073f | |||
| 3e3c696972 | |||
| cb41e2d344 | |||
| 1e19cca7d9 | |||
| fa882dd4cc | |||
| 0b82c6de77 | |||
| a944d7626e | |||
| d1b12dc228 | |||
| 9f7d6038b1 | |||
| cffeb0513e | |||
| bc334c24cf | |||
| 077a9934ad | |||
| 1ed8d8076e | |||
| 345b282733 | |||
| c7c30a9d58 | |||
| 08510659de | |||
| f8fa29d56e | |||
| 8b048c3105 | |||
| b2705a4f8f | |||
| e867caa373 | |||
| ff6176df0a | |||
| d0f4228b45 | |||
| de79b58648 | |||
| 04d528b9b8 | |||
| c815b11015 | |||
| 1e7830d850 | |||
| 77cd1e9bd1 | |||
| e665227437 | |||
| 3a802810e3 | |||
| fbbf917093 | |||
| a7e42bfd29 | |||
| 562fdec899 | |||
| 75e71e39bc | |||
| 337aedf463 | |||
| d670d5de31 | |||
| 5ccb4af249 | |||
| 62aa186a81 | |||
| cb094761ca | |||
| f35e54f21d | |||
| f49f40610b | |||
| 9e754bad9c | |||
| 4dcf6fda40 | |||
| 1b8ccd7199 | |||
| 4f9088559f | |||
| 18550f1d11 | |||
| 881cd31f74 | |||
| e00405dca2 |
@@ -0,0 +1 @@
|
|||||||
|
{"sessionId":"f77248e2-8840-41c6-968b-c3b7d8a9e913","pid":49125,"acquiredAt":1776168010367}
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
---
|
|
||||||
name: integration-nextjs-pages-router
|
|
||||||
description: PostHog integration for Next.js Pages Router applications
|
|
||||||
metadata:
|
|
||||||
author: PostHog
|
|
||||||
version: 1.8.1
|
|
||||||
---
|
|
||||||
|
|
||||||
# PostHog integration for Next.js Pages Router
|
|
||||||
|
|
||||||
This skill helps you add PostHog analytics to Next.js Pages Router applications.
|
|
||||||
|
|
||||||
## Workflow
|
|
||||||
|
|
||||||
Follow these steps in order to complete the integration:
|
|
||||||
|
|
||||||
1. `basic-integration-1.0-begin.md` - PostHog Setup - Begin ← **Start here**
|
|
||||||
2. `basic-integration-1.1-edit.md` - PostHog Setup - Edit
|
|
||||||
3. `basic-integration-1.2-revise.md` - PostHog Setup - Revise
|
|
||||||
4. `basic-integration-1.3-conclude.md` - PostHog Setup - Conclusion
|
|
||||||
|
|
||||||
## Reference files
|
|
||||||
|
|
||||||
- `EXAMPLE.md` - Next.js Pages Router example project code
|
|
||||||
- `next-js.md` - Next.js - docs
|
|
||||||
- `identify-users.md` - Identify users - docs
|
|
||||||
- `basic-integration-1.0-begin.md` - PostHog setup - begin
|
|
||||||
- `basic-integration-1.1-edit.md` - PostHog setup - edit
|
|
||||||
- `basic-integration-1.2-revise.md` - PostHog setup - revise
|
|
||||||
- `basic-integration-1.3-conclude.md` - PostHog setup - conclusion
|
|
||||||
|
|
||||||
The example project shows the target implementation pattern. Consult the documentation for API details.
|
|
||||||
|
|
||||||
## Key principles
|
|
||||||
|
|
||||||
- **Environment variables**: Always use environment variables for PostHog keys. Never hardcode them.
|
|
||||||
- **Minimal changes**: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.
|
|
||||||
- **Match the example**: Your implementation should follow the example project's patterns as closely as possible.
|
|
||||||
|
|
||||||
## Framework guidelines
|
|
||||||
|
|
||||||
- For Next.js 15.3+, initialize PostHog in instrumentation-client.ts for the simplest setup
|
|
||||||
- For feature flags, use useFeatureFlagEnabled() or useFeatureFlagPayload() hooks - they handle loading states and external sync automatically
|
|
||||||
- Add analytics capture in event handlers where user actions occur, NOT in useEffect reacting to state changes
|
|
||||||
- Do NOT use useEffect for data transformation - calculate derived values during render instead
|
|
||||||
- Do NOT use useEffect to respond to user events - put that logic in the event handler itself
|
|
||||||
- Do NOT use useEffect to chain state updates - calculate all related updates together in the event handler
|
|
||||||
- Do NOT use useEffect to notify parent components - call the parent callback alongside setState in the event handler
|
|
||||||
- To reset component state when a prop changes, pass the prop as the component's key instead of using useEffect
|
|
||||||
- useEffect is ONLY for synchronizing with external systems (non-React widgets, browser APIs, network subscriptions)
|
|
||||||
|
|
||||||
## Identifying users
|
|
||||||
|
|
||||||
Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to maintain correlation.
|
|
||||||
|
|
||||||
## Error tracking
|
|
||||||
|
|
||||||
Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries.
|
|
||||||
@@ -1,761 +0,0 @@
|
|||||||
# PostHog Next.js Pages Router Example Project
|
|
||||||
|
|
||||||
Repository: https://github.com/PostHog/context-mill
|
|
||||||
Path: basics/next-pages-router
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## README.md
|
|
||||||
|
|
||||||
# PostHog Next.js pages router example
|
|
||||||
|
|
||||||
This is a [Next.js](https://nextjs.org) Pages Router example demonstrating PostHog integration with product analytics, session replay, feature flags, and error tracking.
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
- **Product Analytics**: Track user events and behaviors
|
|
||||||
- **Session Replay**: Record and replay user sessions
|
|
||||||
- **Error Tracking**: Capture and track errors
|
|
||||||
- **User Authentication**: Demo login system with PostHog user identification
|
|
||||||
- **Server-side & Client-side Tracking**: Examples of both tracking methods
|
|
||||||
- **Reverse Proxy**: PostHog ingestion through Next.js rewrites
|
|
||||||
|
|
||||||
## Getting Started
|
|
||||||
|
|
||||||
### 1. Install Dependencies
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install
|
|
||||||
# or
|
|
||||||
pnpm install
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. Configure Environment Variables
|
|
||||||
|
|
||||||
Create a `.env.local` file in the root directory:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
NEXT_PUBLIC_POSTHOG_KEY=your_posthog_project_api_key
|
|
||||||
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
|
|
||||||
```
|
|
||||||
|
|
||||||
Get your PostHog API key from your [PostHog project settings](https://app.posthog.com/project/settings).
|
|
||||||
|
|
||||||
### 3. Run the Development Server
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run dev
|
|
||||||
# or
|
|
||||||
pnpm dev
|
|
||||||
```
|
|
||||||
|
|
||||||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the app.
|
|
||||||
|
|
||||||
## Project Structure
|
|
||||||
|
|
||||||
```
|
|
||||||
src/
|
|
||||||
├── components/
|
|
||||||
│ └── Header.tsx # Navigation header with auth state
|
|
||||||
├── contexts/
|
|
||||||
│ └── AuthContext.tsx # Authentication context with PostHog integration
|
|
||||||
├── lib/
|
|
||||||
│ └── posthog-server.ts # Server-side PostHog client
|
|
||||||
├── pages/
|
|
||||||
│ ├── _app.tsx # App wrapper with Auth provider
|
|
||||||
│ ├── _document.tsx # Document wrapper
|
|
||||||
│ ├── index.tsx # Home/Login page
|
|
||||||
│ ├── burrito.tsx # Demo feature page with event tracking
|
|
||||||
│ ├── profile.tsx # User profile with error tracking demo
|
|
||||||
│ └── api/
|
|
||||||
│ └── auth/
|
|
||||||
│ └── login.ts # Login API with server-side tracking
|
|
||||||
└── styles/
|
|
||||||
└── globals.css # Global styles
|
|
||||||
|
|
||||||
instrumentation-client.ts # Client-side PostHog initialization
|
|
||||||
```
|
|
||||||
|
|
||||||
## Key Integration Points
|
|
||||||
|
|
||||||
### Client-side initialization (instrumentation-client.ts)
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import posthog from "posthog-js"
|
|
||||||
|
|
||||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
|
|
||||||
api_host: "/ingest",
|
|
||||||
ui_host: "https://us.posthog.com",
|
|
||||||
defaults: '2026-01-30',
|
|
||||||
capture_exceptions: true,
|
|
||||||
debug: process.env.NODE_ENV === "development",
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### User identification (AuthContext.tsx)
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
posthog.identify(username, {
|
|
||||||
username: username,
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### Event tracking (burrito.tsx)
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
posthog.capture('burrito_considered', {
|
|
||||||
total_considerations: count,
|
|
||||||
username: username,
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### Error tracking (profile.tsx)
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
posthog.captureException(error);
|
|
||||||
```
|
|
||||||
|
|
||||||
### Server-side tracking (api/auth/login.ts)
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const posthog = getPostHogClient();
|
|
||||||
posthog.capture({
|
|
||||||
distinctId: username,
|
|
||||||
event: 'server_login',
|
|
||||||
properties: { ... }
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## Pages router differences from app router
|
|
||||||
|
|
||||||
This example uses Next.js Pages Router instead of App Router. Key differences:
|
|
||||||
|
|
||||||
1. **File-based routing**: Pages in `src/pages/` instead of `src/app/`
|
|
||||||
2. **_app.tsx**: Custom App component wraps all pages
|
|
||||||
3. **API Routes**: Located in `src/pages/api/`
|
|
||||||
4. **No 'use client'**: All pages are client-side by default
|
|
||||||
5. **useRouter**: From `next/router` instead of `next/navigation`
|
|
||||||
6. **Head component**: Using `next/head` for metadata instead of `metadata` export
|
|
||||||
|
|
||||||
## Learn More
|
|
||||||
|
|
||||||
- [PostHog Documentation](https://posthog.com/docs)
|
|
||||||
- [Next.js Pages Router Documentation](https://nextjs.org/docs/pages)
|
|
||||||
- [PostHog Next.js Integration Guide](https://posthog.com/docs/libraries/next-js)
|
|
||||||
|
|
||||||
## Deploy on Vercel
|
|
||||||
|
|
||||||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new).
|
|
||||||
|
|
||||||
Check out the [Next.js deployment documentation](https://nextjs.org/docs/pages/building-your-application/deploying) for more details.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## instrumentation-client.ts
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import posthog from "posthog-js"
|
|
||||||
|
|
||||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
|
|
||||||
api_host: "/ingest",
|
|
||||||
ui_host: "https://us.posthog.com",
|
|
||||||
// Include the defaults option as required by PostHog
|
|
||||||
defaults: '2026-01-30',
|
|
||||||
// Enables capturing unhandled exceptions via Error Tracking
|
|
||||||
capture_exceptions: true,
|
|
||||||
// Turn on debug in development mode
|
|
||||||
debug: process.env.NODE_ENV === "development",
|
|
||||||
});
|
|
||||||
|
|
||||||
//IMPORTANT: Never combine this approach with other client-side PostHog initialization approaches, especially components like a PostHogProvider. instrumentation-client.ts is the correct solution for initializating client-side PostHog in Next.js 15.3+ apps.
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## next.config.ts
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import type { NextConfig } from "next";
|
|
||||||
|
|
||||||
const nextConfig: NextConfig = {
|
|
||||||
/* config options here */
|
|
||||||
reactStrictMode: true,
|
|
||||||
async rewrites() {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
source: "/ingest/static/:path*",
|
|
||||||
destination: "https://us-assets.i.posthog.com/static/:path*",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
source: "/ingest/:path*",
|
|
||||||
destination: "https://us.i.posthog.com/:path*",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
},
|
|
||||||
// This is required to support PostHog trailing slash API requests
|
|
||||||
skipTrailingSlashRedirect: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
export default nextConfig;
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/components/Header.tsx
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import Link from 'next/link';
|
|
||||||
import { useAuth } from '@/contexts/AuthContext';
|
|
||||||
|
|
||||||
export default function Header() {
|
|
||||||
const { user, logout } = useAuth();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<header className="header">
|
|
||||||
<div className="header-container">
|
|
||||||
<nav>
|
|
||||||
<Link href="/">Home</Link>
|
|
||||||
{user && (
|
|
||||||
<>
|
|
||||||
<Link href="/burrito">Burrito Consideration</Link>
|
|
||||||
<Link href="/profile">Profile</Link>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</nav>
|
|
||||||
<div className="user-section">
|
|
||||||
{user ? (
|
|
||||||
<>
|
|
||||||
<span>Welcome, {user.username}!</span>
|
|
||||||
<button onClick={logout} className="btn-logout">
|
|
||||||
Logout
|
|
||||||
</button>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<span>Not logged in</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</header>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/contexts/AuthContext.tsx
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import { createContext, useContext, useState, ReactNode } from 'react';
|
|
||||||
import posthog from 'posthog-js';
|
|
||||||
|
|
||||||
interface User {
|
|
||||||
username: string;
|
|
||||||
burritoConsiderations: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AuthContextType {
|
|
||||||
user: User | null;
|
|
||||||
login: (username: string, password: string) => Promise<boolean>;
|
|
||||||
logout: () => void;
|
|
||||||
incrementBurritoConsiderations: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
||||||
|
|
||||||
const users: Map<string, User> = new Map();
|
|
||||||
|
|
||||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
|
||||||
// Use lazy initializer to read from localStorage only once on mount
|
|
||||||
const [user, setUser] = useState<User | null>(() => {
|
|
||||||
if (typeof window === 'undefined') return null;
|
|
||||||
|
|
||||||
const storedUsername = localStorage.getItem('currentUser');
|
|
||||||
if (storedUsername) {
|
|
||||||
const existingUser = users.get(storedUsername);
|
|
||||||
if (existingUser) {
|
|
||||||
return existingUser;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
const login = async (username: string, password: string): Promise<boolean> => {
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/auth/login', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ username, password }),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const { user: userData } = await response.json();
|
|
||||||
|
|
||||||
// Get or create user in local map
|
|
||||||
let localUser = users.get(username);
|
|
||||||
if (!localUser) {
|
|
||||||
localUser = userData as User;
|
|
||||||
users.set(username, localUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
setUser(localUser);
|
|
||||||
localStorage.setItem('currentUser', username);
|
|
||||||
|
|
||||||
// Identify user in PostHog using username as distinct ID
|
|
||||||
posthog.identify(username, {
|
|
||||||
username: username,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Capture login event
|
|
||||||
posthog.capture('user_logged_in', {
|
|
||||||
username: username,
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Login error:', error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const logout = () => {
|
|
||||||
// Capture logout event before resetting
|
|
||||||
posthog.capture('user_logged_out');
|
|
||||||
posthog.reset();
|
|
||||||
|
|
||||||
setUser(null);
|
|
||||||
localStorage.removeItem('currentUser');
|
|
||||||
};
|
|
||||||
|
|
||||||
const incrementBurritoConsiderations = () => {
|
|
||||||
if (user) {
|
|
||||||
user.burritoConsiderations++;
|
|
||||||
users.set(user.username, user);
|
|
||||||
setUser({ ...user });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<AuthContext.Provider value={{ user, login, logout, incrementBurritoConsiderations }}>
|
|
||||||
{children}
|
|
||||||
</AuthContext.Provider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useAuth() {
|
|
||||||
const context = useContext(AuthContext);
|
|
||||||
if (context === undefined) {
|
|
||||||
throw new Error('useAuth must be used within an AuthProvider');
|
|
||||||
}
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/lib/posthog-server.ts
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { PostHog } from 'posthog-node';
|
|
||||||
|
|
||||||
let posthogClient: PostHog | null = null;
|
|
||||||
|
|
||||||
export function getPostHogClient() {
|
|
||||||
if (!posthogClient) {
|
|
||||||
posthogClient = new PostHog(
|
|
||||||
process.env.NEXT_PUBLIC_POSTHOG_KEY!,
|
|
||||||
{
|
|
||||||
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
|
||||||
flushAt: 1,
|
|
||||||
flushInterval: 0
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return posthogClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function shutdownPostHog() {
|
|
||||||
if (posthogClient) {
|
|
||||||
await posthogClient.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/pages/_app.tsx
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import "@/styles/globals.css";
|
|
||||||
import type { AppProps } from "next/app";
|
|
||||||
import { AuthProvider } from "@/contexts/AuthContext";
|
|
||||||
|
|
||||||
export default function App({ Component, pageProps }: AppProps) {
|
|
||||||
return (
|
|
||||||
<AuthProvider>
|
|
||||||
<Component {...pageProps} />
|
|
||||||
</AuthProvider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/pages/_document.tsx
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import { Html, Head, Main, NextScript } from "next/document";
|
|
||||||
|
|
||||||
export default function Document() {
|
|
||||||
return (
|
|
||||||
<Html lang="en">
|
|
||||||
<Head />
|
|
||||||
<body>
|
|
||||||
<Main />
|
|
||||||
<NextScript />
|
|
||||||
</body>
|
|
||||||
</Html>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/pages/api/auth/login.ts
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { getPostHogClient } from '@/lib/posthog-server';
|
|
||||||
|
|
||||||
const users = new Map<string, { username: string; burritoConsiderations: number }>();
|
|
||||||
|
|
||||||
export default async function handler(
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse
|
|
||||||
) {
|
|
||||||
if (req.method !== 'POST') {
|
|
||||||
return res.status(405).json({ error: 'Method not allowed' });
|
|
||||||
}
|
|
||||||
|
|
||||||
const { username, password } = req.body;
|
|
||||||
|
|
||||||
if (!username || !password) {
|
|
||||||
return res.status(400).json({ error: 'Username and password required' });
|
|
||||||
}
|
|
||||||
|
|
||||||
let user = users.get(username);
|
|
||||||
const isNewUser = !user;
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
user = { username, burritoConsiderations: 0 };
|
|
||||||
users.set(username, user);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Capture server-side login event
|
|
||||||
const posthog = getPostHogClient();
|
|
||||||
posthog.capture({
|
|
||||||
distinctId: username,
|
|
||||||
event: 'server_login',
|
|
||||||
properties: {
|
|
||||||
username: username,
|
|
||||||
isNewUser: isNewUser,
|
|
||||||
source: 'api'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Identify user on server side
|
|
||||||
posthog.identify({
|
|
||||||
distinctId: username,
|
|
||||||
properties: {
|
|
||||||
username: username,
|
|
||||||
createdAt: isNewUser ? new Date().toISOString() : undefined
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).json({ success: true, user });
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/pages/api/hello.ts
|
|
||||||
|
|
||||||
```ts
|
|
||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
|
||||||
|
|
||||||
type Data = {
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function handler(
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse<Data>,
|
|
||||||
) {
|
|
||||||
res.status(200).json({ name: "John Doe" });
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/pages/burrito.tsx
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import { useState } from 'react';
|
|
||||||
import Head from 'next/head';
|
|
||||||
import { useRouter } from 'next/router';
|
|
||||||
import posthog from 'posthog-js';
|
|
||||||
import { useAuth } from '@/contexts/AuthContext';
|
|
||||||
import Header from '@/components/Header';
|
|
||||||
|
|
||||||
export default function BurritoPage() {
|
|
||||||
const { user, incrementBurritoConsiderations } = useAuth();
|
|
||||||
const router = useRouter();
|
|
||||||
const [hasConsidered, setHasConsidered] = useState(false);
|
|
||||||
|
|
||||||
// Redirect to home if not logged in
|
|
||||||
if (!user) {
|
|
||||||
router.push('/');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleConsideration = () => {
|
|
||||||
incrementBurritoConsiderations();
|
|
||||||
setHasConsidered(true);
|
|
||||||
setTimeout(() => setHasConsidered(false), 2000);
|
|
||||||
|
|
||||||
// Capture burrito consideration event
|
|
||||||
posthog.capture('burrito_considered', {
|
|
||||||
total_considerations: user.burritoConsiderations + 1,
|
|
||||||
username: user.username,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Head>
|
|
||||||
<title>Burrito Consideration - Burrito Consideration App</title>
|
|
||||||
<meta name="description" content="Consider the potential of burritos" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link rel="icon" href="/favicon.ico" />
|
|
||||||
</Head>
|
|
||||||
<Header />
|
|
||||||
<main>
|
|
||||||
<div className="container">
|
|
||||||
<h1>Burrito consideration zone</h1>
|
|
||||||
<p>Take a moment to truly consider the potential of burritos.</p>
|
|
||||||
|
|
||||||
<div style={{ textAlign: 'center' }}>
|
|
||||||
<button
|
|
||||||
onClick={handleConsideration}
|
|
||||||
className="btn-burrito"
|
|
||||||
>
|
|
||||||
I have considered the burrito potential
|
|
||||||
</button>
|
|
||||||
|
|
||||||
{hasConsidered && (
|
|
||||||
<p className="success">
|
|
||||||
Thank you for your consideration! Count: {user.burritoConsiderations}
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="stats">
|
|
||||||
<h3>Consideration stats</h3>
|
|
||||||
<p>Total considerations: {user.burritoConsiderations}</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/pages/index.tsx
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import { useState } from 'react';
|
|
||||||
import Head from 'next/head';
|
|
||||||
import { useAuth } from '@/contexts/AuthContext';
|
|
||||||
import Header from '@/components/Header';
|
|
||||||
|
|
||||||
export default function Home() {
|
|
||||||
const { user, login } = useAuth();
|
|
||||||
const [username, setUsername] = useState('');
|
|
||||||
const [password, setPassword] = useState('');
|
|
||||||
const [error, setError] = useState('');
|
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const success = await login(username, password);
|
|
||||||
if (success) {
|
|
||||||
setUsername('');
|
|
||||||
setPassword('');
|
|
||||||
} else {
|
|
||||||
setError('Please provide both username and password');
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Login failed:', err);
|
|
||||||
setError('An error occurred during login');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Head>
|
|
||||||
<title>Burrito Consideration App</title>
|
|
||||||
<meta name="description" content="Consider the potential of burritos" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link rel="icon" href="/favicon.ico" />
|
|
||||||
</Head>
|
|
||||||
<Header />
|
|
||||||
<main>
|
|
||||||
{user ? (
|
|
||||||
<div className="container">
|
|
||||||
<h1>Welcome back, {user.username}!</h1>
|
|
||||||
<p>You are now logged in. Feel free to explore:</p>
|
|
||||||
<ul>
|
|
||||||
<li>Consider the potential of burritos</li>
|
|
||||||
<li>View your profile and statistics</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<div className="container">
|
|
||||||
<h1>Welcome to Burrito Consideration App</h1>
|
|
||||||
<p>Please sign in to begin your burrito journey</p>
|
|
||||||
|
|
||||||
<form onSubmit={handleSubmit} className="form">
|
|
||||||
<div className="form-group">
|
|
||||||
<label htmlFor="username">Username:</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
id="username"
|
|
||||||
value={username}
|
|
||||||
onChange={(e) => setUsername(e.target.value)}
|
|
||||||
placeholder="Enter any username"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="form-group">
|
|
||||||
<label htmlFor="password">Password:</label>
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
id="password"
|
|
||||||
value={password}
|
|
||||||
onChange={(e) => setPassword(e.target.value)}
|
|
||||||
placeholder="Enter any password"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{error && <p className="error">{error}</p>}
|
|
||||||
|
|
||||||
<button type="submit" className="btn-primary">Sign In</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<p className="note">
|
|
||||||
Note: This is a demo app. Use any username and password to sign in.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</main>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## src/pages/profile.tsx
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import Head from 'next/head';
|
|
||||||
import { useRouter } from 'next/router';
|
|
||||||
import posthog from 'posthog-js';
|
|
||||||
import { useAuth } from '@/contexts/AuthContext';
|
|
||||||
import Header from '@/components/Header';
|
|
||||||
|
|
||||||
export default function ProfilePage() {
|
|
||||||
const { user } = useAuth();
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
// Redirect to home if not logged in
|
|
||||||
if (!user) {
|
|
||||||
router.push('/');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const triggerTestError = () => {
|
|
||||||
try {
|
|
||||||
throw new Error('Test error for PostHog error tracking');
|
|
||||||
} catch (err) {
|
|
||||||
posthog.captureException(err);
|
|
||||||
console.error('Captured error:', err);
|
|
||||||
alert('Error captured and sent to PostHog!');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Head>
|
|
||||||
<title>Profile - Burrito Consideration App</title>
|
|
||||||
<meta name="description" content="Your burrito consideration profile" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link rel="icon" href="/favicon.ico" />
|
|
||||||
</Head>
|
|
||||||
<Header />
|
|
||||||
<main>
|
|
||||||
<div className="container">
|
|
||||||
<h1>User Profile</h1>
|
|
||||||
|
|
||||||
<div className="stats">
|
|
||||||
<h2>Your Information</h2>
|
|
||||||
<p><strong>Username:</strong> {user.username}</p>
|
|
||||||
<p><strong>Burrito Considerations:</strong> {user.burritoConsiderations}</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div style={{ marginTop: '2rem' }}>
|
|
||||||
<button onClick={triggerTestError} className="btn-primary" style={{ backgroundColor: '#dc3545' }}>
|
|
||||||
Trigger Test Error (for PostHog)
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div style={{ marginTop: '2rem' }}>
|
|
||||||
<h3>Your Burrito Journey</h3>
|
|
||||||
{user.burritoConsiderations === 0 ? (
|
|
||||||
<p>You haven't considered any burritos yet. Visit the Burrito Consideration page to start!</p>
|
|
||||||
) : user.burritoConsiderations === 1 ? (
|
|
||||||
<p>You've considered the burrito potential once. Keep going!</p>
|
|
||||||
) : user.burritoConsiderations < 5 ? (
|
|
||||||
<p>You're getting the hang of burrito consideration!</p>
|
|
||||||
) : user.burritoConsiderations < 10 ? (
|
|
||||||
<p>You're becoming a burrito consideration expert!</p>
|
|
||||||
) : (
|
|
||||||
<p>You are a true burrito consideration master! 🌯</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
-43
@@ -1,43 +0,0 @@
|
|||||||
---
|
|
||||||
title: PostHog Setup - Begin
|
|
||||||
description: Start the event tracking setup process by analyzing the project and creating an event tracking plan
|
|
||||||
---
|
|
||||||
|
|
||||||
We're making an event tracking plan for this project.
|
|
||||||
|
|
||||||
Before proceeding, find any existing `posthog.capture()` code. Make note of event name formatting.
|
|
||||||
|
|
||||||
From the project's file list, select between 10 and 15 files that might have interesting business value for event tracking, especially conversion and churn events. Also look for additional files related to login that could be used for identifying users, along with error handling. Read the files. If a file is already well-covered by PostHog events, replace it with another option. Do not spawn subagents.
|
|
||||||
|
|
||||||
Look for opportunities to track client-side events.
|
|
||||||
|
|
||||||
**IMPORTANT: Server-side events are REQUIRED** if the project includes any instrumentable server-side code. If the project has API routes (e.g., `app/api/**/route.ts`) or Server Actions, you MUST include server-side events for critical business operations like:
|
|
||||||
|
|
||||||
- Payment/checkout completion
|
|
||||||
- Webhook handlers
|
|
||||||
- Authentication endpoints
|
|
||||||
|
|
||||||
Do not skip server-side events - they capture actions that cannot be tracked client-side.
|
|
||||||
|
|
||||||
Create a new file with a JSON array at the root of the project: .posthog-events.json. It should include one object for each event we want to add: event name, event description, and the file path we want to place the event in. If events already exist, don't duplicate them; supplement them.
|
|
||||||
|
|
||||||
Track actions only, not pageviews. These can be captured automatically. Exceptions can be made for "viewed"-type events that correspond to the top of a conversion funnel.
|
|
||||||
|
|
||||||
As you review files, make an internal note of opportunities to identify users and catch errors. We'll need them for the next step.
|
|
||||||
|
|
||||||
## Status
|
|
||||||
|
|
||||||
Before beginning a phase of the setup, you will send a status message with the exact prefix '[STATUS]', as in:
|
|
||||||
|
|
||||||
[STATUS] Checking project structure.
|
|
||||||
|
|
||||||
Status to report in this phase:
|
|
||||||
|
|
||||||
- Checking project structure
|
|
||||||
- Verifying PostHog dependencies
|
|
||||||
- Generating events based on project
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Upon completion, continue with:** [basic-integration-1.1-edit.md](basic-integration-1.1-edit.md)
|
|
||||||
-37
@@ -1,37 +0,0 @@
|
|||||||
---
|
|
||||||
title: PostHog Setup - Edit
|
|
||||||
description: Implement PostHog event tracking in the identified files, following best practices and the example project
|
|
||||||
---
|
|
||||||
|
|
||||||
For each of the files and events noted in .posthog-events.json, make edits to capture events using PostHog. Make sure to set up any helper files needed. Carefully examine the included example project code: your implementation should match it as closely as possible. Do not spawn subagents.
|
|
||||||
|
|
||||||
Use environment variables for PostHog keys. Do not hardcode PostHog keys.
|
|
||||||
|
|
||||||
If a file already has existing integration code for other tools or services, don't overwrite or remove that code. Place PostHog code below it.
|
|
||||||
|
|
||||||
For each event, add useful properties, and use your access to the PostHog source code to ensure correctness. You also have access to documentation about creating new events with PostHog. Consider this documentation carefully and follow it closely before adding events. Your integration should be based on documented best practices. Carefully consider how the user project's framework version may impact the correct PostHog integration approach.
|
|
||||||
|
|
||||||
Remember that you can find the source code for any dependency in the node_modules directory. This may be necessary to properly populate property names. There are also example project code files available via the PostHog MCP; use these for reference.
|
|
||||||
|
|
||||||
Where possible, add calls for PostHog's identify() function on the client side upon events like logins and signups. Use the contents of login and signup forms to identify users on submit. If there is server-side code, pass the client-side session and distinct ID to the server-side code to identify the user. On the server side, make sure events have a matching distinct ID where relevant.
|
|
||||||
|
|
||||||
It's essential to do this in both client code and server code, so that user behavior from both domains is easy to correlate.
|
|
||||||
|
|
||||||
You should also add PostHog exception capture error tracking to these files where relevant.
|
|
||||||
|
|
||||||
Remember: Do not alter the fundamental architecture of existing files. Make your additions minimal and targeted.
|
|
||||||
|
|
||||||
Remember the documentation and example project resources you were provided at the beginning. Read them now.
|
|
||||||
|
|
||||||
## Status
|
|
||||||
|
|
||||||
Status to report in this phase:
|
|
||||||
|
|
||||||
- Inserting PostHog capture code
|
|
||||||
- A status message for each file whose edits you are planning, including a high level summary of changes
|
|
||||||
- A status message for each file you have edited
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Upon completion, continue with:** [basic-integration-1.2-revise.md](basic-integration-1.2-revise.md)
|
|
||||||
-22
@@ -1,22 +0,0 @@
|
|||||||
---
|
|
||||||
title: PostHog Setup - Revise
|
|
||||||
description: Review and fix any errors in the PostHog integration implementation
|
|
||||||
---
|
|
||||||
|
|
||||||
Check the project for errors. Read the package.json file for any type checking or build scripts that may provide input about what to fix. Remember that you can find the source code for any dependency in the node_modules directory. Do not spawn subagents.
|
|
||||||
|
|
||||||
Ensure that any components created were actually used.
|
|
||||||
|
|
||||||
Once all other tasks are complete, run any linter or prettier-like scripts found in the package.json, but ONLY on the files you have edited or created during this session. Do not run formatting or linting across the entire project's codebase.
|
|
||||||
|
|
||||||
## Status
|
|
||||||
|
|
||||||
Status to report in this phase:
|
|
||||||
|
|
||||||
- Finding and correcting errors
|
|
||||||
- Report details of any errors you fix
|
|
||||||
- Linting, building and prettying
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Upon completion, continue with:** [basic-integration-1.3-conclude.md](basic-integration-1.3-conclude.md)
|
|
||||||
-38
@@ -1,38 +0,0 @@
|
|||||||
---
|
|
||||||
title: PostHog Setup - Conclusion
|
|
||||||
description: Review and fix any errors in the PostHog integration implementation
|
|
||||||
---
|
|
||||||
|
|
||||||
Use the PostHog MCP to create a new dashboard named "Analytics basics" based on the events created here. Make sure to use the exact same event names as implemented in the code. Populate it with up to five insights, with special emphasis on things like conversion funnels, churn events, and other business critical insights.
|
|
||||||
|
|
||||||
Search for a file called `.posthog-events.json` and read it for available events. Do not spawn subagents.
|
|
||||||
|
|
||||||
Create the file posthog-setup-report.md. It should include a summary of the integration edits, a table with the event names, event descriptions, and files where events were added, along with a list of links for the dashboard and insights created. Follow this format:
|
|
||||||
|
|
||||||
<wizard-report>
|
|
||||||
# PostHog post-wizard report
|
|
||||||
|
|
||||||
The wizard has completed a deep integration of your project. [Detailed summary of changes]
|
|
||||||
|
|
||||||
[table of events/descriptions/files]
|
|
||||||
|
|
||||||
## Next steps
|
|
||||||
|
|
||||||
We've built some insights and a dashboard for you to keep an eye on user behavior, based on the events we just instrumented:
|
|
||||||
|
|
||||||
[links]
|
|
||||||
|
|
||||||
### Agent skill
|
|
||||||
|
|
||||||
We've left an agent skill folder in your project. You can use this context for further agent development when using Claude Code. This will help ensure the model provides the most up-to-date approaches for integrating PostHog.
|
|
||||||
|
|
||||||
</wizard-report>
|
|
||||||
|
|
||||||
Upon completion, remove .posthog-events.json.
|
|
||||||
|
|
||||||
## Status
|
|
||||||
|
|
||||||
Status to report in this phase:
|
|
||||||
|
|
||||||
- Configured dashboard: [insert PostHog dashboard URL]
|
|
||||||
- Created setup report: [insert full local file path]
|
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
# Identify users - Docs
|
|
||||||
|
|
||||||
Linking events to specific users enables you to build a full picture of how they're using your product across different sessions, devices, and platforms.
|
|
||||||
|
|
||||||
This is straightforward to do when [capturing backend events](/docs/product-analytics/capture-events?tab=Node.js.md), as you associate events to a specific user using a `distinct_id`, which is a required argument.
|
|
||||||
|
|
||||||
However, in the frontend of a [web](/docs/libraries/js/features#capturing-events.md) or [mobile app](/docs/libraries/ios#capturing-events.md), a `distinct_id` is not a required argument — PostHog's SDKs will generate an anonymous `distinct_id` for you automatically and you can capture events anonymously, provided you use the appropriate [configuration](/docs/libraries/js/features#capturing-anonymous-events.md).
|
|
||||||
|
|
||||||
To link events to specific users, call `identify`:
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
### Web
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
posthog.identify(
|
|
||||||
'distinct_id', // Replace 'distinct_id' with your user's unique identifier
|
|
||||||
{ email: 'max@hedgehogmail.com', name: 'Max Hedgehog' } // optional: set additional person properties
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
### Android
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
PostHog.identify(
|
|
||||||
distinctId = distinctID, // Replace 'distinctID' with your user's unique identifier
|
|
||||||
// optional: set additional person properties
|
|
||||||
userProperties = mapOf(
|
|
||||||
"name" to "Max Hedgehog",
|
|
||||||
"email" to "max@hedgehogmail.com"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
### iOS
|
|
||||||
|
|
||||||
```swift
|
|
||||||
PostHogSDK.shared.identify("distinct_id", // Replace "distinct_id" with your user's unique identifier
|
|
||||||
userProperties: ["name": "Max Hedgehog", "email": "max@hedgehogmail.com"]) // optional: set additional person properties
|
|
||||||
```
|
|
||||||
|
|
||||||
### React Native
|
|
||||||
|
|
||||||
```jsx
|
|
||||||
posthog.identify('distinct_id', { // Replace "distinct_id" with your user's unique identifier
|
|
||||||
email: 'max@hedgehogmail.com', // optional: set additional person properties
|
|
||||||
name: 'Max Hedgehog'
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Dart
|
|
||||||
|
|
||||||
```dart
|
|
||||||
await Posthog().identify(
|
|
||||||
userId: 'distinct_id', // Replace "distinct_id" with your user's unique identifier
|
|
||||||
userProperties: {
|
|
||||||
email: "max@hedgehogmail.com", // optional: set additional person properties
|
|
||||||
name: "Max Hedgehog"
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
Events captured after calling `identify` are identified events and this creates a person profile if one doesn't exist already.
|
|
||||||
|
|
||||||
Due to the cost of processing them, anonymous events can be up to 4x cheaper than identified events, so it's recommended you only capture identified events when needed.
|
|
||||||
|
|
||||||
## How identify works
|
|
||||||
|
|
||||||
When a user starts browsing your website or app, PostHog automatically assigns them an **anonymous ID**, which is stored locally.
|
|
||||||
|
|
||||||
Provided you've [configured persistence](/docs/libraries/js/persistence.md) to use cookies or `localStorage`, this enables us to track anonymous users – even across different sessions.
|
|
||||||
|
|
||||||
By calling `identify` with a `distinct_id` of your choice (usually the user's ID in your database, or their email), you link the anonymous ID and distinct ID together.
|
|
||||||
|
|
||||||
Thus, all past and future events made with that anonymous ID are now associated with the distinct ID.
|
|
||||||
|
|
||||||
This enables you to do things like associate events with a user from before they log in for the first time, or associate their events across different devices or platforms.
|
|
||||||
|
|
||||||
Using identify in the backend
|
|
||||||
|
|
||||||
Although you can call `identify` using our backend SDKs, it is used most in frontends. This is because there is no concept of anonymous sessions in the backend SDKs, so calling `identify` only updates person profiles.
|
|
||||||
|
|
||||||
## Best practices when using `identify`
|
|
||||||
|
|
||||||
### 1\. Call `identify` as soon as you're able to
|
|
||||||
|
|
||||||
In your frontend, you should call `identify` as soon as you're able to.
|
|
||||||
|
|
||||||
Typically, this is every time your **app loads** for the first time, and directly after your **users log in**.
|
|
||||||
|
|
||||||
This ensures that events sent during your users' sessions are correctly associated with them.
|
|
||||||
|
|
||||||
You only need to call `identify` once per session, and you should avoid calling it multiple times unnecessarily.
|
|
||||||
|
|
||||||
If you call `identify` multiple times with the same data without reloading the page in between, PostHog will ignore the subsequent calls.
|
|
||||||
|
|
||||||
### 2\. Use unique strings for distinct IDs
|
|
||||||
|
|
||||||
If two users have the same distinct ID, their data is merged and they are considered one user in PostHog. Two common ways this can happen are:
|
|
||||||
|
|
||||||
- Your logic for generating IDs does not generate sufficiently strong IDs and you can end up with a clash where 2 users have the same ID.
|
|
||||||
- There's a bug, typo, or mistake in your code leading to most or all users being identified with generic IDs like `null`, `true`, or `distinctId`.
|
|
||||||
|
|
||||||
PostHog also has built-in protections to stop the most common distinct ID mistakes.
|
|
||||||
|
|
||||||
### 3\. Reset after logout
|
|
||||||
|
|
||||||
If a user logs out on your frontend, you should call `reset()` to unlink any future events made on that device with that user.
|
|
||||||
|
|
||||||
This is important if your users are sharing a computer, as otherwise all of those users are grouped together into a single user due to shared cookies between sessions.
|
|
||||||
|
|
||||||
**We strongly recommend you call `reset` on logout even if you don't expect users to share a computer.**
|
|
||||||
|
|
||||||
You can do that like so:
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
### Web
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
posthog.reset()
|
|
||||||
```
|
|
||||||
|
|
||||||
### iOS
|
|
||||||
|
|
||||||
```swift
|
|
||||||
PostHogSDK.shared.reset()
|
|
||||||
```
|
|
||||||
|
|
||||||
### Android
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
PostHog.reset()
|
|
||||||
```
|
|
||||||
|
|
||||||
### React Native
|
|
||||||
|
|
||||||
```jsx
|
|
||||||
posthog.reset()
|
|
||||||
```
|
|
||||||
|
|
||||||
### Dart
|
|
||||||
|
|
||||||
```dart
|
|
||||||
Posthog().reset()
|
|
||||||
```
|
|
||||||
|
|
||||||
If you *also* want to reset the `device_id` so that the device will be considered a new device in future events, you can pass `true` as an argument:
|
|
||||||
|
|
||||||
Web
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
posthog.reset(true)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 4\. Person profiles and properties
|
|
||||||
|
|
||||||
You'll notice that one of the parameters in the `identify` method is a `properties` object.
|
|
||||||
|
|
||||||
This enables you to set [person properties](/docs/product-analytics/person-properties.md).
|
|
||||||
|
|
||||||
Whenever possible, we recommend passing in all person properties you have available each time you call identify, as this ensures their person profile on PostHog is up to date.
|
|
||||||
|
|
||||||
Person properties can also be set being adding a `$set` property to a event `capture` call.
|
|
||||||
|
|
||||||
See our [person properties docs](/docs/product-analytics/person-properties.md) for more details on how to work with them and best practices.
|
|
||||||
|
|
||||||
### 5\. Use deep links between platforms
|
|
||||||
|
|
||||||
We recommend you call `identify` [as soon as you're able](#1-call-identify-as-soon-as-youre-able), typically when a user signs up or logs in.
|
|
||||||
|
|
||||||
This doesn't work if one or both platforms are unauthenticated. Some examples of such cases are:
|
|
||||||
|
|
||||||
- Onboarding and signup flows before authentication.
|
|
||||||
- Unauthenticated web pages redirecting to authenticated mobile apps.
|
|
||||||
- Authenticated web apps prompting an app download.
|
|
||||||
|
|
||||||
In these cases, you can use a [deep link](https://developer.android.com/training/app-links/deep-linking) on Android and [universal links](https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app) on iOS to identify users.
|
|
||||||
|
|
||||||
1. Use `posthog.get_distinct_id()` to get the current distinct ID. Even if you cannot call identify because the user is unauthenticated, this will return an anonymous distinct ID generated by PostHog.
|
|
||||||
2. Add the distinct ID to the deep link as query parameters, along with other properties like UTM parameters.
|
|
||||||
3. When the user is redirected to the app, parse the deep link and handle the following cases:
|
|
||||||
|
|
||||||
- The user is already authenticated on the mobile app. In this case, call [`posthog.alias()`](/docs/libraries/js/features#alias.md) with the distinct ID from the web. This associates the two distinct IDs as a single person.
|
|
||||||
- The user is unauthenticated. In this case, call [`posthog.identify()`](/docs/libraries/js/features#identifying-users.md) with the distinct ID from the web. Events will be associated with this distinct ID.
|
|
||||||
|
|
||||||
As long as you associate the distinct IDs with `posthog.identify()` or `posthog.alias()`, you can track events generated across platforms.
|
|
||||||
|
|
||||||
## Further reading
|
|
||||||
|
|
||||||
- [Identifying users docs](/docs/product-analytics/identify.md)
|
|
||||||
- [How person processing works](/docs/how-posthog-works/ingestion-pipeline#2-person-processing.md)
|
|
||||||
- [An introductory guide to identifying users in PostHog](/tutorials/identifying-users-guide.md)
|
|
||||||
|
|
||||||
### Community questions
|
|
||||||
|
|
||||||
Ask a question
|
|
||||||
|
|
||||||
### Was this page useful?
|
|
||||||
|
|
||||||
HelpfulCould be better
|
|
||||||
@@ -1,377 +0,0 @@
|
|||||||
# Next.js - Docs
|
|
||||||
|
|
||||||
PostHog makes it easy to get data about traffic and usage of your [Next.js](https://nextjs.org/) app. Integrating PostHog into your site enables analytics about user behavior, custom events capture, session recordings, feature flags, and more.
|
|
||||||
|
|
||||||
This guide walks you through integrating PostHog into your Next.js app using the [React](/docs/libraries/react.md) and the [Node.js](/docs/libraries/node.md) SDKs.
|
|
||||||
|
|
||||||
> You can see a working example of this integration in our [Next.js demo app](https://github.com/PostHog/posthog-js/tree/main/playground/nextjs).
|
|
||||||
|
|
||||||
Next.js has both client and server-side rendering, as well as pages and app routers. We'll cover all of these options in this guide.
|
|
||||||
|
|
||||||
## Prerequisites
|
|
||||||
|
|
||||||
To follow this guide along, you need:
|
|
||||||
|
|
||||||
1. A PostHog instance (either [Cloud](https://app.posthog.com/signup) or [self-hosted](/docs/self-host.md))
|
|
||||||
2. A Next.js application
|
|
||||||
|
|
||||||
## Beta: integration via LLM
|
|
||||||
|
|
||||||
Install PostHog for Next.js in seconds with our wizard by running this prompt with [LLM coding agents](/blog/envoy-wizard-llm-agent.md) like Cursor and Bolt, or by running it in your terminal.
|
|
||||||
|
|
||||||
`npx @posthog/wizard@latest`
|
|
||||||
|
|
||||||
[Learn more](/wizard.md)
|
|
||||||
|
|
||||||
Or, to integrate manually, continue with the rest of this guide.
|
|
||||||
|
|
||||||
## Client-side setup
|
|
||||||
|
|
||||||
Install `posthog-js` using your package manager:
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
### npm
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install --save posthog-js
|
|
||||||
```
|
|
||||||
|
|
||||||
### Yarn
|
|
||||||
|
|
||||||
```bash
|
|
||||||
yarn add posthog-js
|
|
||||||
```
|
|
||||||
|
|
||||||
### pnpm
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pnpm add posthog-js
|
|
||||||
```
|
|
||||||
|
|
||||||
### Bun
|
|
||||||
|
|
||||||
```bash
|
|
||||||
bun add posthog-js
|
|
||||||
```
|
|
||||||
|
|
||||||
Add your environment variables to your `.env.local` file and to your hosting provider (e.g. Vercel, Netlify, AWS). You can find your project token in your [project settings](https://app.posthog.com/project/settings).
|
|
||||||
|
|
||||||
.env.local
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
```shell
|
|
||||||
NEXT_PUBLIC_POSTHOG_TOKEN=<ph_project_token>
|
|
||||||
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
|
|
||||||
```
|
|
||||||
|
|
||||||
These values need to start with `NEXT_PUBLIC_` to be accessible on the client-side.
|
|
||||||
|
|
||||||
## Integration
|
|
||||||
|
|
||||||
Next.js provides the [`instrumentation-client.ts|js`](https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation-client) file for client-side setup. Add it to the root of your Next.js app (for both app and pages router) and initialize PostHog in it like this:
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
### instrumentation-client.js
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import posthog from 'posthog-js'
|
|
||||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_TOKEN, {
|
|
||||||
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
|
||||||
defaults: '2026-01-30'
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### instrumentation-client.ts
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import posthog from 'posthog-js'
|
|
||||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_TOKEN!, {
|
|
||||||
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
|
||||||
defaults: '2026-01-30'
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
Bootstrapping with `instrumentation-client`
|
|
||||||
|
|
||||||
When using `instrumentation-client`, the values you pass to `posthog.init` remain fixed for the entire session. This means bootstrapping only works if you evaluate flags **before your app renders** (for example, on the server).
|
|
||||||
|
|
||||||
If you need flag values after the app has rendered, you’ll want to:
|
|
||||||
|
|
||||||
- Evaluate the flag on the server and pass the value into your app, or
|
|
||||||
- Evaluate the flag in an earlier page/state, then store and re-use it when needed.
|
|
||||||
|
|
||||||
Both approaches avoid flicker and give you the same outcome as bootstrapping, as long as you use the same `distinct_id` across client and server.
|
|
||||||
|
|
||||||
See the [bootstrapping guide](/docs/feature-flags/bootstrapping.md) for more information.
|
|
||||||
|
|
||||||
Set up a reverse proxy (recommended)
|
|
||||||
|
|
||||||
We recommend [setting up a reverse proxy](/docs/advanced/proxy.md), so that events are less likely to be intercepted by tracking blockers.
|
|
||||||
|
|
||||||
We have our [own managed reverse proxy service](/docs/advanced/proxy/managed-reverse-proxy.md), which is free for all PostHog Cloud users, routes through our infrastructure, and makes setting up your proxy easy.
|
|
||||||
|
|
||||||
If you don't want to use our managed service then there are several other options for creating a reverse proxy, including using [Cloudflare](/docs/advanced/proxy/cloudflare.md), [AWS Cloudfront](/docs/advanced/proxy/cloudfront.md), and [Vercel](/docs/advanced/proxy/vercel.md).
|
|
||||||
|
|
||||||
Grouping products in one project (recommended)
|
|
||||||
|
|
||||||
If you have multiple customer-facing products (e.g. a marketing website + mobile app + web app), it's best to install PostHog on them all and [group them in one project](/docs/settings/projects.md).
|
|
||||||
|
|
||||||
This makes it possible to track users across their entire journey (e.g. from visiting your marketing website to signing up for your product), or how they use your product across multiple platforms.
|
|
||||||
|
|
||||||
Add IPs to Firewall/WAF allowlists (recommended)
|
|
||||||
|
|
||||||
For certain features like [heatmaps](/docs/toolbar/heatmaps.md), your Web Application Firewall (WAF) may be blocking PostHog’s requests to your site. Add these IP addresses to your WAF allowlist or rules to let PostHog access your site.
|
|
||||||
|
|
||||||
**EU**: `3.75.65.221`, `18.197.246.42`, `3.120.223.253`
|
|
||||||
|
|
||||||
**US**: `44.205.89.55`, `52.4.194.122`, `44.208.188.173`
|
|
||||||
|
|
||||||
These are public, stable IPs used by PostHog services (e.g., Celery tasks for snapshots).
|
|
||||||
|
|
||||||
## Accessing PostHog
|
|
||||||
|
|
||||||
Once initialized in `instrumentation-client.js|ts`, import `posthog` from `posthog-js` anywhere and call the methods you need on the `posthog` object.
|
|
||||||
|
|
||||||
JavaScript
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
'use client'
|
|
||||||
import posthog from 'posthog-js'
|
|
||||||
export default function Home() {
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<button onClick={() => posthog.capture('test_event')}>
|
|
||||||
Click me for an event
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using React hooks
|
|
||||||
|
|
||||||
The [React feature flag hooks](/docs/libraries/react#feature-flags.md) work automatically when PostHog is initialized via `instrumentation-client.ts`. The hooks use the initialized posthog-js singleton:
|
|
||||||
|
|
||||||
JavaScript
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
'use client'
|
|
||||||
import { useFeatureFlagEnabled } from 'posthog-js/react'
|
|
||||||
export default function FeatureComponent() {
|
|
||||||
const showNewFeature = useFeatureFlagEnabled('new-feature')
|
|
||||||
return showNewFeature ? <NewFeature /> : <OldFeature />
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Usage
|
|
||||||
|
|
||||||
See the [React SDK docs](/docs/libraries/react.md) for examples of how to use:
|
|
||||||
|
|
||||||
- [`posthog-js` functions like custom event capture, user identification, and more.](/docs/libraries/react#using-posthog-js-functions.md)
|
|
||||||
- [Feature flags including variants and payloads.](/docs/libraries/react#feature-flags.md)
|
|
||||||
|
|
||||||
You can also read [the full `posthog-js` documentation](/docs/libraries/js/features.md) for all the usable functions.
|
|
||||||
|
|
||||||
## Server-side analytics
|
|
||||||
|
|
||||||
Next.js enables you to both server-side render pages and add server-side functionality. To integrate PostHog into your Next.js app on the server-side, you can use the [Node SDK](/docs/libraries/node.md).
|
|
||||||
|
|
||||||
First, install the `posthog-node` library:
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
### npm
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install posthog-node --save
|
|
||||||
```
|
|
||||||
|
|
||||||
### Yarn
|
|
||||||
|
|
||||||
```bash
|
|
||||||
yarn add posthog-node
|
|
||||||
```
|
|
||||||
|
|
||||||
### pnpm
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pnpm add posthog-node
|
|
||||||
```
|
|
||||||
|
|
||||||
### Bun
|
|
||||||
|
|
||||||
```bash
|
|
||||||
bun add posthog-node
|
|
||||||
```
|
|
||||||
|
|
||||||
### Router-specific instructions
|
|
||||||
|
|
||||||
## App router
|
|
||||||
|
|
||||||
For the app router, we can initialize the `posthog-node` SDK once with a `PostHogClient` function, and import it into files.
|
|
||||||
|
|
||||||
This enables us to send events and fetch data from PostHog on the server – without making client-side requests.
|
|
||||||
|
|
||||||
JavaScript
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
// app/posthog.js
|
|
||||||
import { PostHog } from 'posthog-node'
|
|
||||||
export default function PostHogClient() {
|
|
||||||
const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_TOKEN, {
|
|
||||||
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
|
||||||
flushAt: 1,
|
|
||||||
flushInterval: 0
|
|
||||||
})
|
|
||||||
return posthogClient
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
> **Note:** Because server-side functions in Next.js can be short-lived, we set `flushAt` to `1` and `flushInterval` to `0`.
|
|
||||||
>
|
|
||||||
> - `flushAt` sets how many capture calls we should flush the queue (in one batch).
|
|
||||||
> - `flushInterval` sets how many milliseconds we should wait before flushing the queue. Setting them to the lowest number ensures events are sent immediately and not batched. We also need to call `await posthog.shutdown()` once done.
|
|
||||||
|
|
||||||
To use this client, we import it into our pages and call it with the `PostHogClient` function:
|
|
||||||
|
|
||||||
JavaScript
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import Link from 'next/link'
|
|
||||||
import PostHogClient from '../posthog'
|
|
||||||
export default async function About() {
|
|
||||||
const posthog = PostHogClient()
|
|
||||||
const flags = await posthog.getAllFlags(
|
|
||||||
'user_distinct_id' // replace with a user's distinct ID
|
|
||||||
);
|
|
||||||
await posthog.shutdown()
|
|
||||||
return (
|
|
||||||
<main>
|
|
||||||
<h1>About</h1>
|
|
||||||
<Link href="/">Go home</Link>
|
|
||||||
{ flags['main-cta'] &&
|
|
||||||
<Link href="http://posthog.com/">Go to PostHog</Link>
|
|
||||||
}
|
|
||||||
</main>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Pages router
|
|
||||||
|
|
||||||
For the pages router, we can use the `getServerSideProps` function to access PostHog on the server-side, send events, evaluate feature flags, and more.
|
|
||||||
|
|
||||||
This looks like this:
|
|
||||||
|
|
||||||
JavaScript
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
// pages/posts/[id].js
|
|
||||||
import { useContext, useEffect, useState } from 'react'
|
|
||||||
import { getServerSession } from "next-auth/next"
|
|
||||||
import { PostHog } from 'posthog-node'
|
|
||||||
export default function Post({ post, flags }) {
|
|
||||||
const [ctaState, setCtaState] = useState()
|
|
||||||
useEffect(() => {
|
|
||||||
if (flags) {
|
|
||||||
setCtaState(flags['blog-cta'])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h1>{post.title}</h1>
|
|
||||||
<p>By: {post.author}</p>
|
|
||||||
<p>{post.content}</p>
|
|
||||||
{ctaState &&
|
|
||||||
<p><a href="/">Go to PostHog</a></p>
|
|
||||||
}
|
|
||||||
<button onClick={likePost}>Like</button>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
export async function getServerSideProps(ctx) {
|
|
||||||
const session = await getServerSession(ctx.req, ctx.res)
|
|
||||||
let flags = null
|
|
||||||
if (session) {
|
|
||||||
const client = new PostHog(
|
|
||||||
process.env.NEXT_PUBLIC_POSTHOG_TOKEN,
|
|
||||||
{
|
|
||||||
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
flags = await client.getAllFlags(session.user.email);
|
|
||||||
client.capture({
|
|
||||||
distinctId: session.user.email,
|
|
||||||
event: 'loaded blog article',
|
|
||||||
properties: {
|
|
||||||
$current_url: ctx.req.url,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
await client.shutdown()
|
|
||||||
}
|
|
||||||
const { posts } = await import('../../blog.json')
|
|
||||||
const post = posts.find((post) => post.id.toString() === ctx.params.id)
|
|
||||||
return {
|
|
||||||
props: {
|
|
||||||
post,
|
|
||||||
flags
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
> **Note**: Make sure to *always* call `await client.shutdown()` after sending events from the server-side. PostHog queues events into larger batches, and this call forces all batched events to be flushed immediately.
|
|
||||||
|
|
||||||
### Server-side configuration
|
|
||||||
|
|
||||||
Next.js overrides the default `fetch` behavior on the server to introduce their own cache. PostHog ignores that cache by default, as this is Next.js's default behavior for any fetch call.
|
|
||||||
|
|
||||||
You can override that configuration when initializing PostHog, but make sure you understand the pros/cons of using Next.js's cache and that you might get cached results rather than the actual result our server would return. This is important for feature flags, for example.
|
|
||||||
|
|
||||||
TSX
|
|
||||||
|
|
||||||
PostHog AI
|
|
||||||
|
|
||||||
```jsx
|
|
||||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_TOKEN, {
|
|
||||||
// ... your configuration
|
|
||||||
fetch_options: {
|
|
||||||
cache: 'force-cache', // Use Next.js cache
|
|
||||||
next_options: { // Passed to the `next` option for `fetch`
|
|
||||||
revalidate: 60, // Cache for 60 seconds
|
|
||||||
tags: ['posthog'], // Can be used with Next.js `revalidateTag` function
|
|
||||||
},
|
|
||||||
}
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Configuring a reverse proxy to PostHog
|
|
||||||
|
|
||||||
To improve the reliability of client-side tracking and make requests less likely to be intercepted by tracking blockers, you can setup a reverse proxy in Next.js. Read more about deploying a reverse proxy using [Next.js rewrites](/docs/advanced/proxy/nextjs.md), [Next.js middleware](/docs/advanced/proxy/nextjs-middleware.md), and [Vercel rewrites](/docs/advanced/proxy/vercel.md).
|
|
||||||
|
|
||||||
## Further reading
|
|
||||||
|
|
||||||
- [How to set up Next.js analytics, feature flags, and more](/tutorials/nextjs-analytics.md)
|
|
||||||
- [How to set up Next.js pages router analytics, feature flags, and more](/tutorials/nextjs-pages-analytics.md)
|
|
||||||
- [How to set up Next.js A/B tests](/tutorials/nextjs-ab-tests.md)
|
|
||||||
|
|
||||||
### Community questions
|
|
||||||
|
|
||||||
Ask a question
|
|
||||||
|
|
||||||
### Was this page useful?
|
|
||||||
|
|
||||||
HelpfulCould be better
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# THIS IS AUTOGENERATED. DO NOT EDIT MANUALLY
|
||||||
|
version = 1
|
||||||
|
name = "formbricks"
|
||||||
|
|
||||||
|
[setup]
|
||||||
|
script = '''
|
||||||
|
pnpm install
|
||||||
|
pnpm dev:setup
|
||||||
|
'''
|
||||||
+69
-1
@@ -38,6 +38,15 @@ LOG_LEVEL=info
|
|||||||
|
|
||||||
DATABASE_URL='postgresql://postgres:postgres@localhost:5432/formbricks?schema=public'
|
DATABASE_URL='postgresql://postgres:postgres@localhost:5432/formbricks?schema=public'
|
||||||
|
|
||||||
|
#################
|
||||||
|
# HUB (DEV) #
|
||||||
|
#################
|
||||||
|
# The dev stack (pnpm db:up / pnpm go) runs Formbricks Hub on port 8080.
|
||||||
|
# Set explicitly to avoid confusion; override as needed when using docker-compose.dev.yml.
|
||||||
|
HUB_API_KEY=dev-api-key
|
||||||
|
HUB_API_URL=http://localhost:8080
|
||||||
|
HUB_DATABASE_URL=postgresql://postgres:postgres@postgres:5432/postgres?sslmode=disable
|
||||||
|
|
||||||
################
|
################
|
||||||
# MAIL SETUP #
|
# MAIL SETUP #
|
||||||
################
|
################
|
||||||
@@ -94,6 +103,12 @@ EMAIL_VERIFICATION_DISABLED=1
|
|||||||
# Password Reset. If you enable Password Reset functionality you have to setup SMTP-Settings, too.
|
# Password Reset. If you enable Password Reset functionality you have to setup SMTP-Settings, too.
|
||||||
PASSWORD_RESET_DISABLED=1
|
PASSWORD_RESET_DISABLED=1
|
||||||
|
|
||||||
|
# Password reset token lifetime in minutes. Must be between 5 and 120 if set.
|
||||||
|
# PASSWORD_RESET_TOKEN_LIFETIME_MINUTES=30
|
||||||
|
|
||||||
|
# Development-only helper: log the password reset link to the server console instead of sending reset emails.
|
||||||
|
# DEBUG_SHOW_RESET_LINK=1
|
||||||
|
|
||||||
# Email login. Disable the ability for users to login with email.
|
# Email login. Disable the ability for users to login with email.
|
||||||
# EMAIL_AUTH_DISABLED=1
|
# EMAIL_AUTH_DISABLED=1
|
||||||
|
|
||||||
@@ -132,6 +147,31 @@ AZUREAD_CLIENT_ID=
|
|||||||
AZUREAD_CLIENT_SECRET=
|
AZUREAD_CLIENT_SECRET=
|
||||||
AZUREAD_TENANT_ID=
|
AZUREAD_TENANT_ID=
|
||||||
|
|
||||||
|
# Configure Formbricks AI at the instance level
|
||||||
|
# Set the provider used for AI features on this instance.
|
||||||
|
# Accepted values for AI_PROVIDER: aws, gcp, azure
|
||||||
|
# Set AI_MODEL to the provider-specific model or deployment name and configure the matching credentials below.
|
||||||
|
# AI_PROVIDER=gcp
|
||||||
|
# AI_MODEL=gemini-2.5-flash
|
||||||
|
|
||||||
|
# Google Vertex AI credentials
|
||||||
|
# AI_GCP_PROJECT=
|
||||||
|
# AI_GCP_LOCATION=
|
||||||
|
# AI_GCP_CREDENTIALS_JSON=
|
||||||
|
# AI_GCP_APPLICATION_CREDENTIALS=
|
||||||
|
|
||||||
|
# Amazon Bedrock credentials
|
||||||
|
# AI_AWS_REGION=
|
||||||
|
# AI_AWS_ACCESS_KEY_ID=
|
||||||
|
# AI_AWS_SECRET_ACCESS_KEY=
|
||||||
|
# AI_AWS_SESSION_TOKEN=
|
||||||
|
|
||||||
|
# Azure AI / Microsoft Foundry credentials
|
||||||
|
# AI_AZURE_BASE_URL=
|
||||||
|
# AI_AZURE_RESOURCE_NAME=
|
||||||
|
# AI_AZURE_API_KEY=
|
||||||
|
# AI_AZURE_API_VERSION=v1
|
||||||
|
|
||||||
# OpenID Connect (OIDC) configuration
|
# OpenID Connect (OIDC) configuration
|
||||||
# OIDC_CLIENT_ID=
|
# OIDC_CLIENT_ID=
|
||||||
# OIDC_CLIENT_SECRET=
|
# OIDC_CLIENT_SECRET=
|
||||||
@@ -150,6 +190,7 @@ NOTION_OAUTH_CLIENT_ID=
|
|||||||
NOTION_OAUTH_CLIENT_SECRET=
|
NOTION_OAUTH_CLIENT_SECRET=
|
||||||
|
|
||||||
# Stripe Billing Variables
|
# Stripe Billing Variables
|
||||||
|
STRIPE_PUBLISHABLE_KEY=
|
||||||
STRIPE_SECRET_KEY=
|
STRIPE_SECRET_KEY=
|
||||||
STRIPE_WEBHOOK_SECRET=
|
STRIPE_WEBHOOK_SECRET=
|
||||||
|
|
||||||
@@ -184,6 +225,14 @@ ENTERPRISE_LICENSE_KEY=
|
|||||||
# Ignore Rate Limiting across the Formbricks app
|
# Ignore Rate Limiting across the Formbricks app
|
||||||
# RATE_LIMITING_DISABLED=1
|
# RATE_LIMITING_DISABLED=1
|
||||||
|
|
||||||
|
# Disable telemetry reporting (usage stats sent to Formbricks). Ignored when an EE license is active.
|
||||||
|
# TELEMETRY_DISABLED=1
|
||||||
|
|
||||||
|
# Allow webhook URLs to point to internal/private network addresses (e.g. localhost, 192.168.x.x)
|
||||||
|
# WARNING: Only enable this if you understand the SSRF risks. Useful for self-hosted instances
|
||||||
|
# that need to send webhooks to internal services.
|
||||||
|
# DANGEROUSLY_ALLOW_WEBHOOK_INTERNAL_URLS=1
|
||||||
|
|
||||||
# OpenTelemetry OTLP endpoint (base URL, exporters append /v1/traces and /v1/metrics)
|
# OpenTelemetry OTLP endpoint (base URL, exporters append /v1/traces and /v1/metrics)
|
||||||
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
|
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
|
||||||
# OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
|
# OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
|
||||||
@@ -229,5 +278,24 @@ REDIS_URL=redis://localhost:6379
|
|||||||
# AUDIT_LOG_GET_USER_IP=0
|
# AUDIT_LOG_GET_USER_IP=0
|
||||||
|
|
||||||
|
|
||||||
|
# Cube.js Analytics (optional — only needed for the analytics/dashboard feature)
|
||||||
|
# Required when running the Cube service (docker-compose.dev.yml). Generate with: openssl rand -hex 32
|
||||||
|
# Use the same value for CUBEJS_API_TOKEN so the client can authenticate.
|
||||||
|
# CUBEJS_API_SECRET=
|
||||||
|
# URL where the Cube.js instance is running
|
||||||
|
# CUBEJS_API_URL=http://localhost:4000
|
||||||
|
# API token sent with each Cube.js request; must match CUBEJS_API_SECRET when CUBEJS_DEV_MODE is off
|
||||||
|
# CUBEJS_API_TOKEN=
|
||||||
|
#
|
||||||
|
# Cube connects to the Hub DB. When using docker-compose.dev.yml with the hub network,
|
||||||
|
# use the container name and internal port. Hub credentials: formbricks/formbricks_dev, db: hub
|
||||||
|
# CUBEJS_DB_HOST=formbricks_hub_postgres
|
||||||
|
# CUBEJS_DB_PORT=5432
|
||||||
|
# CUBEJS_DB_NAME=hub
|
||||||
|
# CUBEJS_DB_USER=formbricks
|
||||||
|
# CUBEJS_DB_PASS=formbricks_dev
|
||||||
|
#
|
||||||
|
# Alternative (when not on same Docker network): host.docker.internal and port 5433
|
||||||
|
|
||||||
# Lingo.dev API key for translation generation
|
# Lingo.dev API key for translation generation
|
||||||
LINGODOTDEV_API_KEY=your_api_key_here
|
LINGO_API_KEY=your_api_key_here
|
||||||
|
|||||||
@@ -285,12 +285,14 @@ runs:
|
|||||||
encryption_key=${{ env.DUMMY_ENCRYPTION_KEY }}
|
encryption_key=${{ env.DUMMY_ENCRYPTION_KEY }}
|
||||||
redis_url=${{ env.DUMMY_REDIS_URL }}
|
redis_url=${{ env.DUMMY_REDIS_URL }}
|
||||||
sentry_auth_token=${{ env.SENTRY_AUTH_TOKEN }}
|
sentry_auth_token=${{ env.SENTRY_AUTH_TOKEN }}
|
||||||
|
posthog_key=${{ env.POSTHOG_KEY }}
|
||||||
env:
|
env:
|
||||||
DEPOT_PROJECT_TOKEN: ${{ env.DEPOT_PROJECT_TOKEN }}
|
DEPOT_PROJECT_TOKEN: ${{ env.DEPOT_PROJECT_TOKEN }}
|
||||||
DUMMY_DATABASE_URL: ${{ env.DUMMY_DATABASE_URL }}
|
DUMMY_DATABASE_URL: ${{ env.DUMMY_DATABASE_URL }}
|
||||||
DUMMY_ENCRYPTION_KEY: ${{ env.DUMMY_ENCRYPTION_KEY }}
|
DUMMY_ENCRYPTION_KEY: ${{ env.DUMMY_ENCRYPTION_KEY }}
|
||||||
DUMMY_REDIS_URL: ${{ env.DUMMY_REDIS_URL }}
|
DUMMY_REDIS_URL: ${{ env.DUMMY_REDIS_URL }}
|
||||||
SENTRY_AUTH_TOKEN: ${{ env.SENTRY_AUTH_TOKEN }}
|
SENTRY_AUTH_TOKEN: ${{ env.SENTRY_AUTH_TOKEN }}
|
||||||
|
POSTHOG_KEY: ${{ env.POSTHOG_KEY }}
|
||||||
|
|
||||||
- name: Sign GHCR image (GHCR only)
|
- name: Sign GHCR image (GHCR only)
|
||||||
if: ${{ inputs.registry_type == 'ghcr' && (github.event_name == 'workflow_call' || github.event_name == 'release' || github.event_name == 'workflow_dispatch') }}
|
if: ${{ inputs.registry_type == 'ghcr' && (github.event_name == 'workflow_call' || github.event_name == 'release' || github.event_name == 'workflow_dispatch') }}
|
||||||
|
|||||||
@@ -92,3 +92,4 @@ jobs:
|
|||||||
DUMMY_ENCRYPTION_KEY: ${{ secrets.DUMMY_ENCRYPTION_KEY }}
|
DUMMY_ENCRYPTION_KEY: ${{ secrets.DUMMY_ENCRYPTION_KEY }}
|
||||||
DUMMY_REDIS_URL: ${{ secrets.DUMMY_REDIS_URL }}
|
DUMMY_REDIS_URL: ${{ secrets.DUMMY_REDIS_URL }}
|
||||||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
||||||
|
POSTHOG_KEY: ${{ secrets.POSTHOG_KEY }}
|
||||||
|
|||||||
+1
-2
@@ -45,7 +45,7 @@ yarn-error.log*
|
|||||||
.direnv
|
.direnv
|
||||||
|
|
||||||
# Playwright
|
# Playwright
|
||||||
/test-results/
|
**/test-results/
|
||||||
/playwright-report/
|
/playwright-report/
|
||||||
/blob-report/
|
/blob-report/
|
||||||
/playwright/.cache/
|
/playwright/.cache/
|
||||||
@@ -66,4 +66,3 @@ i18n.cache
|
|||||||
stats.html
|
stats.html
|
||||||
# next-agents-md
|
# next-agents-md
|
||||||
.next-docs/
|
.next-docs/
|
||||||
.env
|
|
||||||
|
|||||||
+13
-1
@@ -1 +1,13 @@
|
|||||||
pnpm lint-staged
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
if command -v pnpm >/dev/null 2>&1; then
|
||||||
|
pnpm lint-staged
|
||||||
|
elif command -v npm >/dev/null 2>&1; then
|
||||||
|
npm exec --yes pnpm@10.32.1 lint-staged
|
||||||
|
elif command -v corepack >/dev/null 2>&1; then
|
||||||
|
corepack pnpm lint-staged
|
||||||
|
else
|
||||||
|
echo "Error: pnpm, npm, and corepack are unavailable in this Git hook PATH."
|
||||||
|
echo "Install Node.js tooling or update your PATH, then retry the commit."
|
||||||
|
exit 127
|
||||||
|
fi
|
||||||
@@ -32,6 +32,7 @@ The `@formbricks/surveys` package is pre-compiled (Vite → UMD + ESM) and the b
|
|||||||
|
|
||||||
TypeScript, React, and Prisma are the primary languages. Use the shared ESLint presets (`@formbricks/eslint-config`) and Prettier preset (110-char width, semicolons, double quotes, sorted import groups). Two-space indentation is standard; prefer `PascalCase` for React components and folders under `modules/`, `camelCase` for functions/variables, and `SCREAMING_SNAKE_CASE` only for constants. When adding mocks, place them inside `__mocks__` so import ordering stays stable.
|
TypeScript, React, and Prisma are the primary languages. Use the shared ESLint presets (`@formbricks/eslint-config`) and Prettier preset (110-char width, semicolons, double quotes, sorted import groups). Two-space indentation is standard; prefer `PascalCase` for React components and folders under `modules/`, `camelCase` for functions/variables, and `SCREAMING_SNAKE_CASE` only for constants. When adding mocks, place them inside `__mocks__` so import ordering stays stable.
|
||||||
We are using SonarQube to identify code smells and security hotspots.
|
We are using SonarQube to identify code smells and security hotspots.
|
||||||
|
Always mark React component props as `Readonly<>` (e.g., `({ children }: Readonly<MyProps>)`).
|
||||||
|
|
||||||
## Architecture & Patterns
|
## Architecture & Patterns
|
||||||
|
|
||||||
@@ -52,6 +53,14 @@ We are using SonarQube to identify code smells and security hotspots.
|
|||||||
- Translations are in `apps/web/locales/`. Default is `en-US.json`.
|
- Translations are in `apps/web/locales/`. Default is `en-US.json`.
|
||||||
- Lingo.dev is automatically translating strings from en-US into other languages on commit. Run `pnpm i18n` to generate missing translations and validate keys.
|
- Lingo.dev is automatically translating strings from en-US into other languages on commit. Run `pnpm i18n` to generate missing translations and validate keys.
|
||||||
|
|
||||||
|
## Date and Time Rendering
|
||||||
|
|
||||||
|
- All user-facing dates and times must use shared formatting helpers instead of ad hoc `date-fns`, `Intl`, or `toLocale*` calls in components.
|
||||||
|
- Locale for display must come from the app language source of truth (`user.locale`, `getLocale()`, or `i18n.resolvedLanguage`), not browser defaults or implicit `undefined` locale behavior.
|
||||||
|
- Locale and time zone are different concerns: locale controls formatting, time zone controls the represented clock/calendar moment.
|
||||||
|
- Never infer a time zone from locale. If a product-level time zone source of truth exists, use it explicitly; otherwise preserve the existing semantic meaning of the stored value and avoid introducing browser-dependent conversions.
|
||||||
|
- Machine-facing values for storage, APIs, exports, integrations, and logs must remain stable and non-localized (`ISO 8601` / UTC where applicable).
|
||||||
|
|
||||||
## Database & Prisma Performance
|
## Database & Prisma Performance
|
||||||
|
|
||||||
- Multi-tenancy: All data must be scoped by Organization or Environment.
|
- Multi-tenancy: All data must be scoped by Organization or Environment.
|
||||||
|
|||||||
@@ -127,34 +127,10 @@ Formbricks has a hosted cloud offering with a generous free plan to get you up a
|
|||||||
|
|
||||||
Formbricks is available Open-Source under AGPLv3 license. You can host Formbricks on your own servers using Docker without a subscription.
|
Formbricks is available Open-Source under AGPLv3 license. You can host Formbricks on your own servers using Docker without a subscription.
|
||||||
|
|
||||||
If you opt for self-hosting Formbricks, here are a few options to consider:
|
|
||||||
|
|
||||||
#### Docker
|
#### Docker
|
||||||
|
|
||||||
To get started with self-hosting with Docker, take a look at our [self-hosting docs](https://formbricks.com/docs/self-hosting/deployment).
|
To get started with self-hosting with Docker, take a look at our [self-hosting docs](https://formbricks.com/docs/self-hosting/deployment).
|
||||||
|
|
||||||
#### Community-managed One Click Hosting
|
|
||||||
|
|
||||||
##### Railway
|
|
||||||
|
|
||||||
You can deploy Formbricks on [Railway](https://railway.app) using the button below.
|
|
||||||
|
|
||||||
[](https://railway.app/new/template/PPDzCd)
|
|
||||||
|
|
||||||
##### RepoCloud
|
|
||||||
|
|
||||||
Or you can also deploy Formbricks on [RepoCloud](https://repocloud.io) using the button below.
|
|
||||||
|
|
||||||
[](https://repocloud.io/details/?app_id=254)
|
|
||||||
|
|
||||||
##### Zeabur
|
|
||||||
|
|
||||||
Or you can also deploy Formbricks on [Zeabur](https://zeabur.com) using the button below.
|
|
||||||
|
|
||||||
[](https://zeabur.com/templates/G4TUJL)
|
|
||||||
|
|
||||||
<a id="development"></a>
|
|
||||||
|
|
||||||
## 👨💻 Development
|
## 👨💻 Development
|
||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
@@ -247,4 +223,4 @@ We currently do not offer Formbricks white-labeled. That means that we don't sel
|
|||||||
|
|
||||||
The Enterprise Edition allows us to fund the development of Formbricks sustainably. It guarantees that the free and open-source surveying infrastructure we're building will be around for decades to come.
|
The Enterprise Edition allows us to fund the development of Formbricks sustainably. It guarantees that the free and open-source surveying infrastructure we're building will be around for decades to come.
|
||||||
|
|
||||||
<p align="right"><a href="#top">🔼 Back to top</a></p>
|
<a id="readme-de"></a>
|
||||||
|
|||||||
@@ -12,18 +12,18 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@chromatic-com/storybook": "^5.0.1",
|
"@chromatic-com/storybook": "^5.0.1",
|
||||||
"@storybook/addon-a11y": "10.2.15",
|
"@storybook/addon-a11y": "10.2.17",
|
||||||
"@storybook/addon-links": "10.2.15",
|
"@storybook/addon-links": "10.2.17",
|
||||||
"@storybook/addon-onboarding": "10.2.15",
|
"@storybook/addon-onboarding": "10.2.17",
|
||||||
"@storybook/react-vite": "10.2.15",
|
"@storybook/react-vite": "10.2.17",
|
||||||
"@typescript-eslint/eslint-plugin": "8.56.1",
|
"@typescript-eslint/eslint-plugin": "8.57.0",
|
||||||
"@tailwindcss/vite": "4.2.1",
|
"@tailwindcss/vite": "4.2.1",
|
||||||
"@typescript-eslint/parser": "8.56.1",
|
"@typescript-eslint/parser": "8.57.0",
|
||||||
"@vitejs/plugin-react": "5.1.4",
|
"@vitejs/plugin-react": "5.1.4",
|
||||||
"eslint-plugin-react-refresh": "0.4.26",
|
"eslint-plugin-react-refresh": "0.4.26",
|
||||||
"eslint-plugin-storybook": "10.2.14",
|
"eslint-plugin-storybook": "10.2.17",
|
||||||
"storybook": "10.2.15",
|
"storybook": "10.2.17",
|
||||||
"vite": "7.3.1",
|
"vite": "7.3.1",
|
||||||
"@storybook/addon-docs": "10.2.15"
|
"@storybook/addon-docs": "10.2.17"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-6
@@ -18,7 +18,7 @@ FROM node:24-alpine3.23 AS base
|
|||||||
FROM base AS installer
|
FROM base AS installer
|
||||||
|
|
||||||
# Enable corepack and prepare pnpm
|
# Enable corepack and prepare pnpm
|
||||||
RUN npm install --ignore-scripts -g corepack@latest
|
RUN npm install --ignore-scripts -g corepack@latest
|
||||||
RUN corepack enable
|
RUN corepack enable
|
||||||
RUN corepack prepare pnpm@10.28.2 --activate
|
RUN corepack prepare pnpm@10.28.2 --activate
|
||||||
|
|
||||||
@@ -67,6 +67,7 @@ RUN --mount=type=secret,id=database_url \
|
|||||||
--mount=type=secret,id=encryption_key \
|
--mount=type=secret,id=encryption_key \
|
||||||
--mount=type=secret,id=redis_url \
|
--mount=type=secret,id=redis_url \
|
||||||
--mount=type=secret,id=sentry_auth_token \
|
--mount=type=secret,id=sentry_auth_token \
|
||||||
|
--mount=type=secret,id=posthog_key \
|
||||||
/tmp/read-secrets.sh pnpm build --filter=@formbricks/web...
|
/tmp/read-secrets.sh pnpm build --filter=@formbricks/web...
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -74,9 +75,10 @@ RUN --mount=type=secret,id=database_url \
|
|||||||
#
|
#
|
||||||
FROM base AS runner
|
FROM base AS runner
|
||||||
|
|
||||||
# Update npm to latest, then create user
|
# Upgrade Alpine system packages to pick up security patches, update npm to latest, then create user
|
||||||
# Note: npm's bundled tar has a known vulnerability but npm is only used during build, not at runtime
|
# Note: npm's bundled tar has a known vulnerability but npm is only used during build, not at runtime
|
||||||
RUN npm install --ignore-scripts -g npm@latest \
|
RUN apk update && apk upgrade --no-cache \
|
||||||
|
&& npm install --ignore-scripts -g npm@latest \
|
||||||
&& addgroup -S nextjs \
|
&& addgroup -S nextjs \
|
||||||
&& adduser -S -u 1001 -G nextjs nextjs
|
&& adduser -S -u 1001 -G nextjs nextjs
|
||||||
|
|
||||||
@@ -120,8 +122,11 @@ RUN chown -R nextjs:nextjs ./node_modules/.prisma && chmod -R 755 ./node_modules
|
|||||||
COPY --from=installer /app/node_modules/@paralleldrive/cuid2 ./node_modules/@paralleldrive/cuid2
|
COPY --from=installer /app/node_modules/@paralleldrive/cuid2 ./node_modules/@paralleldrive/cuid2
|
||||||
RUN chmod -R 755 ./node_modules/@paralleldrive/cuid2
|
RUN chmod -R 755 ./node_modules/@paralleldrive/cuid2
|
||||||
|
|
||||||
COPY --from=installer /app/node_modules/uuid ./node_modules/uuid
|
# Runtime migrations import uuid v7 from the database package, so copy the
|
||||||
RUN chmod -R 755 ./node_modules/uuid
|
# database package's resolved install instead of the repo-root hoisted version.
|
||||||
|
COPY --from=installer /app/packages/database/node_modules/uuid ./node_modules/uuid
|
||||||
|
RUN chmod -R 755 ./node_modules/uuid \
|
||||||
|
&& node --input-type=module -e "import('uuid').then((module) => { if (typeof module.v7 !== 'function') throw new Error('uuid v7 missing in runtime image'); })"
|
||||||
|
|
||||||
COPY --from=installer /app/node_modules/@noble/hashes ./node_modules/@noble/hashes
|
COPY --from=installer /app/node_modules/@noble/hashes ./node_modules/@noble/hashes
|
||||||
RUN chmod -R 755 ./node_modules/@noble/hashes
|
RUN chmod -R 755 ./node_modules/@noble/hashes
|
||||||
@@ -164,4 +169,4 @@ RUN mkdir -p /home/nextjs/apps/web/uploads/ && \
|
|||||||
VOLUME /home/nextjs/apps/web/uploads/
|
VOLUME /home/nextjs/apps/web/uploads/
|
||||||
VOLUME /home/nextjs/apps/web/saml-connection
|
VOLUME /home/nextjs/apps/web/saml-connection
|
||||||
|
|
||||||
CMD ["/home/nextjs/start.sh"]
|
CMD ["/home/nextjs/start.sh"]
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
import { XIcon } from "lucide-react";
|
|
||||||
import { getServerSession } from "next-auth";
|
|
||||||
import Link from "next/link";
|
|
||||||
import { XMTemplateList } from "@/app/(app)/(onboarding)/environments/[environmentId]/xm-templates/components/XMTemplateList";
|
|
||||||
import { getEnvironment } from "@/lib/environment/service";
|
|
||||||
import { getProjectByEnvironmentId, getUserProjects } from "@/lib/project/service";
|
|
||||||
import { getUser } from "@/lib/user/service";
|
|
||||||
import { getOrganizationIdFromEnvironmentId } from "@/lib/utils/helper";
|
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
|
||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
|
||||||
import { Button } from "@/modules/ui/components/button";
|
|
||||||
import { Header } from "@/modules/ui/components/header";
|
|
||||||
|
|
||||||
interface XMTemplatePageProps {
|
|
||||||
params: Promise<{
|
|
||||||
environmentId: string;
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Page = async (props: XMTemplatePageProps) => {
|
|
||||||
const params = await props.params;
|
|
||||||
const session = await getServerSession(authOptions);
|
|
||||||
const environment = await getEnvironment(params.environmentId);
|
|
||||||
const t = await getTranslate();
|
|
||||||
if (!session) {
|
|
||||||
throw new Error(t("common.session_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await getUser(session.user.id);
|
|
||||||
if (!user) {
|
|
||||||
throw new Error(t("common.user_not_found"));
|
|
||||||
}
|
|
||||||
if (!environment) {
|
|
||||||
throw new Error(t("common.environment_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
const organizationId = await getOrganizationIdFromEnvironmentId(environment.id);
|
|
||||||
|
|
||||||
const project = await getProjectByEnvironmentId(environment.id);
|
|
||||||
if (!project) {
|
|
||||||
throw new Error(t("common.workspace_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
const projects = await getUserProjects(session.user.id, organizationId);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex min-h-full min-w-full flex-col items-center justify-center space-y-12">
|
|
||||||
<Header title={t("environments.xm-templates.headline")} />
|
|
||||||
<XMTemplateList project={project} user={user} environmentId={environment.id} />
|
|
||||||
{projects.length >= 2 && (
|
|
||||||
<Button
|
|
||||||
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
|
||||||
variant="ghost"
|
|
||||||
asChild>
|
|
||||||
<Link href={`/environments/${environment.id}/surveys`}>
|
|
||||||
<XIcon className="h-7 w-7" strokeWidth={1.5} />
|
|
||||||
</Link>
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Page;
|
|
||||||
@@ -19,8 +19,8 @@ describe("getTeamsByOrganizationId", () => {
|
|||||||
|
|
||||||
test("returns mapped teams", async () => {
|
test("returns mapped teams", async () => {
|
||||||
const mockTeams = [
|
const mockTeams = [
|
||||||
{ id: "t1", name: "Team 1" },
|
{ id: "t1", name: "Team 1", createdAt: new Date(), updatedAt: new Date(), organizationId: "org1" },
|
||||||
{ id: "t2", name: "Team 2" },
|
{ id: "t2", name: "Team 2", createdAt: new Date(), updatedAt: new Date(), organizationId: "org1" },
|
||||||
];
|
];
|
||||||
vi.mocked(prisma.team.findMany).mockResolvedValueOnce(mockTeams);
|
vi.mocked(prisma.team.findMany).mockResolvedValueOnce(mockTeams);
|
||||||
const result = await getTeamsByOrganizationId("org1");
|
const result = await getTeamsByOrganizationId("org1");
|
||||||
|
|||||||
@@ -22,12 +22,10 @@ export const getTeamsByOrganizationId = reactCache(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const projectTeams = teams.map((team) => ({
|
return teams.map((team: TOrganizationTeam) => ({
|
||||||
id: team.id,
|
id: team.id,
|
||||||
name: team.name,
|
name: team.name,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return projectTeams;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseError(error.message);
|
throw new DatabaseError(error.message);
|
||||||
|
|||||||
+1
-2
@@ -44,7 +44,7 @@ export const LandingSidebar = ({ user, organization }: LandingSidebarProps) => {
|
|||||||
className={cn(
|
className={cn(
|
||||||
"z-40 flex w-sidebar-collapsed flex-col justify-between rounded-r-xl border-r border-slate-200 bg-white pt-3 shadow-md transition-all duration-100"
|
"z-40 flex w-sidebar-collapsed flex-col justify-between rounded-r-xl border-r border-slate-200 bg-white pt-3 shadow-md transition-all duration-100"
|
||||||
)}>
|
)}>
|
||||||
<Image src={FBLogo} width={160} height={30} alt={t("environments.formbricks_logo")} />
|
<Image src={FBLogo} width={160} height={30} alt={t("workspace.formbricks_logo")} />
|
||||||
|
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
@@ -105,7 +105,6 @@ export const LandingSidebar = ({ user, organization }: LandingSidebarProps) => {
|
|||||||
organizationId: organization.id,
|
organizationId: organization.id,
|
||||||
redirect: true,
|
redirect: true,
|
||||||
callbackUrl: "/auth/login",
|
callbackUrl: "/auth/login",
|
||||||
clearEnvironmentId: true,
|
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
icon={<LogOutIcon className="mr-2 h-4 w-4" strokeWidth={1.5} />}>
|
icon={<LogOutIcon className="mr-2 h-4 w-4" strokeWidth={1.5} />}>
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { notFound, redirect } from "next/navigation";
|
import { notFound, redirect } from "next/navigation";
|
||||||
import { getEnvironments } from "@/lib/environment/service";
|
|
||||||
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
||||||
import { getUserProjects } from "@/lib/project/service";
|
import { getUserWorkspaces } from "@/lib/workspace/service";
|
||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
||||||
|
|
||||||
const LandingLayout = async (props) => {
|
const LandingLayout = async (props: {
|
||||||
|
params: Promise<{ organizationId: string }>;
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) => {
|
||||||
const params = await props.params;
|
const params = await props.params;
|
||||||
|
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
@@ -21,16 +23,11 @@ const LandingLayout = async (props) => {
|
|||||||
return notFound();
|
return notFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
const projects = await getUserProjects(session.user.id, params.organizationId);
|
const workspaces = await getUserWorkspaces(session.user.id, params.organizationId);
|
||||||
|
|
||||||
if (projects.length !== 0) {
|
if (workspaces.length !== 0) {
|
||||||
const firstProject = projects[0];
|
const firstWorkspace = workspaces[0];
|
||||||
const environments = await getEnvironments(firstProject.id);
|
return redirect(`/workspaces/${firstWorkspace.id}/`);
|
||||||
const prodEnvironment = environments.find((e) => e.type === "production");
|
|
||||||
|
|
||||||
if (prodEnvironment) {
|
|
||||||
return redirect(`/environments/${prodEnvironment.id}/`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return <>{children}</>;
|
return <>{children}</>;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { notFound, redirect } from "next/navigation";
|
import { notFound, redirect } from "next/navigation";
|
||||||
import { LandingSidebar } from "@/app/(app)/(onboarding)/organizations/[organizationId]/landing/components/landing-sidebar";
|
import { LandingSidebar } from "@/app/(app)/(onboarding)/organizations/[organizationId]/landing/components/landing-sidebar";
|
||||||
import { ProjectAndOrgSwitch } from "@/app/(app)/environments/[environmentId]/components/project-and-org-switch";
|
import { WorkspaceAndOrgSwitch } from "@/app/(app)/workspaces/[workspaceId]/components/workspace-and-org-switch";
|
||||||
import { IS_FORMBRICKS_CLOUD } from "@/lib/constants";
|
import { IS_FORMBRICKS_CLOUD } from "@/lib/constants";
|
||||||
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
||||||
import { getAccessFlags } from "@/lib/membership/utils";
|
import { getAccessFlags } from "@/lib/membership/utils";
|
||||||
@@ -10,7 +10,7 @@ import { getIsMultiOrgEnabled } from "@/modules/ee/license-check/lib/utils";
|
|||||||
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
||||||
import { Header } from "@/modules/ui/components/header";
|
import { Header } from "@/modules/ui/components/header";
|
||||||
|
|
||||||
const Page = async (props) => {
|
const Page = async (props: { params: Promise<{ organizationId: string }> }) => {
|
||||||
const params = await props.params;
|
const params = await props.params;
|
||||||
const t = await getTranslate();
|
const t = await getTranslate();
|
||||||
|
|
||||||
@@ -26,7 +26,8 @@ const Page = async (props) => {
|
|||||||
const isMultiOrgEnabled = await getIsMultiOrgEnabled();
|
const isMultiOrgEnabled = await getIsMultiOrgEnabled();
|
||||||
|
|
||||||
const membership = await getMembershipByUserIdOrganizationId(session.user.id, organization.id);
|
const membership = await getMembershipByUserIdOrganizationId(session.user.id, organization.id);
|
||||||
const { isMember } = getAccessFlags(membership?.role);
|
const { isMember, isBilling } = getAccessFlags(membership?.role);
|
||||||
|
const isMembershipPending = membership?.role === undefined;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-full min-w-full flex-row">
|
<div className="flex min-h-full min-w-full flex-row">
|
||||||
@@ -34,18 +35,19 @@ const Page = async (props) => {
|
|||||||
<div className="flex-1">
|
<div className="flex-1">
|
||||||
<div className="flex h-full flex-col">
|
<div className="flex h-full flex-col">
|
||||||
<div className="p-6">
|
<div className="p-6">
|
||||||
{/* we only need to render organization breadcrumb on this page, organizations/projects are lazy-loaded */}
|
{/* we only need to render organization breadcrumb on this page, organizations/workspaces are lazy-loaded */}
|
||||||
<ProjectAndOrgSwitch
|
<WorkspaceAndOrgSwitch
|
||||||
currentOrganizationId={organization.id}
|
currentOrganizationId={organization.id}
|
||||||
currentOrganizationName={organization.name}
|
currentOrganizationName={organization.name}
|
||||||
isMultiOrgEnabled={isMultiOrgEnabled}
|
isMultiOrgEnabled={isMultiOrgEnabled}
|
||||||
organizationProjectsLimit={0}
|
organizationWorkspacesLimit={0}
|
||||||
isFormbricksCloud={IS_FORMBRICKS_CLOUD}
|
isFormbricksCloud={IS_FORMBRICKS_CLOUD}
|
||||||
isLicenseActive={false}
|
isLicenseActive={false}
|
||||||
isOwnerOrManager={false}
|
isOwnerOrManager={false}
|
||||||
isAccessControlAllowed={false}
|
isAccessControlAllowed={false}
|
||||||
isMember={isMember}
|
isMember={isMember}
|
||||||
environments={[]}
|
isBilling={isBilling}
|
||||||
|
isMembershipPending={isMembershipPending}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex h-full flex-col items-center justify-center space-y-12">
|
<div className="flex h-full flex-col items-center justify-center space-y-12">
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { AuthorizationError } from "@formbricks/types/errors";
|
import { AuthenticationError, AuthorizationError, ResourceNotFoundError } from "@formbricks/types/errors";
|
||||||
import { canUserAccessOrganization } from "@/lib/organization/auth";
|
import { canUserAccessOrganization } from "@/lib/organization/auth";
|
||||||
import { getOrganization } from "@/lib/organization/service";
|
import { getOrganization } from "@/lib/organization/service";
|
||||||
import { getUser } from "@/lib/user/service";
|
import { getUser } from "@/lib/user/service";
|
||||||
@@ -8,7 +8,10 @@ import { getTranslate } from "@/lingodotdev/server";
|
|||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
||||||
import { ToasterClient } from "@/modules/ui/components/toaster-client";
|
import { ToasterClient } from "@/modules/ui/components/toaster-client";
|
||||||
|
|
||||||
const ProjectOnboardingLayout = async (props) => {
|
const WorkspaceOnboardingLayout = async (props: {
|
||||||
|
params: Promise<{ organizationId: string }>;
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) => {
|
||||||
const params = await props.params;
|
const params = await props.params;
|
||||||
|
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
@@ -22,7 +25,7 @@ const ProjectOnboardingLayout = async (props) => {
|
|||||||
|
|
||||||
const user = await getUser(session.user.id);
|
const user = await getUser(session.user.id);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error(t("common.user_not_found"));
|
throw new AuthenticationError(t("common.not_authenticated"));
|
||||||
}
|
}
|
||||||
|
|
||||||
const isAuthorized = await canUserAccessOrganization(session.user.id, params.organizationId);
|
const isAuthorized = await canUserAccessOrganization(session.user.id, params.organizationId);
|
||||||
@@ -33,7 +36,7 @@ const ProjectOnboardingLayout = async (props) => {
|
|||||||
|
|
||||||
const organization = await getOrganization(params.organizationId);
|
const organization = await getOrganization(params.organizationId);
|
||||||
if (!organization) {
|
if (!organization) {
|
||||||
throw new Error(t("common.organization_not_found"));
|
throw new ResourceNotFoundError(t("common.organization"), params.organizationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -44,4 +47,4 @@ const ProjectOnboardingLayout = async (props) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ProjectOnboardingLayout;
|
export default WorkspaceOnboardingLayout;
|
||||||
|
|||||||
+3
-3
@@ -2,7 +2,7 @@ import { PictureInPicture2Icon, SendIcon, XIcon } from "lucide-react";
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { OnboardingOptionsContainer } from "@/app/(app)/(onboarding)/organizations/components/OnboardingOptionsContainer";
|
import { OnboardingOptionsContainer } from "@/app/(app)/(onboarding)/organizations/components/OnboardingOptionsContainer";
|
||||||
import { getUserProjects } from "@/lib/project/service";
|
import { getUserWorkspaces } from "@/lib/workspace/service";
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
import { getTranslate } from "@/lingodotdev/server";
|
||||||
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
||||||
import { Button } from "@/modules/ui/components/button";
|
import { Button } from "@/modules/ui/components/button";
|
||||||
@@ -39,7 +39,7 @@ const Page = async (props: ChannelPageProps) => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const projects = await getUserProjects(session.user.id, params.organizationId);
|
const workspaces = await getUserWorkspaces(session.user.id, params.organizationId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-full min-w-full flex-col items-center justify-center space-y-12">
|
<div className="flex min-h-full min-w-full flex-col items-center justify-center space-y-12">
|
||||||
@@ -48,7 +48,7 @@ const Page = async (props: ChannelPageProps) => {
|
|||||||
subtitle={t("organizations.workspaces.new.channel.channel_select_subtitle")}
|
subtitle={t("organizations.workspaces.new.channel.channel_select_subtitle")}
|
||||||
/>
|
/>
|
||||||
<OnboardingOptionsContainer options={channelOptions} />
|
<OnboardingOptionsContainer options={channelOptions} />
|
||||||
{projects.length >= 1 && (
|
{workspaces.length >= 1 && (
|
||||||
<Button
|
<Button
|
||||||
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
|
|||||||
+13
-7
@@ -1,14 +1,18 @@
|
|||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { notFound, redirect } from "next/navigation";
|
import { notFound, redirect } from "next/navigation";
|
||||||
|
import { ResourceNotFoundError } from "@formbricks/types/errors";
|
||||||
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
||||||
import { getAccessFlags } from "@/lib/membership/utils";
|
import { getAccessFlags } from "@/lib/membership/utils";
|
||||||
import { getOrganization } from "@/lib/organization/service";
|
import { getOrganization } from "@/lib/organization/service";
|
||||||
import { getOrganizationProjectsCount } from "@/lib/project/service";
|
import { getOrganizationWorkspacesCount } from "@/lib/workspace/service";
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
import { getTranslate } from "@/lingodotdev/server";
|
||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
||||||
import { getOrganizationProjectsLimit } from "@/modules/ee/license-check/lib/utils";
|
import { getOrganizationWorkspacesLimit } from "@/modules/ee/license-check/lib/utils";
|
||||||
|
|
||||||
const OnboardingLayout = async (props) => {
|
const OnboardingLayout = async (props: {
|
||||||
|
params: Promise<{ organizationId: string }>;
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) => {
|
||||||
const params = await props.params;
|
const params = await props.params;
|
||||||
|
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
@@ -25,13 +29,15 @@ const OnboardingLayout = async (props) => {
|
|||||||
|
|
||||||
const organization = await getOrganization(params.organizationId);
|
const organization = await getOrganization(params.organizationId);
|
||||||
if (!organization) {
|
if (!organization) {
|
||||||
throw new Error(t("common.organization_not_found"));
|
throw new ResourceNotFoundError(t("common.organization"), params.organizationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
const organizationProjectsLimit = await getOrganizationProjectsLimit(organization.billing.limits);
|
const [organizationWorkspacesLimit, organizationWorkspacesCount] = await Promise.all([
|
||||||
const organizationProjectsCount = await getOrganizationProjectsCount(organization.id);
|
getOrganizationWorkspacesLimit(organization.id),
|
||||||
|
getOrganizationWorkspacesCount(organization.id),
|
||||||
|
]);
|
||||||
|
|
||||||
if (organizationProjectsCount >= organizationProjectsLimit) {
|
if (organizationWorkspacesCount >= organizationWorkspacesLimit) {
|
||||||
return redirect(`/`);
|
return redirect(`/`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -2,7 +2,7 @@ import { HeartIcon, ListTodoIcon, XIcon } from "lucide-react";
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { OnboardingOptionsContainer } from "@/app/(app)/(onboarding)/organizations/components/OnboardingOptionsContainer";
|
import { OnboardingOptionsContainer } from "@/app/(app)/(onboarding)/organizations/components/OnboardingOptionsContainer";
|
||||||
import { getUserProjects } from "@/lib/project/service";
|
import { getUserWorkspaces } from "@/lib/workspace/service";
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
import { getTranslate } from "@/lingodotdev/server";
|
||||||
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
||||||
import { Button } from "@/modules/ui/components/button";
|
import { Button } from "@/modules/ui/components/button";
|
||||||
@@ -39,13 +39,13 @@ const Page = async (props: ModePageProps) => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const projects = await getUserProjects(session.user.id, params.organizationId);
|
const workspaces = await getUserWorkspaces(session.user.id, params.organizationId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-full min-w-full flex-col items-center justify-center space-y-12">
|
<div className="flex min-h-full min-w-full flex-col items-center justify-center space-y-12">
|
||||||
<Header title={t("organizations.workspaces.new.mode.what_are_you_here_for")} />
|
<Header title={t("organizations.workspaces.new.mode.what_are_you_here_for")} />
|
||||||
<OnboardingOptionsContainer options={channelOptions} />
|
<OnboardingOptionsContainer options={channelOptions} />
|
||||||
{projects.length >= 1 && (
|
{workspaces.length >= 1 && (
|
||||||
<Button
|
<Button
|
||||||
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
|
|||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
import { getTranslate } from "@/lingodotdev/server";
|
||||||
|
import { SelectPlanCard } from "@/modules/ee/billing/components/select-plan-card";
|
||||||
|
import { Header } from "@/modules/ui/components/header";
|
||||||
|
|
||||||
|
interface SelectPlanOnboardingProps {
|
||||||
|
organizationId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const SelectPlanOnboarding = async ({ organizationId }: SelectPlanOnboardingProps) => {
|
||||||
|
const t = await getTranslate();
|
||||||
|
const nextUrl = `/organizations/${organizationId}/workspaces/new/mode`;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex min-h-full min-w-full flex-col items-center justify-center space-y-8">
|
||||||
|
<Header
|
||||||
|
title={t("workspace.settings.billing.select_plan_header_title")}
|
||||||
|
subtitle={t("workspace.settings.billing.select_plan_header_subtitle")}
|
||||||
|
/>
|
||||||
|
<SelectPlanCard nextUrl={nextUrl} organizationId={organizationId} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
+42
@@ -0,0 +1,42 @@
|
|||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { TCloudBillingPlan } from "@formbricks/types/organizations";
|
||||||
|
import { IS_FORMBRICKS_CLOUD } from "@/lib/constants";
|
||||||
|
import { getOrganizationBillingWithReadThroughSync } from "@/modules/ee/billing/lib/organization-billing";
|
||||||
|
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
||||||
|
import { SelectPlanOnboarding } from "./components/select-plan-onboarding";
|
||||||
|
|
||||||
|
const PAID_PLANS = new Set<TCloudBillingPlan>(["pro", "scale", "custom"]);
|
||||||
|
|
||||||
|
interface PlanPageProps {
|
||||||
|
params: Promise<{
|
||||||
|
organizationId: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Page = async (props: PlanPageProps) => {
|
||||||
|
const params = await props.params;
|
||||||
|
|
||||||
|
if (!IS_FORMBRICKS_CLOUD) {
|
||||||
|
return redirect(`/organizations/${params.organizationId}/workspaces/new/mode`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { session } = await getOrganizationAuth(params.organizationId);
|
||||||
|
|
||||||
|
if (!session?.user) {
|
||||||
|
return redirect(`/auth/login`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Users with an existing paid/trial subscription should not be shown the trial page.
|
||||||
|
// Redirect them directly to the next onboarding step.
|
||||||
|
const billing = await getOrganizationBillingWithReadThroughSync(params.organizationId);
|
||||||
|
const currentPlan = billing?.stripe?.plan;
|
||||||
|
const hasExistingSubscription = currentPlan !== undefined && PAID_PLANS.has(currentPlan);
|
||||||
|
|
||||||
|
if (hasExistingSubscription) {
|
||||||
|
return redirect(`/organizations/${params.organizationId}/workspaces/new/mode`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return <SelectPlanOnboarding organizationId={params.organizationId} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Page;
|
||||||
+35
-40
@@ -8,19 +8,19 @@ import { useForm } from "react-hook-form";
|
|||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import {
|
import {
|
||||||
TProjectConfigChannel,
|
TWorkspaceConfigChannel,
|
||||||
TProjectConfigIndustry,
|
TWorkspaceConfigIndustry,
|
||||||
TProjectMode,
|
TWorkspaceMode,
|
||||||
TProjectUpdateInput,
|
TWorkspaceUpdateInput,
|
||||||
ZProjectUpdateInput,
|
ZWorkspaceUpdateInput,
|
||||||
} from "@formbricks/types/project";
|
} from "@formbricks/types/workspace";
|
||||||
import { createProjectAction } from "@/app/(app)/environments/[environmentId]/actions";
|
import { createWorkspaceAction } from "@/app/(app)/workspaces/[workspaceId]/actions";
|
||||||
import { previewSurvey } from "@/app/lib/templates";
|
import { previewSurvey } from "@/app/lib/templates";
|
||||||
import { FORMBRICKS_SURVEYS_FILTERS_KEY_LS } from "@/lib/localStorage";
|
import { FORMBRICKS_SURVEYS_FILTERS_KEY_LS } from "@/lib/localStorage";
|
||||||
import { buildStylingFromBrandColor } from "@/lib/styling/constants";
|
import { buildStylingFromBrandColor } from "@/lib/styling/constants";
|
||||||
import { getFormattedErrorMessage } from "@/lib/utils/helper";
|
import { getFormattedErrorMessage } from "@/lib/utils/helper";
|
||||||
import { TOrganizationTeam } from "@/modules/ee/teams/project-teams/types/team";
|
|
||||||
import { CreateTeamModal } from "@/modules/ee/teams/team-list/components/create-team-modal";
|
import { CreateTeamModal } from "@/modules/ee/teams/team-list/components/create-team-modal";
|
||||||
|
import { TOrganizationTeam } from "@/modules/ee/teams/workspace-teams/types/team";
|
||||||
import { Button } from "@/modules/ui/components/button";
|
import { Button } from "@/modules/ui/components/button";
|
||||||
import { ColorPicker } from "@/modules/ui/components/color-picker";
|
import { ColorPicker } from "@/modules/ui/components/color-picker";
|
||||||
import {
|
import {
|
||||||
@@ -36,34 +36,34 @@ import { Input } from "@/modules/ui/components/input";
|
|||||||
import { MultiSelect } from "@/modules/ui/components/multi-select";
|
import { MultiSelect } from "@/modules/ui/components/multi-select";
|
||||||
import { SurveyInline } from "@/modules/ui/components/survey";
|
import { SurveyInline } from "@/modules/ui/components/survey";
|
||||||
|
|
||||||
interface ProjectSettingsProps {
|
interface WorkspaceSettingsProps {
|
||||||
organizationId: string;
|
organizationId: string;
|
||||||
projectMode: TProjectMode;
|
workspaceMode: TWorkspaceMode;
|
||||||
channel: TProjectConfigChannel;
|
channel: TWorkspaceConfigChannel;
|
||||||
industry: TProjectConfigIndustry;
|
industry: TWorkspaceConfigIndustry;
|
||||||
defaultBrandColor: string;
|
defaultBrandColor: string;
|
||||||
organizationTeams: TOrganizationTeam[];
|
organizationTeams: TOrganizationTeam[];
|
||||||
isAccessControlAllowed: boolean;
|
isAccessControlAllowed: boolean;
|
||||||
userProjectsCount: number;
|
userWorkspacesCount: number;
|
||||||
publicDomain: string;
|
publicDomain: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ProjectSettings = ({
|
export const WorkspaceSettings = ({
|
||||||
organizationId,
|
organizationId,
|
||||||
projectMode,
|
workspaceMode,
|
||||||
channel,
|
channel,
|
||||||
industry,
|
industry,
|
||||||
defaultBrandColor,
|
defaultBrandColor,
|
||||||
organizationTeams,
|
organizationTeams,
|
||||||
isAccessControlAllowed = false,
|
isAccessControlAllowed = false,
|
||||||
userProjectsCount,
|
userWorkspacesCount,
|
||||||
publicDomain,
|
publicDomain,
|
||||||
}: ProjectSettingsProps) => {
|
}: WorkspaceSettingsProps) => {
|
||||||
const [createTeamModalOpen, setCreateTeamModalOpen] = useState(false);
|
const [createTeamModalOpen, setCreateTeamModalOpen] = useState(false);
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const addProject = async (data: TProjectUpdateInput) => {
|
const addWorkspace = async (data: TWorkspaceUpdateInput) => {
|
||||||
try {
|
try {
|
||||||
// Build the full styling from the chosen brand color so all derived
|
// Build the full styling from the chosen brand color so all derived
|
||||||
// colours (question, button, input, option, progress, etc.) are persisted.
|
// colours (question, button, input, option, progress, etc.) are persisted.
|
||||||
@@ -71,7 +71,7 @@ export const ProjectSettings = ({
|
|||||||
// back to STYLE_DEFAULTS computed from the default brand (#64748b).
|
// back to STYLE_DEFAULTS computed from the default brand (#64748b).
|
||||||
const fullStyling = buildStylingFromBrandColor(data.styling?.brandColor?.light);
|
const fullStyling = buildStylingFromBrandColor(data.styling?.brandColor?.light);
|
||||||
|
|
||||||
const createProjectResponse = await createProjectAction({
|
const createWorkspaceResponse = await createWorkspaceAction({
|
||||||
organizationId,
|
organizationId,
|
||||||
data: {
|
data: {
|
||||||
...data,
|
...data,
|
||||||
@@ -81,26 +81,21 @@ export const ProjectSettings = ({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (createProjectResponse?.data) {
|
if (createWorkspaceResponse?.data) {
|
||||||
// get production environment
|
if (globalThis.window !== undefined) {
|
||||||
const productionEnvironment = createProjectResponse.data.environments.find(
|
// Remove filters when creating a new workspace
|
||||||
(environment) => environment.type === "production"
|
localStorage.removeItem(FORMBRICKS_SURVEYS_FILTERS_KEY_LS);
|
||||||
);
|
|
||||||
if (productionEnvironment) {
|
|
||||||
if (globalThis.window !== undefined) {
|
|
||||||
// Rmove filters when creating a new project
|
|
||||||
localStorage.removeItem(FORMBRICKS_SURVEYS_FILTERS_KEY_LS);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
const workspaceId = createWorkspaceResponse.data.id;
|
||||||
if (channel === "app" || channel === "website") {
|
if (channel === "app" || channel === "website") {
|
||||||
router.push(`/environments/${productionEnvironment?.id}/connect`);
|
router.push(`/workspaces/${workspaceId}/connect`);
|
||||||
} else if (channel === "link") {
|
} else if (channel === "link") {
|
||||||
router.push(`/environments/${productionEnvironment?.id}/surveys`);
|
router.push(`/workspaces/${workspaceId}/surveys`);
|
||||||
} else if (projectMode === "cx") {
|
} else if (workspaceMode === "cx") {
|
||||||
router.push(`/environments/${productionEnvironment?.id}/xm-templates`);
|
router.push(`/workspaces/${workspaceId}/xm-templates`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const errorMessage = getFormattedErrorMessage(createProjectResponse);
|
const errorMessage = getFormattedErrorMessage(createWorkspaceResponse);
|
||||||
toast.error(errorMessage);
|
toast.error(errorMessage);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -109,15 +104,15 @@ export const ProjectSettings = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const form = useForm<TProjectUpdateInput>({
|
const form = useForm<TWorkspaceUpdateInput>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
name: "",
|
name: "",
|
||||||
styling: { allowStyleOverwrite: true, brandColor: { light: defaultBrandColor } },
|
styling: { allowStyleOverwrite: true, brandColor: { light: defaultBrandColor } },
|
||||||
teamIds: [],
|
teamIds: [],
|
||||||
},
|
},
|
||||||
resolver: zodResolver(ZProjectUpdateInput),
|
resolver: zodResolver(ZWorkspaceUpdateInput),
|
||||||
});
|
});
|
||||||
const projectName = form.watch("name");
|
const workspaceName = form.watch("name");
|
||||||
const logoUrl = form.watch("logo.url");
|
const logoUrl = form.watch("logo.url");
|
||||||
const brandColor = form.watch("styling.brandColor.light") ?? defaultBrandColor;
|
const brandColor = form.watch("styling.brandColor.light") ?? defaultBrandColor;
|
||||||
const previewStyling = useMemo(() => buildStylingFromBrandColor(brandColor), [brandColor]);
|
const previewStyling = useMemo(() => buildStylingFromBrandColor(brandColor), [brandColor]);
|
||||||
@@ -132,7 +127,7 @@ export const ProjectSettings = ({
|
|||||||
<div className="mt-6 flex w-5/6 space-x-10 lg:w-2/3 2xl:w-1/2">
|
<div className="mt-6 flex w-5/6 space-x-10 lg:w-2/3 2xl:w-1/2">
|
||||||
<div className="flex w-1/2 flex-col space-y-4">
|
<div className="flex w-1/2 flex-col space-y-4">
|
||||||
<FormProvider {...form}>
|
<FormProvider {...form}>
|
||||||
<form onSubmit={form.handleSubmit(addProject)} className="w-full space-y-4">
|
<form onSubmit={form.handleSubmit(addWorkspace)} className="w-full space-y-4">
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="styling.brandColor.light"
|
name="styling.brandColor.light"
|
||||||
@@ -184,7 +179,7 @@ export const ProjectSettings = ({
|
|||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{isAccessControlAllowed && userProjectsCount > 0 && (
|
{isAccessControlAllowed && userWorkspacesCount > 0 && (
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="teamIds"
|
name="teamIds"
|
||||||
@@ -242,7 +237,7 @@ export const ProjectSettings = ({
|
|||||||
<SurveyInline
|
<SurveyInline
|
||||||
appUrl={publicDomain}
|
appUrl={publicDomain}
|
||||||
isPreviewMode={true}
|
isPreviewMode={true}
|
||||||
survey={previewSurvey(projectName || t("common.my_product"), t)}
|
survey={previewSurvey(workspaceName || t("common.my_product"), t)}
|
||||||
styling={previewStyling}
|
styling={previewStyling}
|
||||||
isBrandingEnabled={false}
|
isBrandingEnabled={false}
|
||||||
languageCode="default"
|
languageCode="default"
|
||||||
+20
-15
@@ -1,30 +1,35 @@
|
|||||||
import { XIcon } from "lucide-react";
|
import { XIcon } from "lucide-react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { TProjectConfigChannel, TProjectConfigIndustry, TProjectMode } from "@formbricks/types/project";
|
import { ResourceNotFoundError } from "@formbricks/types/errors";
|
||||||
|
import {
|
||||||
|
TWorkspaceConfigChannel,
|
||||||
|
TWorkspaceConfigIndustry,
|
||||||
|
TWorkspaceMode,
|
||||||
|
} from "@formbricks/types/workspace";
|
||||||
import { getTeamsByOrganizationId } from "@/app/(app)/(onboarding)/lib/onboarding";
|
import { getTeamsByOrganizationId } from "@/app/(app)/(onboarding)/lib/onboarding";
|
||||||
import { ProjectSettings } from "@/app/(app)/(onboarding)/organizations/[organizationId]/workspaces/new/settings/components/ProjectSettings";
|
import { WorkspaceSettings } from "@/app/(app)/(onboarding)/organizations/[organizationId]/workspaces/new/settings/components/WorkspaceSettings";
|
||||||
import { DEFAULT_BRAND_COLOR } from "@/lib/constants";
|
import { DEFAULT_BRAND_COLOR } from "@/lib/constants";
|
||||||
import { getPublicDomain } from "@/lib/getPublicUrl";
|
import { getPublicDomain } from "@/lib/getPublicUrl";
|
||||||
import { getUserProjects } from "@/lib/project/service";
|
import { getUserWorkspaces } from "@/lib/workspace/service";
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
import { getTranslate } from "@/lingodotdev/server";
|
||||||
import { getAccessControlPermission } from "@/modules/ee/license-check/lib/utils";
|
import { getAccessControlPermission } from "@/modules/ee/license-check/lib/utils";
|
||||||
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
import { getOrganizationAuth } from "@/modules/organization/lib/utils";
|
||||||
import { Button } from "@/modules/ui/components/button";
|
import { Button } from "@/modules/ui/components/button";
|
||||||
import { Header } from "@/modules/ui/components/header";
|
import { Header } from "@/modules/ui/components/header";
|
||||||
|
|
||||||
interface ProjectSettingsPageProps {
|
interface WorkspaceSettingsPageProps {
|
||||||
params: Promise<{
|
params: Promise<{
|
||||||
organizationId: string;
|
organizationId: string;
|
||||||
}>;
|
}>;
|
||||||
searchParams: Promise<{
|
searchParams: Promise<{
|
||||||
channel?: TProjectConfigChannel;
|
channel?: TWorkspaceConfigChannel;
|
||||||
industry?: TProjectConfigIndustry;
|
industry?: TWorkspaceConfigIndustry;
|
||||||
mode?: TProjectMode;
|
mode?: TWorkspaceMode;
|
||||||
}>;
|
}>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Page = async (props: ProjectSettingsPageProps) => {
|
const Page = async (props: WorkspaceSettingsPageProps) => {
|
||||||
const searchParams = await props.searchParams;
|
const searchParams = await props.searchParams;
|
||||||
const params = await props.params;
|
const params = await props.params;
|
||||||
const t = await getTranslate();
|
const t = await getTranslate();
|
||||||
@@ -38,14 +43,14 @@ const Page = async (props: ProjectSettingsPageProps) => {
|
|||||||
const channel = searchParams.channel ?? null;
|
const channel = searchParams.channel ?? null;
|
||||||
const industry = searchParams.industry ?? null;
|
const industry = searchParams.industry ?? null;
|
||||||
const mode = searchParams.mode ?? "surveys";
|
const mode = searchParams.mode ?? "surveys";
|
||||||
const projects = await getUserProjects(session.user.id, params.organizationId);
|
const workspaces = await getUserWorkspaces(session.user.id, params.organizationId);
|
||||||
|
|
||||||
const organizationTeams = await getTeamsByOrganizationId(params.organizationId);
|
const organizationTeams = await getTeamsByOrganizationId(params.organizationId);
|
||||||
|
|
||||||
const isAccessControlAllowed = await getAccessControlPermission(organization.billing.plan);
|
const isAccessControlAllowed = await getAccessControlPermission(organization.id);
|
||||||
|
|
||||||
if (!organizationTeams) {
|
if (!organizationTeams) {
|
||||||
throw new Error(t("common.organization_teams_not_found"));
|
throw new ResourceNotFoundError(t("common.team"), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
const publicDomain = getPublicDomain();
|
const publicDomain = getPublicDomain();
|
||||||
@@ -56,18 +61,18 @@ const Page = async (props: ProjectSettingsPageProps) => {
|
|||||||
title={t("organizations.workspaces.new.settings.workspace_settings_title")}
|
title={t("organizations.workspaces.new.settings.workspace_settings_title")}
|
||||||
subtitle={t("organizations.workspaces.new.settings.workspace_settings_subtitle")}
|
subtitle={t("organizations.workspaces.new.settings.workspace_settings_subtitle")}
|
||||||
/>
|
/>
|
||||||
<ProjectSettings
|
<WorkspaceSettings
|
||||||
organizationId={params.organizationId}
|
organizationId={params.organizationId}
|
||||||
projectMode={mode}
|
workspaceMode={mode}
|
||||||
channel={channel}
|
channel={channel}
|
||||||
industry={industry}
|
industry={industry}
|
||||||
defaultBrandColor={DEFAULT_BRAND_COLOR}
|
defaultBrandColor={DEFAULT_BRAND_COLOR}
|
||||||
organizationTeams={organizationTeams}
|
organizationTeams={organizationTeams}
|
||||||
isAccessControlAllowed={isAccessControlAllowed}
|
isAccessControlAllowed={isAccessControlAllowed}
|
||||||
userProjectsCount={projects.length}
|
userWorkspacesCount={workspaces.length}
|
||||||
publicDomain={publicDomain}
|
publicDomain={publicDomain}
|
||||||
/>
|
/>
|
||||||
{projects.length >= 1 && (
|
{workspaces.length >= 1 && (
|
||||||
<Button
|
<Button
|
||||||
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@ interface OnboardingOptionsContainerProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const OnboardingOptionsContainer = ({ options }: OnboardingOptionsContainerProps) => {
|
export const OnboardingOptionsContainer = ({ options }: OnboardingOptionsContainerProps) => {
|
||||||
const getOptionCard = (option) => {
|
const getOptionCard = (option: OnboardingOptionsContainerProps["options"][number]) => {
|
||||||
const Icon = option.icon;
|
const Icon = option.icon;
|
||||||
return (
|
return (
|
||||||
<OptionCard
|
<OptionCard
|
||||||
|
|||||||
+10
-13
@@ -4,21 +4,20 @@ import { ArrowRight } from "lucide-react";
|
|||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { TEnvironment } from "@formbricks/types/environment";
|
import { TWorkspaceConfigChannel } from "@formbricks/types/workspace";
|
||||||
import { TProjectConfigChannel } from "@formbricks/types/project";
|
|
||||||
import { cn } from "@/lib/cn";
|
import { cn } from "@/lib/cn";
|
||||||
import { Button } from "@/modules/ui/components/button";
|
import { Button } from "@/modules/ui/components/button";
|
||||||
import { OnboardingSetupInstructions } from "./OnboardingSetupInstructions";
|
import { OnboardingSetupInstructions } from "./OnboardingSetupInstructions";
|
||||||
|
|
||||||
interface ConnectWithFormbricksProps {
|
interface ConnectWithFormbricksProps {
|
||||||
environment: TEnvironment;
|
workspaceId: string;
|
||||||
publicDomain: string;
|
publicDomain: string;
|
||||||
appSetupCompleted: boolean;
|
appSetupCompleted: boolean;
|
||||||
channel: TProjectConfigChannel;
|
channel: TWorkspaceConfigChannel;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ConnectWithFormbricks = ({
|
export const ConnectWithFormbricks = ({
|
||||||
environment,
|
workspaceId,
|
||||||
publicDomain,
|
publicDomain,
|
||||||
appSetupCompleted,
|
appSetupCompleted,
|
||||||
channel,
|
channel,
|
||||||
@@ -26,7 +25,7 @@ export const ConnectWithFormbricks = ({
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const handleFinishOnboarding = async () => {
|
const handleFinishOnboarding = async () => {
|
||||||
router.push(`/environments/${environment.id}/surveys`);
|
router.push(`/workspaces/${workspaceId}/surveys`);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -48,7 +47,7 @@ export const ConnectWithFormbricks = ({
|
|||||||
<div className="flex w-full space-x-10">
|
<div className="flex w-full space-x-10">
|
||||||
<div className="flex w-1/2 flex-col space-y-4">
|
<div className="flex w-1/2 flex-col space-y-4">
|
||||||
<OnboardingSetupInstructions
|
<OnboardingSetupInstructions
|
||||||
environmentId={environment.id}
|
workspaceId={workspaceId}
|
||||||
publicDomain={publicDomain}
|
publicDomain={publicDomain}
|
||||||
channel={channel}
|
channel={channel}
|
||||||
appSetupCompleted={appSetupCompleted}
|
appSetupCompleted={appSetupCompleted}
|
||||||
@@ -61,9 +60,9 @@ export const ConnectWithFormbricks = ({
|
|||||||
)}>
|
)}>
|
||||||
{appSetupCompleted ? (
|
{appSetupCompleted ? (
|
||||||
<div>
|
<div>
|
||||||
<p className="text-3xl">{t("environments.connect.congrats")}</p>
|
<p className="text-3xl">{t("workspace.connect.congrats")}</p>
|
||||||
<p className="pt-4 text-sm font-medium text-slate-600">
|
<p className="pt-4 text-sm font-medium text-slate-600">
|
||||||
{t("environments.connect.connection_successful_message")}
|
{t("workspace.connect.connection_successful_message")}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
@@ -73,7 +72,7 @@ export const ConnectWithFormbricks = ({
|
|||||||
<span className="relative inline-flex h-10 w-10 rounded-full bg-slate-500"></span>
|
<span className="relative inline-flex h-10 w-10 rounded-full bg-slate-500"></span>
|
||||||
</span>
|
</span>
|
||||||
<p className="pt-4 text-sm font-medium text-slate-600">
|
<p className="pt-4 text-sm font-medium text-slate-600">
|
||||||
{t("environments.connect.waiting_for_your_signal")}
|
{t("workspace.connect.waiting_for_your_signal")}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -83,9 +82,7 @@ export const ConnectWithFormbricks = ({
|
|||||||
id="finishOnboarding"
|
id="finishOnboarding"
|
||||||
variant={appSetupCompleted ? "default" : "ghost"}
|
variant={appSetupCompleted ? "default" : "ghost"}
|
||||||
onClick={handleFinishOnboarding}>
|
onClick={handleFinishOnboarding}>
|
||||||
{appSetupCompleted
|
{appSetupCompleted ? t("workspace.connect.finish_onboarding") : t("workspace.connect.do_it_later")}
|
||||||
? t("environments.connect.finish_onboarding")
|
|
||||||
: t("environments.connect.do_it_later")}
|
|
||||||
<ArrowRight />
|
<ArrowRight />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
+19
-19
@@ -5,7 +5,7 @@ import "prismjs/themes/prism.css";
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { TProjectConfigChannel } from "@formbricks/types/project";
|
import { TWorkspaceConfigChannel } from "@formbricks/types/workspace";
|
||||||
import { Button } from "@/modules/ui/components/button";
|
import { Button } from "@/modules/ui/components/button";
|
||||||
import { CodeBlock } from "@/modules/ui/components/code-block";
|
import { CodeBlock } from "@/modules/ui/components/code-block";
|
||||||
import { Html5Icon, NpmIcon } from "@/modules/ui/components/icons";
|
import { Html5Icon, NpmIcon } from "@/modules/ui/components/icons";
|
||||||
@@ -17,14 +17,14 @@ const tabs = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
interface OnboardingSetupInstructionsProps {
|
interface OnboardingSetupInstructionsProps {
|
||||||
environmentId: string;
|
workspaceId: string;
|
||||||
publicDomain: string;
|
publicDomain: string;
|
||||||
channel: TProjectConfigChannel;
|
channel: TWorkspaceConfigChannel;
|
||||||
appSetupCompleted: boolean;
|
appSetupCompleted: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const OnboardingSetupInstructions = ({
|
export const OnboardingSetupInstructions = ({
|
||||||
environmentId,
|
workspaceId,
|
||||||
publicDomain,
|
publicDomain,
|
||||||
channel,
|
channel,
|
||||||
appSetupCompleted,
|
appSetupCompleted,
|
||||||
@@ -35,8 +35,8 @@ export const OnboardingSetupInstructions = ({
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
!function(){
|
!function(){
|
||||||
var appUrl = "${publicDomain}";
|
var appUrl = "${publicDomain}";
|
||||||
var environmentId = "${environmentId}";
|
var workspaceId = "${workspaceId}";
|
||||||
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=appUrl+"/js/formbricks.umd.cjs",t.onload=function(){window.formbricks?window.formbricks.setup({environmentId:environmentId,appUrl:appUrl}):console.error("Formbricks library failed to load properly. The formbricks object is not available.");};var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e)}();
|
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=appUrl+"/js/formbricks.umd.cjs",t.onload=function(){window.formbricks?window.formbricks.setup({workspaceId:workspaceId,appUrl:appUrl}):console.error("Formbricks library failed to load properly. The formbricks object is not available.");};var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e)}();
|
||||||
</script>
|
</script>
|
||||||
<!-- END Formbricks Surveys -->
|
<!-- END Formbricks Surveys -->
|
||||||
`;
|
`;
|
||||||
@@ -45,46 +45,46 @@ export const OnboardingSetupInstructions = ({
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
!function(){
|
!function(){
|
||||||
var appUrl = "${publicDomain}";
|
var appUrl = "${publicDomain}";
|
||||||
var environmentId = "${environmentId}";
|
var workspaceId = "${workspaceId}";
|
||||||
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=appUrl+"/js/formbricks.umd.cjs",t.onload=function(){window.formbricks?window.formbricks.setup({environmentId:environmentId,appUrl:appUrl}):console.error("Formbricks library failed to load properly. The formbricks object is not available.");};var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e)}();
|
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=appUrl+"/js/formbricks.umd.cjs",t.onload=function(){window.formbricks?window.formbricks.setup({workspaceId:workspaceId,appUrl:appUrl}):console.error("Formbricks library failed to load properly. The formbricks object is not available.");};var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e)}();
|
||||||
</script>
|
</script>
|
||||||
<!-- END Formbricks Surveys -->
|
<!-- END Formbricks Surveys -->
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const npmSnippetForAppSurveys = `
|
const npmSnippetForAppSurveys = `
|
||||||
import formbricks from "@formbricks/js";
|
import formbricks from "@formbricks/js";
|
||||||
|
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
formbricks.setup({
|
formbricks.setup({
|
||||||
environmentId: "${environmentId}",
|
workspaceId: "${workspaceId}",
|
||||||
appUrl: "${publicDomain}",
|
appUrl: "${publicDomain}",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
// your own app
|
// your own app
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const npmSnippetForWebsiteSurveys = `
|
const npmSnippetForWebsiteSurveys = `
|
||||||
// other imports
|
// other imports
|
||||||
import formbricks from "@formbricks/js";
|
import formbricks from "@formbricks/js";
|
||||||
|
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
formbricks.setup({
|
formbricks.setup({
|
||||||
environmentId: "${environmentId}",
|
workspaceId: "${workspaceId}",
|
||||||
appUrl: "${publicDomain}",
|
appUrl: "${publicDomain}",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
// your own app
|
// your own app
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -109,7 +109,7 @@ export const OnboardingSetupInstructions = ({
|
|||||||
yarn add @formbricks/js
|
yarn add @formbricks/js
|
||||||
</CodeBlock>
|
</CodeBlock>
|
||||||
<p className="text-sm text-slate-700">
|
<p className="text-sm text-slate-700">
|
||||||
{t("environments.connect.import_formbricks_and_initialize_the_widget_in_your_component")}
|
{t("workspace.connect.import_formbricks_and_initialize_the_widget_in_your_component")}
|
||||||
</p>
|
</p>
|
||||||
<CodeBlock customEditorClass="!bg-white border border-slate-200" language="js">
|
<CodeBlock customEditorClass="!bg-white border border-slate-200" language="js">
|
||||||
{channel === "app" ? npmSnippetForAppSurveys : npmSnippetForWebsiteSurveys}
|
{channel === "app" ? npmSnippetForAppSurveys : npmSnippetForWebsiteSurveys}
|
||||||
@@ -126,7 +126,7 @@ export const OnboardingSetupInstructions = ({
|
|||||||
) : activeTab === "html" ? (
|
) : activeTab === "html" ? (
|
||||||
<div className="prose prose-slate">
|
<div className="prose prose-slate">
|
||||||
<p className="-mb-1 mt-6 text-sm text-slate-700">
|
<p className="-mb-1 mt-6 text-sm text-slate-700">
|
||||||
{t("environments.connect.insert_this_code_into_the_head_tag_of_your_website")}
|
{t("workspace.connect.insert_this_code_into_the_head_tag_of_your_website")}
|
||||||
</p>
|
</p>
|
||||||
<div>
|
<div>
|
||||||
<CodeBlock customEditorClass="!bg-white border border-slate-200" language="js">
|
<CodeBlock customEditorClass="!bg-white border border-slate-200" language="js">
|
||||||
+12
-17
@@ -1,55 +1,50 @@
|
|||||||
import { XIcon } from "lucide-react";
|
import { XIcon } from "lucide-react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { ConnectWithFormbricks } from "@/app/(app)/(onboarding)/environments/[environmentId]/connect/components/ConnectWithFormbricks";
|
import { ResourceNotFoundError } from "@formbricks/types/errors";
|
||||||
import { getEnvironment } from "@/lib/environment/service";
|
import { ConnectWithFormbricks } from "@/app/(app)/(onboarding)/workspaces/[workspaceId]/connect/components/ConnectWithFormbricks";
|
||||||
import { getPublicDomain } from "@/lib/getPublicUrl";
|
import { getPublicDomain } from "@/lib/getPublicUrl";
|
||||||
import { getProjectByEnvironmentId } from "@/lib/project/service";
|
import { getWorkspace } from "@/lib/workspace/service";
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
import { getTranslate } from "@/lingodotdev/server";
|
||||||
import { Button } from "@/modules/ui/components/button";
|
import { Button } from "@/modules/ui/components/button";
|
||||||
import { Header } from "@/modules/ui/components/header";
|
import { Header } from "@/modules/ui/components/header";
|
||||||
|
|
||||||
interface ConnectPageProps {
|
interface ConnectPageProps {
|
||||||
params: Promise<{
|
params: Promise<{
|
||||||
environmentId: string;
|
workspaceId: string;
|
||||||
}>;
|
}>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Page = async (props: ConnectPageProps) => {
|
const Page = async (props: ConnectPageProps) => {
|
||||||
const params = await props.params;
|
const params = await props.params;
|
||||||
const t = await getTranslate();
|
const t = await getTranslate();
|
||||||
const environment = await getEnvironment(params.environmentId);
|
|
||||||
|
|
||||||
if (!environment) {
|
const workspace = await getWorkspace(params.workspaceId);
|
||||||
throw new Error(t("common.environment_not_found"));
|
if (!workspace) {
|
||||||
|
throw new ResourceNotFoundError(t("common.workspace"), params.workspaceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
const project = await getProjectByEnvironmentId(environment.id);
|
const channel = workspace.config.channel || null;
|
||||||
if (!project) {
|
|
||||||
throw new Error(t("common.workspace_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
const channel = project.config.channel || null;
|
|
||||||
|
|
||||||
const publicDomain = getPublicDomain();
|
const publicDomain = getPublicDomain();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-full flex-col items-center justify-center py-10">
|
<div className="flex min-h-full flex-col items-center justify-center py-10">
|
||||||
<Header title={t("environments.connect.headline")} subtitle={t("environments.connect.subtitle")} />
|
<Header title={t("workspace.connect.headline")} subtitle={t("workspace.connect.subtitle")} />
|
||||||
<div className="space-y-4 text-center">
|
<div className="space-y-4 text-center">
|
||||||
<p className="text-4xl font-medium text-slate-800"></p>
|
<p className="text-4xl font-medium text-slate-800"></p>
|
||||||
<p className="text-sm text-slate-500"></p>
|
<p className="text-sm text-slate-500"></p>
|
||||||
</div>
|
</div>
|
||||||
<ConnectWithFormbricks
|
<ConnectWithFormbricks
|
||||||
environment={environment}
|
workspaceId={params.workspaceId}
|
||||||
publicDomain={publicDomain}
|
publicDomain={publicDomain}
|
||||||
appSetupCompleted={environment.appSetupCompleted}
|
appSetupCompleted={workspace.appSetupCompleted}
|
||||||
channel={channel}
|
channel={channel}
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
asChild>
|
asChild>
|
||||||
<Link href={`/environments/${environment.id}`}>
|
<Link href={`/workspaces/${params.workspaceId}`}>
|
||||||
<XIcon className="h-7 w-7" strokeWidth={1.5} />
|
<XIcon className="h-7 w-7" strokeWidth={1.5} />
|
||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
+7
-4
@@ -1,10 +1,13 @@
|
|||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { AuthorizationError } from "@formbricks/types/errors";
|
import { AuthorizationError } from "@formbricks/types/errors";
|
||||||
import { hasUserEnvironmentAccess } from "@/lib/environment/auth";
|
import { hasUserWorkspaceAccess } from "@/lib/workspace/auth";
|
||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
||||||
|
|
||||||
const OnboardingLayout = async (props) => {
|
const OnboardingLayout = async (props: {
|
||||||
|
params: Promise<{ workspaceId: string }>;
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) => {
|
||||||
const params = await props.params;
|
const params = await props.params;
|
||||||
|
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
@@ -14,9 +17,9 @@ const OnboardingLayout = async (props) => {
|
|||||||
return redirect(`/auth/login`);
|
return redirect(`/auth/login`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const isAuthorized = await hasUserEnvironmentAccess(session.user.id, params.environmentId);
|
const isAuthorized = await hasUserWorkspaceAccess(session.user.id, params.workspaceId);
|
||||||
if (!isAuthorized) {
|
if (!isAuthorized) {
|
||||||
throw new AuthorizationError("User is not authorized to access this environment");
|
throw new AuthorizationError("User is not authorized to access this workspace");
|
||||||
}
|
}
|
||||||
|
|
||||||
return <div className="flex-1 bg-slate-50">{children}</div>;
|
return <div className="flex-1 bg-slate-50">{children}</div>;
|
||||||
+21
-21
@@ -5,23 +5,23 @@ import { useRouter } from "next/navigation";
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { TProject } from "@formbricks/types/project";
|
|
||||||
import { TSurveyCreateInput } from "@formbricks/types/surveys/types";
|
import { TSurveyCreateInput } from "@formbricks/types/surveys/types";
|
||||||
import { TXMTemplate } from "@formbricks/types/templates";
|
import { TXMTemplate } from "@formbricks/types/templates";
|
||||||
import { TUser } from "@formbricks/types/user";
|
import { TUser } from "@formbricks/types/user";
|
||||||
import { replacePresetPlaceholders } from "@/app/(app)/(onboarding)/environments/[environmentId]/xm-templates/lib/utils";
|
import { TWorkspace } from "@formbricks/types/workspace";
|
||||||
import { getXMTemplates } from "@/app/(app)/(onboarding)/environments/[environmentId]/xm-templates/lib/xm-templates";
|
|
||||||
import { OnboardingOptionsContainer } from "@/app/(app)/(onboarding)/organizations/components/OnboardingOptionsContainer";
|
import { OnboardingOptionsContainer } from "@/app/(app)/(onboarding)/organizations/components/OnboardingOptionsContainer";
|
||||||
|
import { replacePresetPlaceholders } from "@/app/(app)/(onboarding)/workspaces/[workspaceId]/xm-templates/lib/utils";
|
||||||
|
import { getXMTemplates } from "@/app/(app)/(onboarding)/workspaces/[workspaceId]/xm-templates/lib/xm-templates";
|
||||||
import { getFormattedErrorMessage } from "@/lib/utils/helper";
|
import { getFormattedErrorMessage } from "@/lib/utils/helper";
|
||||||
import { createSurveyAction } from "@/modules/survey/components/template-list/actions";
|
import { createSurveyAction } from "@/modules/survey/components/template-list/actions";
|
||||||
|
|
||||||
interface XMTemplateListProps {
|
interface XMTemplateListProps {
|
||||||
project: TProject;
|
workspace: TWorkspace;
|
||||||
user: TUser;
|
user: TUser;
|
||||||
environmentId: string;
|
workspaceId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const XMTemplateList = ({ project, user, environmentId }: XMTemplateListProps) => {
|
export const XMTemplateList = ({ workspace, user, workspaceId }: XMTemplateListProps) => {
|
||||||
const [activeTemplateId, setActiveTemplateId] = useState<number | null>(null);
|
const [activeTemplateId, setActiveTemplateId] = useState<number | null>(null);
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -33,12 +33,12 @@ export const XMTemplateList = ({ project, user, environmentId }: XMTemplateListP
|
|||||||
createdBy: user.id,
|
createdBy: user.id,
|
||||||
};
|
};
|
||||||
const createSurveyResponse = await createSurveyAction({
|
const createSurveyResponse = await createSurveyAction({
|
||||||
environmentId: environmentId,
|
workspaceId: workspaceId,
|
||||||
surveyBody: augmentedTemplate,
|
surveyBody: augmentedTemplate,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (createSurveyResponse?.data) {
|
if (createSurveyResponse?.data) {
|
||||||
router.push(`/environments/${environmentId}/surveys/${createSurveyResponse.data.id}/edit?mode=cx`);
|
router.push(`/workspaces/${workspaceId}/surveys/${createSurveyResponse.data.id}/edit?mode=cx`);
|
||||||
} else {
|
} else {
|
||||||
const errorMessage = getFormattedErrorMessage(createSurveyResponse);
|
const errorMessage = getFormattedErrorMessage(createSurveyResponse);
|
||||||
toast.error(errorMessage);
|
toast.error(errorMessage);
|
||||||
@@ -48,49 +48,49 @@ export const XMTemplateList = ({ project, user, environmentId }: XMTemplateListP
|
|||||||
const handleTemplateClick = (templateIdx: number) => {
|
const handleTemplateClick = (templateIdx: number) => {
|
||||||
setActiveTemplateId(templateIdx);
|
setActiveTemplateId(templateIdx);
|
||||||
const template = getXMTemplates(t)[templateIdx];
|
const template = getXMTemplates(t)[templateIdx];
|
||||||
const newTemplate = replacePresetPlaceholders(template, project);
|
const newTemplate = replacePresetPlaceholders(template, workspace);
|
||||||
createSurvey(newTemplate);
|
createSurvey(newTemplate);
|
||||||
};
|
};
|
||||||
|
|
||||||
const XMTemplateOptions = [
|
const XMTemplateOptions = [
|
||||||
{
|
{
|
||||||
title: t("environments.xm-templates.nps"),
|
title: t("workspace.xm-templates.nps"),
|
||||||
description: t("environments.xm-templates.nps_description"),
|
description: t("workspace.xm-templates.nps_description"),
|
||||||
icon: ShoppingCartIcon,
|
icon: ShoppingCartIcon,
|
||||||
onClick: () => handleTemplateClick(0),
|
onClick: () => handleTemplateClick(0),
|
||||||
isLoading: activeTemplateId === 0,
|
isLoading: activeTemplateId === 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: t("environments.xm-templates.five_star_rating"),
|
title: t("workspace.xm-templates.five_star_rating"),
|
||||||
description: t("environments.xm-templates.five_star_rating_description"),
|
description: t("workspace.xm-templates.five_star_rating_description"),
|
||||||
icon: StarIcon,
|
icon: StarIcon,
|
||||||
onClick: () => handleTemplateClick(1),
|
onClick: () => handleTemplateClick(1),
|
||||||
isLoading: activeTemplateId === 1,
|
isLoading: activeTemplateId === 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: t("environments.xm-templates.csat"),
|
title: t("workspace.xm-templates.csat"),
|
||||||
description: t("environments.xm-templates.csat_description"),
|
description: t("workspace.xm-templates.csat_description"),
|
||||||
icon: ThumbsUpIcon,
|
icon: ThumbsUpIcon,
|
||||||
onClick: () => handleTemplateClick(2),
|
onClick: () => handleTemplateClick(2),
|
||||||
isLoading: activeTemplateId === 2,
|
isLoading: activeTemplateId === 2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: t("environments.xm-templates.ces"),
|
title: t("workspace.xm-templates.ces"),
|
||||||
description: t("environments.xm-templates.ces_description"),
|
description: t("workspace.xm-templates.ces_description"),
|
||||||
icon: ActivityIcon,
|
icon: ActivityIcon,
|
||||||
onClick: () => handleTemplateClick(3),
|
onClick: () => handleTemplateClick(3),
|
||||||
isLoading: activeTemplateId === 3,
|
isLoading: activeTemplateId === 3,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: t("environments.xm-templates.smileys"),
|
title: t("workspace.xm-templates.smileys"),
|
||||||
description: t("environments.xm-templates.smileys_description"),
|
description: t("workspace.xm-templates.smileys_description"),
|
||||||
icon: SmileIcon,
|
icon: SmileIcon,
|
||||||
onClick: () => handleTemplateClick(4),
|
onClick: () => handleTemplateClick(4),
|
||||||
isLoading: activeTemplateId === 4,
|
isLoading: activeTemplateId === 4,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: t("environments.xm-templates.enps"),
|
title: t("workspace.xm-templates.enps"),
|
||||||
description: t("environments.xm-templates.enps_description"),
|
description: t("workspace.xm-templates.enps_description"),
|
||||||
icon: UsersIcon,
|
icon: UsersIcon,
|
||||||
onClick: () => handleTemplateClick(5),
|
onClick: () => handleTemplateClick(5),
|
||||||
isLoading: activeTemplateId === 5,
|
isLoading: activeTemplateId === 5,
|
||||||
+17
-16
@@ -1,16 +1,17 @@
|
|||||||
import "@testing-library/jest-dom/vitest";
|
import "@testing-library/jest-dom/vitest";
|
||||||
import { cleanup } from "@testing-library/react";
|
import { cleanup } from "@testing-library/react";
|
||||||
import { afterEach, describe, expect, test } from "vitest";
|
import { afterEach, describe, expect, test } from "vitest";
|
||||||
import { TProject } from "@formbricks/types/project";
|
import { TSurveyElementTypeEnum } from "@formbricks/types/surveys/constants";
|
||||||
import { TXMTemplate } from "@formbricks/types/templates";
|
import { TXMTemplate } from "@formbricks/types/templates";
|
||||||
|
import { TWorkspace } from "@formbricks/types/workspace";
|
||||||
import { replacePresetPlaceholders } from "./utils";
|
import { replacePresetPlaceholders } from "./utils";
|
||||||
|
|
||||||
// Mock data
|
// Mock data
|
||||||
const mockProject: TProject = {
|
const mockWorkspace: TWorkspace = {
|
||||||
id: "project1",
|
id: "workspace1",
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
name: "Test Project",
|
name: "Test Workspace",
|
||||||
organizationId: "org1",
|
organizationId: "org1",
|
||||||
styling: {
|
styling: {
|
||||||
allowStyleOverwrite: true,
|
allowStyleOverwrite: true,
|
||||||
@@ -26,12 +27,12 @@ const mockProject: TProject = {
|
|||||||
placement: "bottomRight",
|
placement: "bottomRight",
|
||||||
clickOutsideClose: true,
|
clickOutsideClose: true,
|
||||||
overlay: "none",
|
overlay: "none",
|
||||||
environments: [],
|
appSetupCompleted: false,
|
||||||
languages: [],
|
languages: [],
|
||||||
logo: null,
|
logo: null,
|
||||||
};
|
};
|
||||||
const mockTemplate: TXMTemplate = {
|
const mockTemplate: TXMTemplate = {
|
||||||
name: "$[projectName] Survey",
|
name: "$[workspaceName] Survey",
|
||||||
blocks: [
|
blocks: [
|
||||||
{
|
{
|
||||||
id: "block1",
|
id: "block1",
|
||||||
@@ -39,13 +40,13 @@ const mockTemplate: TXMTemplate = {
|
|||||||
elements: [
|
elements: [
|
||||||
{
|
{
|
||||||
id: "q1",
|
id: "q1",
|
||||||
type: "openText" as const,
|
type: "openText" as TSurveyElementTypeEnum.OpenText,
|
||||||
inputType: "text" as const,
|
inputType: "text" as const,
|
||||||
headline: { default: "$[projectName] Question" },
|
headline: { default: "$[workspaceName] Question" },
|
||||||
subheader: { default: "" },
|
subheader: { default: "" },
|
||||||
required: false,
|
required: false,
|
||||||
placeholder: { default: "" },
|
placeholder: { default: "" },
|
||||||
charLimit: 1000,
|
charLimit: { enabled: true, max: 1000 },
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -69,19 +70,19 @@ describe("replacePresetPlaceholders", () => {
|
|||||||
cleanup();
|
cleanup();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("replaces projectName placeholder in template name", () => {
|
test("replaces workspaceName placeholder in template name", () => {
|
||||||
const result = replacePresetPlaceholders(mockTemplate, mockProject);
|
const result = replacePresetPlaceholders(mockTemplate, mockWorkspace);
|
||||||
expect(result.name).toBe("Test Project Survey");
|
expect(result.name).toBe("Test Workspace Survey");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("replaces projectName placeholder in element headline", () => {
|
test("replaces workspaceName placeholder in element headline", () => {
|
||||||
const result = replacePresetPlaceholders(mockTemplate, mockProject);
|
const result = replacePresetPlaceholders(mockTemplate, mockWorkspace);
|
||||||
expect(result.blocks[0].elements[0].headline.default).toBe("Test Project Question");
|
expect(result.blocks[0].elements[0].headline.default).toBe("Test Workspace Question");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("returns a new object without mutating the original template", () => {
|
test("returns a new object without mutating the original template", () => {
|
||||||
const originalTemplate = structuredClone(mockTemplate);
|
const originalTemplate = structuredClone(mockTemplate);
|
||||||
const result = replacePresetPlaceholders(mockTemplate, mockProject);
|
const result = replacePresetPlaceholders(mockTemplate, mockWorkspace);
|
||||||
expect(result).not.toBe(mockTemplate);
|
expect(result).not.toBe(mockTemplate);
|
||||||
expect(mockTemplate).toEqual(originalTemplate);
|
expect(mockTemplate).toEqual(originalTemplate);
|
||||||
});
|
});
|
||||||
+5
-5
@@ -1,16 +1,16 @@
|
|||||||
import { TProject } from "@formbricks/types/project";
|
|
||||||
import { TSurveyBlock } from "@formbricks/types/surveys/blocks";
|
import { TSurveyBlock } from "@formbricks/types/surveys/blocks";
|
||||||
import { TXMTemplate } from "@formbricks/types/templates";
|
import { TXMTemplate } from "@formbricks/types/templates";
|
||||||
|
import { TWorkspace } from "@formbricks/types/workspace";
|
||||||
import { replaceElementPresetPlaceholders } from "@/lib/utils/templates";
|
import { replaceElementPresetPlaceholders } from "@/lib/utils/templates";
|
||||||
|
|
||||||
// replace all occurences of projectName with the actual project name in the current template
|
// replace all occurences of workspaceName with the actual workspace name in the current template
|
||||||
export const replacePresetPlaceholders = (template: TXMTemplate, project: TProject): TXMTemplate => {
|
export const replacePresetPlaceholders = (template: TXMTemplate, workspace: TWorkspace): TXMTemplate => {
|
||||||
const survey = structuredClone(template);
|
const survey = structuredClone(template);
|
||||||
|
|
||||||
const modifiedBlocks = survey.blocks.map((block: TSurveyBlock) => ({
|
const modifiedBlocks = survey.blocks.map((block: TSurveyBlock) => ({
|
||||||
...block,
|
...block,
|
||||||
elements: block.elements.map((element) => replaceElementPresetPlaceholders(element, project)),
|
elements: block.elements.map((element) => replaceElementPresetPlaceholders(element, workspace)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return { ...survey, name: survey.name.replace("$[projectName]", project.name), blocks: modifiedBlocks };
|
return { ...survey, name: survey.name.replace("$[workspaceName]", workspace.name), blocks: modifiedBlocks };
|
||||||
};
|
};
|
||||||
+3
-3
@@ -14,7 +14,7 @@ describe("xm-templates", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("getXMSurveyDefault returns default survey template", () => {
|
test("getXMSurveyDefault returns default survey template", () => {
|
||||||
const tMock = vi.fn((key) => key) as TFunction;
|
const tMock = vi.fn((key: string) => key) as unknown as TFunction;
|
||||||
const result = getXMSurveyDefault(tMock);
|
const result = getXMSurveyDefault(tMock);
|
||||||
|
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
@@ -29,7 +29,7 @@ describe("xm-templates", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("getXMTemplates returns all templates", () => {
|
test("getXMTemplates returns all templates", () => {
|
||||||
const tMock = vi.fn((key) => key) as TFunction;
|
const tMock = vi.fn((key: string) => key) as unknown as TFunction;
|
||||||
const result = getXMTemplates(tMock);
|
const result = getXMTemplates(tMock);
|
||||||
|
|
||||||
expect(result).toHaveLength(6);
|
expect(result).toHaveLength(6);
|
||||||
@@ -44,7 +44,7 @@ describe("xm-templates", () => {
|
|||||||
test("getXMTemplates handles errors gracefully", async () => {
|
test("getXMTemplates handles errors gracefully", async () => {
|
||||||
const tMock = vi.fn(() => {
|
const tMock = vi.fn(() => {
|
||||||
throw new Error("Test error");
|
throw new Error("Test error");
|
||||||
}) as TFunction;
|
}) as unknown as TFunction;
|
||||||
|
|
||||||
const result = getXMTemplates(tMock);
|
const result = getXMTemplates(tMock);
|
||||||
|
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import { XIcon } from "lucide-react";
|
||||||
|
import { getServerSession } from "next-auth";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { AuthenticationError, ResourceNotFoundError } from "@formbricks/types/errors";
|
||||||
|
import { XMTemplateList } from "@/app/(app)/(onboarding)/workspaces/[workspaceId]/xm-templates/components/XMTemplateList";
|
||||||
|
import { getUser } from "@/lib/user/service";
|
||||||
|
import { getUserWorkspaces, getWorkspace } from "@/lib/workspace/service";
|
||||||
|
import { getTranslate } from "@/lingodotdev/server";
|
||||||
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
||||||
|
import { Button } from "@/modules/ui/components/button";
|
||||||
|
import { Header } from "@/modules/ui/components/header";
|
||||||
|
|
||||||
|
interface XMTemplatePageProps {
|
||||||
|
params: Promise<{
|
||||||
|
workspaceId: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Page = async (props: XMTemplatePageProps) => {
|
||||||
|
const params = await props.params;
|
||||||
|
const session = await getServerSession(authOptions);
|
||||||
|
const t = await getTranslate();
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
throw new AuthenticationError(t("common.not_authenticated"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await getUser(session.user.id);
|
||||||
|
if (!user) {
|
||||||
|
throw new AuthenticationError(t("common.not_authenticated"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspace = await getWorkspace(params.workspaceId);
|
||||||
|
if (!workspace) {
|
||||||
|
throw new ResourceNotFoundError(t("common.workspace"), params.workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspaces = await getUserWorkspaces(session.user.id, workspace.organizationId);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex min-h-full min-w-full flex-col items-center justify-center space-y-12">
|
||||||
|
<Header title={t("workspace.xm-templates.headline")} />
|
||||||
|
<XMTemplateList workspace={workspace} user={user} workspaceId={params.workspaceId} />
|
||||||
|
{workspaces.length >= 2 && (
|
||||||
|
<Button
|
||||||
|
className="absolute right-5 top-5 !mt-0 text-slate-500 hover:text-slate-700"
|
||||||
|
variant="ghost"
|
||||||
|
asChild>
|
||||||
|
<Link href={`/workspaces/${params.workspaceId}/surveys`}>
|
||||||
|
<XIcon className="h-7 w-7" strokeWidth={1.5} />
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Page;
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
import { redirect } from "next/navigation";
|
|
||||||
import { getEnvironment } from "@/lib/environment/service";
|
|
||||||
import { environmentIdLayoutChecks } from "@/modules/environments/lib/utils";
|
|
||||||
|
|
||||||
const SurveyEditorEnvironmentLayout = async (props) => {
|
|
||||||
const params = await props.params;
|
|
||||||
|
|
||||||
const { children } = props;
|
|
||||||
|
|
||||||
const { t, session, user } = await environmentIdLayoutChecks(params.environmentId);
|
|
||||||
|
|
||||||
if (!session) {
|
|
||||||
return redirect(`/auth/login`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
throw new Error(t("common.user_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
const environment = await getEnvironment(params.environmentId);
|
|
||||||
|
|
||||||
if (!environment) {
|
|
||||||
throw new Error(t("common.environment_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex h-screen flex-col">
|
|
||||||
<div className="h-full overflow-y-auto bg-slate-50">{children}</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default SurveyEditorEnvironmentLayout;
|
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { AuthenticationError, ResourceNotFoundError } from "@formbricks/types/errors";
|
||||||
|
import { getWorkspace } from "@/lib/workspace/service";
|
||||||
|
import { workspaceIdLayoutChecks } from "@/modules/workspaces/lib/utils";
|
||||||
|
|
||||||
|
const SurveyEditorWorkspaceLayout = async (props: {
|
||||||
|
params: Promise<{ workspaceId: string }>;
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) => {
|
||||||
|
const params = await props.params;
|
||||||
|
|
||||||
|
const { children } = props;
|
||||||
|
|
||||||
|
const { t, session, user } = await workspaceIdLayoutChecks(params.workspaceId);
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
return redirect(`/auth/login`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new AuthenticationError(t("common.not_authenticated"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspace = await getWorkspace(params.workspaceId);
|
||||||
|
|
||||||
|
if (!workspace) {
|
||||||
|
throw new ResourceNotFoundError(t("common.workspace"), params.workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex h-screen flex-col">
|
||||||
|
<div className="h-full overflow-y-auto bg-slate-50">{children}</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SurveyEditorWorkspaceLayout;
|
||||||
@@ -6,15 +6,24 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { Button } from "@/modules/ui/components/button";
|
import { Button } from "@/modules/ui/components/button";
|
||||||
import { Confetti } from "@/modules/ui/components/confetti";
|
import { Confetti } from "@/modules/ui/components/confetti";
|
||||||
|
|
||||||
interface ConfirmationPageProps {
|
const BILLING_CONFIRMATION_WORKSPACE_ID_KEY = "billingConfirmationWorkspaceId";
|
||||||
environmentId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ConfirmationPage = ({ environmentId }: ConfirmationPageProps) => {
|
export const ConfirmationPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [showConfetti, setShowConfetti] = useState(false);
|
const [showConfetti, setShowConfetti] = useState(false);
|
||||||
|
const [resolvedWorkspaceId, setResolvedWorkspaceId] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setShowConfetti(true);
|
setShowConfetti(true);
|
||||||
|
|
||||||
|
if (globalThis.window === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const storedWorkspaceId = globalThis.window.sessionStorage.getItem(BILLING_CONFIRMATION_WORKSPACE_ID_KEY);
|
||||||
|
if (storedWorkspaceId) {
|
||||||
|
setResolvedWorkspaceId(storedWorkspaceId);
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -30,7 +39,7 @@ export const ConfirmationPage = ({ environmentId }: ConfirmationPageProps) => {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<Button asChild className="w-full justify-center">
|
<Button asChild className="w-full justify-center">
|
||||||
<Link href={`/environments/${environmentId}/settings/billing`}>
|
<Link href={resolvedWorkspaceId ? `/workspaces/${resolvedWorkspaceId}/settings/billing` : "/"}>
|
||||||
{t("billing_confirmation.back_to_billing_overview")}
|
{t("billing_confirmation.back_to_billing_overview")}
|
||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -3,13 +3,10 @@ import { PageContentWrapper } from "@/modules/ui/components/page-content-wrapper
|
|||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
const Page = async (props) => {
|
const Page = async () => {
|
||||||
const searchParams = await props.searchParams;
|
|
||||||
const { environmentId } = searchParams;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageContentWrapper>
|
<PageContentWrapper>
|
||||||
<ConfirmationPage environmentId={environmentId?.toString()} />
|
<ConfirmationPage />
|
||||||
</PageContentWrapper>
|
</PageContentWrapper>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { SettingsCard } from "@/app/(app)/environments/[environmentId]/settings/components/SettingsCard";
|
import { SettingsCard } from "@/app/(app)/workspaces/[workspaceId]/settings/components/SettingsCard";
|
||||||
import { cn } from "@/lib/cn";
|
import { cn } from "@/lib/cn";
|
||||||
|
|
||||||
export const LoadingCard = ({
|
export const LoadingCard = ({
|
||||||
|
|||||||
@@ -1,145 +0,0 @@
|
|||||||
"use server";
|
|
||||||
|
|
||||||
import { z } from "zod";
|
|
||||||
import { ZId } from "@formbricks/types/common";
|
|
||||||
import { AuthorizationError, OperationNotAllowedError } from "@formbricks/types/errors";
|
|
||||||
import { ZProjectUpdateInput } from "@formbricks/types/project";
|
|
||||||
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
|
||||||
import { getOrganization } from "@/lib/organization/service";
|
|
||||||
import { getOrganizationProjectsCount } from "@/lib/project/service";
|
|
||||||
import { updateUser } from "@/lib/user/service";
|
|
||||||
import { authenticatedActionClient } from "@/lib/utils/action-client";
|
|
||||||
import { checkAuthorizationUpdated } from "@/lib/utils/action-client/action-client-middleware";
|
|
||||||
import { AuthenticatedActionClientCtx } from "@/lib/utils/action-client/types/context";
|
|
||||||
import { withAuditLogging } from "@/modules/ee/audit-logs/lib/handler";
|
|
||||||
import {
|
|
||||||
getAccessControlPermission,
|
|
||||||
getOrganizationProjectsLimit,
|
|
||||||
} from "@/modules/ee/license-check/lib/utils";
|
|
||||||
import { createProject } from "@/modules/projects/settings/lib/project";
|
|
||||||
import { getOrganizationsByUserId } from "./lib/organization";
|
|
||||||
import { getProjectsByUserId } from "./lib/project";
|
|
||||||
|
|
||||||
const ZCreateProjectAction = z.object({
|
|
||||||
organizationId: ZId,
|
|
||||||
data: ZProjectUpdateInput,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const createProjectAction = authenticatedActionClient.inputSchema(ZCreateProjectAction).action(
|
|
||||||
withAuditLogging(
|
|
||||||
"created",
|
|
||||||
"project",
|
|
||||||
async ({ ctx, parsedInput }: { ctx: AuthenticatedActionClientCtx; parsedInput: Record<string, any> }) => {
|
|
||||||
const { user } = ctx;
|
|
||||||
|
|
||||||
const organizationId = parsedInput.organizationId;
|
|
||||||
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: user.id,
|
|
||||||
organizationId: parsedInput.organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
data: parsedInput.data,
|
|
||||||
schema: ZProjectUpdateInput,
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner", "manager"],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
const organization = await getOrganization(organizationId);
|
|
||||||
|
|
||||||
if (!organization) {
|
|
||||||
throw new Error("Organization not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
const organizationProjectsLimit = await getOrganizationProjectsLimit(organization.billing.limits);
|
|
||||||
const organizationProjectsCount = await getOrganizationProjectsCount(organization.id);
|
|
||||||
|
|
||||||
if (organizationProjectsCount >= organizationProjectsLimit) {
|
|
||||||
throw new OperationNotAllowedError("Organization workspace limit reached");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsedInput.data.teamIds && parsedInput.data.teamIds.length > 0) {
|
|
||||||
const isAccessControlAllowed = await getAccessControlPermission(organization.billing.plan);
|
|
||||||
|
|
||||||
if (!isAccessControlAllowed) {
|
|
||||||
throw new OperationNotAllowedError("You do not have permission to manage roles");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const project = await createProject(parsedInput.organizationId, parsedInput.data);
|
|
||||||
const updatedNotificationSettings = {
|
|
||||||
...user.notificationSettings,
|
|
||||||
alert: {
|
|
||||||
...user.notificationSettings?.alert,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
await updateUser(user.id, {
|
|
||||||
notificationSettings: updatedNotificationSettings,
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.auditLoggingCtx.organizationId = organizationId;
|
|
||||||
ctx.auditLoggingCtx.projectId = project.id;
|
|
||||||
ctx.auditLoggingCtx.newObject = project;
|
|
||||||
return project;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const ZGetOrganizationsForSwitcherAction = z.object({
|
|
||||||
organizationId: ZId, // Changed from environmentId to avoid extra query
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches organizations list for switcher dropdown.
|
|
||||||
* Called on-demand when user opens the organization switcher.
|
|
||||||
*/
|
|
||||||
export const getOrganizationsForSwitcherAction = authenticatedActionClient
|
|
||||||
.inputSchema(ZGetOrganizationsForSwitcherAction)
|
|
||||||
.action(async ({ ctx, parsedInput }) => {
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user.id,
|
|
||||||
organizationId: parsedInput.organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner", "manager", "member", "billing"],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
return await getOrganizationsByUserId(ctx.user.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
const ZGetProjectsForSwitcherAction = z.object({
|
|
||||||
organizationId: ZId, // Changed from environmentId to avoid extra query
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches projects list for switcher dropdown.
|
|
||||||
* Called on-demand when user opens the project switcher.
|
|
||||||
*/
|
|
||||||
export const getProjectsForSwitcherAction = authenticatedActionClient
|
|
||||||
.inputSchema(ZGetProjectsForSwitcherAction)
|
|
||||||
.action(async ({ ctx, parsedInput }) => {
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user.id,
|
|
||||||
organizationId: parsedInput.organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner", "manager", "member", "billing"],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
// Need membership for getProjectsByUserId (1 DB query)
|
|
||||||
const membership = await getMembershipByUserIdOrganizationId(ctx.user.id, parsedInput.organizationId);
|
|
||||||
if (!membership) {
|
|
||||||
throw new AuthorizationError("Membership not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
return await getProjectsByUserId(ctx.user.id, membership);
|
|
||||||
});
|
|
||||||
-18
@@ -1,18 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useEffect } from "react";
|
|
||||||
import { FORMBRICKS_ENVIRONMENT_ID_LS } from "@/lib/localStorage";
|
|
||||||
|
|
||||||
interface EnvironmentStorageHandlerProps {
|
|
||||||
environmentId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const EnvironmentStorageHandler = ({ environmentId }: EnvironmentStorageHandlerProps) => {
|
|
||||||
useEffect(() => {
|
|
||||||
localStorage.setItem(FORMBRICKS_ENVIRONMENT_ID_LS, environmentId);
|
|
||||||
}, [environmentId]);
|
|
||||||
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default EnvironmentStorageHandler;
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { useState } from "react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { TEnvironment } from "@formbricks/types/environment";
|
|
||||||
import { cn } from "@/lib/cn";
|
|
||||||
import { Label } from "@/modules/ui/components/label";
|
|
||||||
import { Switch } from "@/modules/ui/components/switch";
|
|
||||||
|
|
||||||
interface EnvironmentSwitchProps {
|
|
||||||
environment: TEnvironment;
|
|
||||||
environments: TEnvironment[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export const EnvironmentSwitch = ({ environment, environments }: EnvironmentSwitchProps) => {
|
|
||||||
const { t } = useTranslation();
|
|
||||||
const router = useRouter();
|
|
||||||
const [isEnvSwitchChecked, setIsEnvSwitchChecked] = useState(environment?.type === "development");
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
|
|
||||||
const handleEnvironmentChange = (environmentType: "production" | "development") => {
|
|
||||||
const newEnvironmentId = environments.find((e) => e.type === environmentType)?.id;
|
|
||||||
if (newEnvironmentId) {
|
|
||||||
router.push(`/environments/${newEnvironmentId}/`);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const toggleEnvSwitch = () => {
|
|
||||||
const newEnvironmentType = isEnvSwitchChecked ? "production" : "development";
|
|
||||||
setIsLoading(true);
|
|
||||||
setIsEnvSwitchChecked(!isEnvSwitchChecked);
|
|
||||||
handleEnvironmentChange(newEnvironmentType);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
"flex items-center space-x-2 rounded-lg p-2",
|
|
||||||
isEnvSwitchChecked ? "bg-slate-100 text-orange-800" : "hover:bg-slate-100"
|
|
||||||
)}>
|
|
||||||
<Label
|
|
||||||
htmlFor="development-mode"
|
|
||||||
className={cn("hover:cursor-pointer", isEnvSwitchChecked && "text-orange-800")}>
|
|
||||||
{t("common.dev_env")}
|
|
||||||
</Label>
|
|
||||||
<Switch
|
|
||||||
className="focus:ring-orange-800 data-[state=checked]:bg-orange-800"
|
|
||||||
id="development-mode"
|
|
||||||
disabled={isLoading}
|
|
||||||
checked={isEnvSwitchChecked}
|
|
||||||
onCheckedChange={toggleEnvSwitch}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,324 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import {
|
|
||||||
ArrowUpRightIcon,
|
|
||||||
ChevronRightIcon,
|
|
||||||
Cog,
|
|
||||||
LogOutIcon,
|
|
||||||
MessageCircle,
|
|
||||||
PanelLeftCloseIcon,
|
|
||||||
PanelLeftOpenIcon,
|
|
||||||
RocketIcon,
|
|
||||||
UserCircleIcon,
|
|
||||||
UserIcon,
|
|
||||||
WorkflowIcon,
|
|
||||||
} from "lucide-react";
|
|
||||||
import Image from "next/image";
|
|
||||||
import Link from "next/link";
|
|
||||||
import { usePathname, useRouter } from "next/navigation";
|
|
||||||
import { useEffect, useMemo, useState } from "react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { TEnvironment } from "@formbricks/types/environment";
|
|
||||||
import { TOrganizationRole } from "@formbricks/types/memberships";
|
|
||||||
import { TOrganization } from "@formbricks/types/organizations";
|
|
||||||
import { TUser } from "@formbricks/types/user";
|
|
||||||
import { NavigationLink } from "@/app/(app)/environments/[environmentId]/components/NavigationLink";
|
|
||||||
import { isNewerVersion } from "@/app/(app)/environments/[environmentId]/lib/utils";
|
|
||||||
import FBLogo from "@/images/formbricks-wordmark.svg";
|
|
||||||
import { cn } from "@/lib/cn";
|
|
||||||
import { getAccessFlags } from "@/lib/membership/utils";
|
|
||||||
import { useSignOut } from "@/modules/auth/hooks/use-sign-out";
|
|
||||||
import { getLatestStableFbReleaseAction } from "@/modules/projects/settings/(setup)/app-connection/actions";
|
|
||||||
import { ProfileAvatar } from "@/modules/ui/components/avatars";
|
|
||||||
import { Button } from "@/modules/ui/components/button";
|
|
||||||
import {
|
|
||||||
DropdownMenu,
|
|
||||||
DropdownMenuContent,
|
|
||||||
DropdownMenuItem,
|
|
||||||
DropdownMenuTrigger,
|
|
||||||
} from "@/modules/ui/components/dropdown-menu";
|
|
||||||
import packageJson from "../../../../../package.json";
|
|
||||||
|
|
||||||
interface NavigationProps {
|
|
||||||
environment: TEnvironment;
|
|
||||||
user: TUser;
|
|
||||||
organization: TOrganization;
|
|
||||||
project: { id: string; name: string };
|
|
||||||
isFormbricksCloud: boolean;
|
|
||||||
isDevelopment: boolean;
|
|
||||||
membershipRole?: TOrganizationRole;
|
|
||||||
publicDomain: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const MainNavigation = ({
|
|
||||||
environment,
|
|
||||||
organization,
|
|
||||||
user,
|
|
||||||
project,
|
|
||||||
membershipRole,
|
|
||||||
isFormbricksCloud,
|
|
||||||
isDevelopment,
|
|
||||||
publicDomain,
|
|
||||||
}: NavigationProps) => {
|
|
||||||
const router = useRouter();
|
|
||||||
const pathname = usePathname();
|
|
||||||
const { t } = useTranslation();
|
|
||||||
const [isCollapsed, setIsCollapsed] = useState(false);
|
|
||||||
const [isTextVisible, setIsTextVisible] = useState(true);
|
|
||||||
const [latestVersion, setLatestVersion] = useState("");
|
|
||||||
const { signOut: signOutWithAudit } = useSignOut({ id: user.id, email: user.email });
|
|
||||||
|
|
||||||
const { isManager, isOwner, isBilling } = getAccessFlags(membershipRole);
|
|
||||||
|
|
||||||
const isOwnerOrManager = isManager || isOwner;
|
|
||||||
|
|
||||||
const toggleSidebar = () => {
|
|
||||||
setIsCollapsed(!isCollapsed);
|
|
||||||
localStorage.setItem("isMainNavCollapsed", isCollapsed ? "false" : "true");
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const isCollapsedValueFromLocalStorage = localStorage.getItem("isMainNavCollapsed") === "true";
|
|
||||||
setIsCollapsed(isCollapsedValueFromLocalStorage);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const toggleTextOpacity = () => {
|
|
||||||
setIsTextVisible(isCollapsed);
|
|
||||||
};
|
|
||||||
const timeoutId = setTimeout(toggleTextOpacity, 150);
|
|
||||||
return () => clearTimeout(timeoutId);
|
|
||||||
}, [isCollapsed]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
// Auto collapse project navbar on org and account settings
|
|
||||||
if (pathname?.includes("/settings")) {
|
|
||||||
setIsCollapsed(true);
|
|
||||||
}
|
|
||||||
}, [pathname]);
|
|
||||||
|
|
||||||
const mainNavigation = useMemo(
|
|
||||||
() => [
|
|
||||||
{
|
|
||||||
name: t("common.surveys"),
|
|
||||||
href: `/environments/${environment.id}/surveys`,
|
|
||||||
icon: MessageCircle,
|
|
||||||
isActive: pathname?.includes("/surveys"),
|
|
||||||
isHidden: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
href: `/environments/${environment.id}/contacts`,
|
|
||||||
name: t("common.contacts"),
|
|
||||||
icon: UserIcon,
|
|
||||||
isActive:
|
|
||||||
pathname?.includes("/contacts") ||
|
|
||||||
pathname?.includes("/segments") ||
|
|
||||||
pathname?.includes("/attributes"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: t("common.workflows"),
|
|
||||||
href: `/environments/${environment.id}/workflows`,
|
|
||||||
icon: WorkflowIcon,
|
|
||||||
isActive: pathname?.includes("/workflows"),
|
|
||||||
isHidden: !isFormbricksCloud,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: t("common.configuration"),
|
|
||||||
href: `/environments/${environment.id}/workspace/general`,
|
|
||||||
icon: Cog,
|
|
||||||
isActive: pathname?.includes("/project"),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
[t, environment.id, pathname, isFormbricksCloud]
|
|
||||||
);
|
|
||||||
|
|
||||||
const dropdownNavigation = [
|
|
||||||
{
|
|
||||||
label: t("common.account"),
|
|
||||||
href: `/environments/${environment.id}/settings/profile`,
|
|
||||||
icon: UserCircleIcon,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: t("common.documentation"),
|
|
||||||
href: "https://formbricks.com/docs",
|
|
||||||
target: "_blank",
|
|
||||||
icon: ArrowUpRightIcon,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: t("common.share_feedback"),
|
|
||||||
href: "https://github.com/formbricks/formbricks/issues",
|
|
||||||
target: "_blank",
|
|
||||||
icon: ArrowUpRightIcon,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
async function loadReleases() {
|
|
||||||
const res = await getLatestStableFbReleaseAction();
|
|
||||||
if (res?.data) {
|
|
||||||
const latestVersionTag = res.data;
|
|
||||||
const currentVersionTag = `v${packageJson.version}`;
|
|
||||||
|
|
||||||
if (isNewerVersion(currentVersionTag, latestVersionTag)) {
|
|
||||||
setLatestVersion(latestVersionTag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isOwnerOrManager) loadReleases();
|
|
||||||
}, [isOwnerOrManager]);
|
|
||||||
|
|
||||||
const mainNavigationLink = `/environments/${environment.id}/${isBilling ? "settings/billing/" : "surveys/"}`;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{project && (
|
|
||||||
<aside
|
|
||||||
className={cn(
|
|
||||||
"z-40 flex flex-col justify-between rounded-r-xl border-r border-slate-200 bg-white pt-3 shadow-md transition-all duration-100",
|
|
||||||
isCollapsed ? "w-sidebar-expanded" : "w-sidebar-collapsed"
|
|
||||||
)}>
|
|
||||||
<div>
|
|
||||||
{/* Logo and Toggle */}
|
|
||||||
|
|
||||||
<div className="flex items-center justify-between px-3 pb-4">
|
|
||||||
{!isCollapsed && (
|
|
||||||
<Link
|
|
||||||
href={mainNavigationLink}
|
|
||||||
className={cn(
|
|
||||||
"flex items-center justify-center transition-opacity duration-100",
|
|
||||||
isTextVisible ? "opacity-0" : "opacity-100"
|
|
||||||
)}>
|
|
||||||
<Image src={FBLogo} width={160} height={30} alt={t("environments.formbricks_logo")} />
|
|
||||||
</Link>
|
|
||||||
)}
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
size="icon"
|
|
||||||
onClick={toggleSidebar}
|
|
||||||
className={cn(
|
|
||||||
"rounded-xl bg-slate-50 p-1 text-slate-600 transition-all hover:bg-slate-100 focus:outline-none focus:ring-0 focus:ring-transparent"
|
|
||||||
)}>
|
|
||||||
{isCollapsed ? (
|
|
||||||
<PanelLeftOpenIcon strokeWidth={1.5} />
|
|
||||||
) : (
|
|
||||||
<PanelLeftCloseIcon strokeWidth={1.5} />
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Main Nav Switch */}
|
|
||||||
{!isBilling && (
|
|
||||||
<ul>
|
|
||||||
{mainNavigation.map(
|
|
||||||
(item) =>
|
|
||||||
!item.isHidden && (
|
|
||||||
<NavigationLink
|
|
||||||
key={item.name}
|
|
||||||
href={item.href}
|
|
||||||
isActive={item.isActive}
|
|
||||||
isCollapsed={isCollapsed}
|
|
||||||
isTextVisible={isTextVisible}
|
|
||||||
linkText={item.name}>
|
|
||||||
<item.icon strokeWidth={1.5} />
|
|
||||||
</NavigationLink>
|
|
||||||
)
|
|
||||||
)}
|
|
||||||
</ul>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
{/* New Version Available */}
|
|
||||||
{!isCollapsed && isOwnerOrManager && latestVersion && !isFormbricksCloud && !isDevelopment && (
|
|
||||||
<Link
|
|
||||||
href="https://github.com/formbricks/formbricks/releases"
|
|
||||||
target="_blank"
|
|
||||||
className="m-2 flex items-center space-x-4 rounded-lg border border-slate-200 bg-slate-100 p-2 text-sm text-slate-800 hover:border-slate-300 hover:bg-slate-200">
|
|
||||||
<p className="flex items-center justify-center gap-x-2 text-xs">
|
|
||||||
<RocketIcon strokeWidth={1.5} className="mx-1 h-6 w-6 text-slate-900" />
|
|
||||||
{t("common.new_version_available", { version: latestVersion })}
|
|
||||||
</p>
|
|
||||||
</Link>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* User Switch */}
|
|
||||||
<div className="flex items-center">
|
|
||||||
<DropdownMenu>
|
|
||||||
<DropdownMenuTrigger
|
|
||||||
asChild
|
|
||||||
id="userDropdownTrigger"
|
|
||||||
className="w-full rounded-br-xl border-t py-4 transition-colors duration-200 hover:bg-slate-50 focus:outline-none">
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
"flex cursor-pointer flex-row items-center gap-3",
|
|
||||||
isCollapsed ? "justify-center px-2" : "px-4"
|
|
||||||
)}>
|
|
||||||
<ProfileAvatar userId={user.id} />
|
|
||||||
{!isCollapsed && !isTextVisible && (
|
|
||||||
<>
|
|
||||||
<div
|
|
||||||
className={cn(isTextVisible ? "opacity-0" : "opacity-100", "grow overflow-hidden")}>
|
|
||||||
<p
|
|
||||||
title={user?.email}
|
|
||||||
className={cn(
|
|
||||||
"ph-no-capture ph-no-capture -mb-0.5 truncate text-sm font-bold text-slate-700"
|
|
||||||
)}>
|
|
||||||
{user?.name ? <span>{user?.name}</span> : <span>{user?.email}</span>}
|
|
||||||
</p>
|
|
||||||
<p className="text-sm text-slate-700">{t("common.account")}</p>
|
|
||||||
</div>
|
|
||||||
<ChevronRightIcon
|
|
||||||
className={cn("h-5 w-5 shrink-0 text-slate-700 hover:text-slate-500")}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</DropdownMenuTrigger>
|
|
||||||
|
|
||||||
<DropdownMenuContent
|
|
||||||
id="userDropdownInnerContentWrapper"
|
|
||||||
side="right"
|
|
||||||
sideOffset={10}
|
|
||||||
alignOffset={5}
|
|
||||||
align="end">
|
|
||||||
{/* Dropdown Items */}
|
|
||||||
|
|
||||||
{dropdownNavigation.map((link) => (
|
|
||||||
<Link
|
|
||||||
href={link.href}
|
|
||||||
target={link.target}
|
|
||||||
className="flex w-full items-center"
|
|
||||||
key={link.label}
|
|
||||||
rel={link.target === "_blank" ? "noopener noreferrer" : undefined}>
|
|
||||||
<DropdownMenuItem>
|
|
||||||
<link.icon className="mr-2 h-4 w-4" strokeWidth={1.5} />
|
|
||||||
{link.label}
|
|
||||||
</DropdownMenuItem>
|
|
||||||
</Link>
|
|
||||||
))}
|
|
||||||
{/* Logout */}
|
|
||||||
<DropdownMenuItem
|
|
||||||
onClick={async () => {
|
|
||||||
const loginUrl = `${publicDomain}/auth/login`;
|
|
||||||
const route = await signOutWithAudit({
|
|
||||||
reason: "user_initiated",
|
|
||||||
redirectUrl: loginUrl,
|
|
||||||
organizationId: organization.id,
|
|
||||||
redirect: false,
|
|
||||||
callbackUrl: loginUrl,
|
|
||||||
clearEnvironmentId: true,
|
|
||||||
});
|
|
||||||
router.push(route?.url || loginUrl); // NOSONAR // We want to check for empty strings
|
|
||||||
}}
|
|
||||||
icon={<LogOutIcon className="mr-2 h-4 w-4" strokeWidth={1.5} />}>
|
|
||||||
{t("common.logout")}
|
|
||||||
</DropdownMenuItem>
|
|
||||||
</DropdownMenuContent>
|
|
||||||
</DropdownMenu>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</aside>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
import Link from "next/link";
|
|
||||||
import React from "react";
|
|
||||||
import { cn } from "@/lib/cn";
|
|
||||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/modules/ui/components/tooltip";
|
|
||||||
|
|
||||||
interface NavigationLinkProps {
|
|
||||||
href: string;
|
|
||||||
isActive: boolean;
|
|
||||||
isCollapsed: boolean;
|
|
||||||
children: React.ReactNode;
|
|
||||||
linkText: string;
|
|
||||||
isTextVisible: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const NavigationLink = ({
|
|
||||||
href,
|
|
||||||
isActive,
|
|
||||||
isCollapsed = false,
|
|
||||||
children,
|
|
||||||
linkText,
|
|
||||||
isTextVisible = true,
|
|
||||||
}: NavigationLinkProps) => {
|
|
||||||
const activeClass = "bg-slate-50 border-r-4 border-brand-dark font-semibold text-slate-900";
|
|
||||||
const inactiveClass =
|
|
||||||
"hover:bg-slate-50 border-r-4 border-transparent hover:border-slate-300 transition-all duration-150 ease-in-out";
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{isCollapsed ? (
|
|
||||||
<TooltipProvider delayDuration={0}>
|
|
||||||
<Tooltip>
|
|
||||||
<TooltipTrigger asChild>
|
|
||||||
<li
|
|
||||||
className={cn(
|
|
||||||
"mb-1 ml-2 rounded-l-md py-2 pl-2 text-sm text-slate-700 hover:text-slate-900",
|
|
||||||
isActive ? activeClass : inactiveClass
|
|
||||||
)}>
|
|
||||||
<Link href={href} className="flex items-center">
|
|
||||||
{children}
|
|
||||||
</Link>
|
|
||||||
</li>
|
|
||||||
</TooltipTrigger>
|
|
||||||
<TooltipContent side="right">{linkText}</TooltipContent>
|
|
||||||
</Tooltip>
|
|
||||||
</TooltipProvider>
|
|
||||||
) : (
|
|
||||||
<li
|
|
||||||
className={cn(
|
|
||||||
"mb-1 rounded-l-md py-2 pl-5 text-sm text-slate-600 hover:text-slate-900",
|
|
||||||
isActive ? activeClass : inactiveClass
|
|
||||||
)}>
|
|
||||||
<Link href={href} className="flex items-center">
|
|
||||||
{children}
|
|
||||||
<span
|
|
||||||
className={cn(
|
|
||||||
"ml-2 flex transition-opacity duration-100",
|
|
||||||
isTextVisible ? "opacity-0" : "opacity-100"
|
|
||||||
)}>
|
|
||||||
{linkText}
|
|
||||||
</span>
|
|
||||||
</Link>
|
|
||||||
</li>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { ChevronDownIcon, CircleHelpIcon, Code2Icon, Loader2 } from "lucide-react";
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { useState } from "react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { BreadcrumbItem } from "@/modules/ui/components/breadcrumb";
|
|
||||||
import {
|
|
||||||
DropdownMenu,
|
|
||||||
DropdownMenuCheckboxItem,
|
|
||||||
DropdownMenuContent,
|
|
||||||
DropdownMenuGroup,
|
|
||||||
DropdownMenuTrigger,
|
|
||||||
} from "@/modules/ui/components/dropdown-menu";
|
|
||||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/modules/ui/components/tooltip";
|
|
||||||
|
|
||||||
export const EnvironmentBreadcrumb = ({
|
|
||||||
environments,
|
|
||||||
currentEnvironment,
|
|
||||||
}: {
|
|
||||||
environments: { id: string; type: string }[];
|
|
||||||
currentEnvironment: { id: string; type: string };
|
|
||||||
}) => {
|
|
||||||
const { t } = useTranslation();
|
|
||||||
const [isEnvironmentDropdownOpen, setIsEnvironmentDropdownOpen] = useState(false);
|
|
||||||
const router = useRouter();
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
|
|
||||||
const handleEnvironmentChange = (environmentId: string) => {
|
|
||||||
if (environmentId === currentEnvironment.id) return;
|
|
||||||
setIsLoading(true);
|
|
||||||
router.push(`/environments/${environmentId}/`);
|
|
||||||
};
|
|
||||||
|
|
||||||
const developmentTooltip = () => {
|
|
||||||
return (
|
|
||||||
<TooltipProvider>
|
|
||||||
<Tooltip delayDuration={0}>
|
|
||||||
<TooltipTrigger asChild>
|
|
||||||
<CircleHelpIcon className="h-3 w-3" />
|
|
||||||
</TooltipTrigger>
|
|
||||||
<TooltipContent className="mt-2 border-none bg-red-800 text-white">
|
|
||||||
{t("common.development_environment_banner")}
|
|
||||||
</TooltipContent>
|
|
||||||
</Tooltip>
|
|
||||||
</TooltipProvider>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<BreadcrumbItem
|
|
||||||
isActive={isEnvironmentDropdownOpen}
|
|
||||||
isHighlighted={currentEnvironment.type === "development"}>
|
|
||||||
<DropdownMenu onOpenChange={setIsEnvironmentDropdownOpen}>
|
|
||||||
<DropdownMenuTrigger
|
|
||||||
className="flex cursor-pointer items-center gap-1 outline-none"
|
|
||||||
id="environmentDropdownTrigger"
|
|
||||||
asChild>
|
|
||||||
<div className="flex items-center gap-1">
|
|
||||||
<Code2Icon className="h-3 w-3" strokeWidth={1.5} />
|
|
||||||
<span className="capitalize">{currentEnvironment.type}</span>
|
|
||||||
{isLoading && <Loader2 className="h-3 w-3 animate-spin" strokeWidth={1.5} />}
|
|
||||||
{currentEnvironment.type === "development" && developmentTooltip()}
|
|
||||||
{isEnvironmentDropdownOpen && <ChevronDownIcon className="h-3 w-3" strokeWidth={1.5} />}
|
|
||||||
</div>
|
|
||||||
</DropdownMenuTrigger>
|
|
||||||
<DropdownMenuContent className="mt-2" align="start">
|
|
||||||
<div className="px-2 py-1.5 text-sm font-medium text-slate-500">
|
|
||||||
<Code2Icon className="mr-2 inline h-4 w-4" />
|
|
||||||
{t("common.choose_environment")}
|
|
||||||
</div>
|
|
||||||
<DropdownMenuGroup>
|
|
||||||
{environments.map((env) => (
|
|
||||||
<DropdownMenuCheckboxItem
|
|
||||||
key={env.id}
|
|
||||||
checked={env.type === currentEnvironment.type}
|
|
||||||
onClick={() => handleEnvironmentChange(env.id)}
|
|
||||||
className="cursor-pointer">
|
|
||||||
<div className="flex items-center gap-2 capitalize">
|
|
||||||
<span>{env.type}</span>
|
|
||||||
</div>
|
|
||||||
</DropdownMenuCheckboxItem>
|
|
||||||
))}
|
|
||||||
</DropdownMenuGroup>
|
|
||||||
</DropdownMenuContent>
|
|
||||||
</DropdownMenu>
|
|
||||||
</BreadcrumbItem>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { EnvironmentBreadcrumb } from "@/app/(app)/environments/[environmentId]/components/environment-breadcrumb";
|
|
||||||
import { OrganizationBreadcrumb } from "@/app/(app)/environments/[environmentId]/components/organization-breadcrumb";
|
|
||||||
import { ProjectBreadcrumb } from "@/app/(app)/environments/[environmentId]/components/project-breadcrumb";
|
|
||||||
import { Breadcrumb, BreadcrumbList } from "@/modules/ui/components/breadcrumb";
|
|
||||||
|
|
||||||
interface ProjectAndOrgSwitchProps {
|
|
||||||
currentOrganizationId: string;
|
|
||||||
currentOrganizationName?: string; // Optional: for pages without context
|
|
||||||
currentProjectId?: string;
|
|
||||||
currentProjectName?: string; // Optional: for pages without context
|
|
||||||
currentEnvironmentId?: string;
|
|
||||||
environments: { id: string; type: string }[];
|
|
||||||
isMultiOrgEnabled: boolean;
|
|
||||||
organizationProjectsLimit: number;
|
|
||||||
isFormbricksCloud: boolean;
|
|
||||||
isLicenseActive: boolean;
|
|
||||||
isOwnerOrManager: boolean;
|
|
||||||
isMember: boolean;
|
|
||||||
isAccessControlAllowed: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ProjectAndOrgSwitch = ({
|
|
||||||
currentOrganizationId,
|
|
||||||
currentOrganizationName,
|
|
||||||
currentProjectId,
|
|
||||||
currentProjectName,
|
|
||||||
currentEnvironmentId,
|
|
||||||
environments,
|
|
||||||
isMultiOrgEnabled,
|
|
||||||
organizationProjectsLimit,
|
|
||||||
isFormbricksCloud,
|
|
||||||
isLicenseActive,
|
|
||||||
isOwnerOrManager,
|
|
||||||
isAccessControlAllowed,
|
|
||||||
isMember,
|
|
||||||
}: ProjectAndOrgSwitchProps) => {
|
|
||||||
const currentEnvironment = environments.find((env) => env.id === currentEnvironmentId);
|
|
||||||
const showEnvironmentBreadcrumb = currentEnvironment?.type === "development";
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Breadcrumb>
|
|
||||||
<BreadcrumbList className="gap-0">
|
|
||||||
<OrganizationBreadcrumb
|
|
||||||
currentOrganizationId={currentOrganizationId}
|
|
||||||
currentOrganizationName={currentOrganizationName}
|
|
||||||
currentEnvironmentId={currentEnvironmentId}
|
|
||||||
isMultiOrgEnabled={isMultiOrgEnabled}
|
|
||||||
isFormbricksCloud={isFormbricksCloud}
|
|
||||||
isMember={isMember}
|
|
||||||
isOwnerOrManager={isOwnerOrManager}
|
|
||||||
/>
|
|
||||||
{currentProjectId && currentEnvironmentId && (
|
|
||||||
<ProjectBreadcrumb
|
|
||||||
currentProjectId={currentProjectId}
|
|
||||||
currentProjectName={currentProjectName}
|
|
||||||
currentOrganizationId={currentOrganizationId}
|
|
||||||
currentEnvironmentId={currentEnvironmentId}
|
|
||||||
isOwnerOrManager={isOwnerOrManager}
|
|
||||||
organizationProjectsLimit={organizationProjectsLimit}
|
|
||||||
isFormbricksCloud={isFormbricksCloud}
|
|
||||||
isLicenseActive={isLicenseActive}
|
|
||||||
isAccessControlAllowed={isAccessControlAllowed}
|
|
||||||
isEnvironmentBreadcrumbVisible={showEnvironmentBreadcrumb}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{showEnvironmentBreadcrumb && (
|
|
||||||
<EnvironmentBreadcrumb environments={environments} currentEnvironment={currentEnvironment} />
|
|
||||||
)}
|
|
||||||
</BreadcrumbList>
|
|
||||||
</Breadcrumb>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,297 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import * as Sentry from "@sentry/nextjs";
|
|
||||||
import { ChevronDownIcon, ChevronRightIcon, CogIcon, HotelIcon, Loader2, PlusIcon } from "lucide-react";
|
|
||||||
import { usePathname, useRouter } from "next/navigation";
|
|
||||||
import { useEffect, useState, useTransition } from "react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { logger } from "@formbricks/logger";
|
|
||||||
import { getProjectsForSwitcherAction } from "@/app/(app)/environments/[environmentId]/actions";
|
|
||||||
import { getFormattedErrorMessage } from "@/lib/utils/helper";
|
|
||||||
import { CreateProjectModal } from "@/modules/projects/components/create-project-modal";
|
|
||||||
import { ProjectLimitModal } from "@/modules/projects/components/project-limit-modal";
|
|
||||||
import { BreadcrumbItem } from "@/modules/ui/components/breadcrumb";
|
|
||||||
import {
|
|
||||||
DropdownMenu,
|
|
||||||
DropdownMenuCheckboxItem,
|
|
||||||
DropdownMenuContent,
|
|
||||||
DropdownMenuGroup,
|
|
||||||
DropdownMenuSeparator,
|
|
||||||
DropdownMenuTrigger,
|
|
||||||
} from "@/modules/ui/components/dropdown-menu";
|
|
||||||
import { ModalButton } from "@/modules/ui/components/upgrade-prompt";
|
|
||||||
import { useProject } from "../context/environment-context";
|
|
||||||
|
|
||||||
interface ProjectBreadcrumbProps {
|
|
||||||
currentProjectId: string;
|
|
||||||
currentProjectName?: string; // Optional: pass directly if context not available
|
|
||||||
isOwnerOrManager: boolean;
|
|
||||||
organizationProjectsLimit: number;
|
|
||||||
isFormbricksCloud: boolean;
|
|
||||||
isLicenseActive: boolean;
|
|
||||||
currentOrganizationId: string;
|
|
||||||
currentEnvironmentId: string;
|
|
||||||
isAccessControlAllowed: boolean;
|
|
||||||
isEnvironmentBreadcrumbVisible: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isActiveProjectSetting = (pathname: string, settingId: string): boolean => {
|
|
||||||
// Match /workspace/{settingId} or /workspace/{settingId}/... but exclude settings paths
|
|
||||||
if (pathname.includes("/settings/")) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Check if path matches /workspace/{settingId} (with optional trailing path)
|
|
||||||
const pattern = new RegExp(`/workspace/${settingId}(?:/|$)`);
|
|
||||||
return pattern.test(pathname);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const ProjectBreadcrumb = ({
|
|
||||||
currentProjectId,
|
|
||||||
currentProjectName,
|
|
||||||
isOwnerOrManager,
|
|
||||||
organizationProjectsLimit,
|
|
||||||
isFormbricksCloud,
|
|
||||||
isLicenseActive,
|
|
||||||
currentOrganizationId,
|
|
||||||
currentEnvironmentId,
|
|
||||||
isAccessControlAllowed,
|
|
||||||
isEnvironmentBreadcrumbVisible,
|
|
||||||
}: ProjectBreadcrumbProps) => {
|
|
||||||
const { t } = useTranslation();
|
|
||||||
const [isProjectDropdownOpen, setIsProjectDropdownOpen] = useState(false);
|
|
||||||
const [openCreateProjectModal, setOpenCreateProjectModal] = useState(false);
|
|
||||||
const [openLimitModal, setOpenLimitModal] = useState(false);
|
|
||||||
const router = useRouter();
|
|
||||||
const [isLoadingProjects, setIsLoadingProjects] = useState(false);
|
|
||||||
const [projects, setProjects] = useState<{ id: string; name: string }[]>([]);
|
|
||||||
const [loadError, setLoadError] = useState<string | null>(null);
|
|
||||||
const [isPending, startTransition] = useTransition();
|
|
||||||
const pathname = usePathname();
|
|
||||||
|
|
||||||
// Get current project name from context OR prop
|
|
||||||
// Context is preferred, but prop is fallback for pages without EnvironmentContextWrapper
|
|
||||||
const { project: currentProject } = useProject();
|
|
||||||
const projectName = currentProject?.name || currentProjectName || "";
|
|
||||||
|
|
||||||
// Lazy-load projects when dropdown opens
|
|
||||||
useEffect(() => {
|
|
||||||
// Only fetch when dropdown opened for first time (and no error state)
|
|
||||||
if (isProjectDropdownOpen && projects.length === 0 && !isLoadingProjects && !loadError) {
|
|
||||||
setIsLoadingProjects(true);
|
|
||||||
setLoadError(null); // Clear any previous errors
|
|
||||||
getProjectsForSwitcherAction({ organizationId: currentOrganizationId }).then((result) => {
|
|
||||||
if (result?.data) {
|
|
||||||
// Sort projects by name
|
|
||||||
const sorted = [...result.data].sort((a, b) => a.name.localeCompare(b.name));
|
|
||||||
setProjects(sorted);
|
|
||||||
} else {
|
|
||||||
// Handle server errors or validation errors
|
|
||||||
const errorMessage = getFormattedErrorMessage(result);
|
|
||||||
const error = new Error(errorMessage);
|
|
||||||
logger.error(error, "Failed to load projects");
|
|
||||||
Sentry.captureException(error);
|
|
||||||
setLoadError(errorMessage || t("common.failed_to_load_workspaces"));
|
|
||||||
}
|
|
||||||
setIsLoadingProjects(false);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [isProjectDropdownOpen, currentOrganizationId, projects.length, isLoadingProjects, loadError, t]);
|
|
||||||
|
|
||||||
const projectSettings = [
|
|
||||||
{
|
|
||||||
id: "general",
|
|
||||||
label: t("common.general"),
|
|
||||||
href: `/environments/${currentEnvironmentId}/workspace/general`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "look",
|
|
||||||
label: t("common.look_and_feel"),
|
|
||||||
href: `/environments/${currentEnvironmentId}/workspace/look`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "app-connection",
|
|
||||||
label: t("common.website_and_app_connection"),
|
|
||||||
href: `/environments/${currentEnvironmentId}/workspace/app-connection`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "integrations",
|
|
||||||
label: t("common.integrations"),
|
|
||||||
href: `/environments/${currentEnvironmentId}/workspace/integrations`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "teams",
|
|
||||||
label: t("common.team_access"),
|
|
||||||
href: `/environments/${currentEnvironmentId}/workspace/teams`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "languages",
|
|
||||||
label: t("common.survey_languages"),
|
|
||||||
href: `/environments/${currentEnvironmentId}/workspace/languages`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "tags",
|
|
||||||
label: t("common.tags"),
|
|
||||||
href: `/environments/${currentEnvironmentId}/workspace/tags`,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
if (!currentProject) {
|
|
||||||
const errorMessage = `Workspace not found for workspace id: ${currentProjectId}`;
|
|
||||||
logger.error(errorMessage);
|
|
||||||
Sentry.captureException(new Error(errorMessage));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleProjectChange = (projectId: string) => {
|
|
||||||
if (projectId === currentProjectId) return;
|
|
||||||
startTransition(() => {
|
|
||||||
router.push(`/workspaces/${projectId}/`);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleAddProject = () => {
|
|
||||||
if (projects.length >= organizationProjectsLimit) {
|
|
||||||
setOpenLimitModal(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setOpenCreateProjectModal(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleProjectSettingsNavigation = (settingId: string) => {
|
|
||||||
startTransition(() => {
|
|
||||||
router.push(`/environments/${currentEnvironmentId}/workspace/${settingId}`);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const LimitModalButtons = (): [ModalButton, ModalButton] => {
|
|
||||||
if (isFormbricksCloud) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
text: t("environments.settings.billing.upgrade"),
|
|
||||||
href: `/environments/${currentEnvironmentId}/settings/billing`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: t("common.cancel"),
|
|
||||||
onClick: () => setOpenLimitModal(false),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
text: t("environments.settings.billing.upgrade"),
|
|
||||||
href: isLicenseActive
|
|
||||||
? `/environments/${currentEnvironmentId}/settings/enterprise`
|
|
||||||
: "https://formbricks.com/upgrade-self-hosted-license",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: t("common.cancel"),
|
|
||||||
onClick: () => setOpenLimitModal(false),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
};
|
|
||||||
return (
|
|
||||||
<BreadcrumbItem isActive={isProjectDropdownOpen}>
|
|
||||||
<DropdownMenu onOpenChange={setIsProjectDropdownOpen}>
|
|
||||||
<DropdownMenuTrigger
|
|
||||||
className="flex cursor-pointer items-center gap-1 outline-none"
|
|
||||||
id="projectDropdownTrigger"
|
|
||||||
asChild>
|
|
||||||
<div className="flex items-center gap-1">
|
|
||||||
<HotelIcon className="h-3 w-3" strokeWidth={1.5} />
|
|
||||||
<span>{projectName}</span>
|
|
||||||
{isPending && <Loader2 className="h-3 w-3 animate-spin" strokeWidth={1.5} />}
|
|
||||||
{isEnvironmentBreadcrumbVisible && !isProjectDropdownOpen ? (
|
|
||||||
<ChevronRightIcon className="h-3 w-3" strokeWidth={1.5} />
|
|
||||||
) : (
|
|
||||||
<ChevronDownIcon className="h-3 w-3" strokeWidth={1.5} />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</DropdownMenuTrigger>
|
|
||||||
|
|
||||||
<DropdownMenuContent align="start" className="mt-2">
|
|
||||||
<div className="px-2 py-1.5 text-sm font-medium text-slate-500">
|
|
||||||
<HotelIcon className="mr-2 inline h-4 w-4" strokeWidth={1.5} />
|
|
||||||
{t("common.choose_workspace")}
|
|
||||||
</div>
|
|
||||||
{isLoadingProjects && (
|
|
||||||
<div className="flex items-center justify-center py-2">
|
|
||||||
<Loader2 className="h-4 w-4 animate-spin" />
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{!isLoadingProjects && loadError && (
|
|
||||||
<div className="px-2 py-4">
|
|
||||||
<p className="mb-2 text-sm text-red-600">{loadError}</p>
|
|
||||||
<button
|
|
||||||
onClick={() => {
|
|
||||||
setLoadError(null);
|
|
||||||
setProjects([]);
|
|
||||||
}}
|
|
||||||
className="text-xs text-slate-600 underline hover:text-slate-800">
|
|
||||||
{t("common.try_again")}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{!isLoadingProjects && !loadError && (
|
|
||||||
<>
|
|
||||||
<DropdownMenuGroup className="max-h-[300px] overflow-y-auto">
|
|
||||||
{projects.map((proj) => (
|
|
||||||
<DropdownMenuCheckboxItem
|
|
||||||
key={proj.id}
|
|
||||||
checked={proj.id === currentProjectId}
|
|
||||||
onClick={() => handleProjectChange(proj.id)}
|
|
||||||
className="cursor-pointer">
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span>{proj.name}</span>
|
|
||||||
</div>
|
|
||||||
</DropdownMenuCheckboxItem>
|
|
||||||
))}
|
|
||||||
</DropdownMenuGroup>
|
|
||||||
{isOwnerOrManager && (
|
|
||||||
<DropdownMenuCheckboxItem
|
|
||||||
onClick={handleAddProject}
|
|
||||||
className="w-full cursor-pointer justify-between">
|
|
||||||
<span>{t("common.add_new_workspace")}</span>
|
|
||||||
<PlusIcon className="ml-2 h-4 w-4" strokeWidth={1.5} />
|
|
||||||
</DropdownMenuCheckboxItem>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
<DropdownMenuGroup>
|
|
||||||
<DropdownMenuSeparator />
|
|
||||||
<div className="px-2 py-1.5 text-sm font-medium text-slate-500">
|
|
||||||
<CogIcon className="mr-2 inline h-4 w-4" strokeWidth={1.5} />
|
|
||||||
{t("common.workspace_configuration")}
|
|
||||||
</div>
|
|
||||||
{projectSettings.map((setting) => (
|
|
||||||
<DropdownMenuCheckboxItem
|
|
||||||
key={setting.id}
|
|
||||||
checked={isActiveProjectSetting(pathname, setting.id)}
|
|
||||||
onClick={() => handleProjectSettingsNavigation(setting.id)}
|
|
||||||
className="cursor-pointer">
|
|
||||||
{setting.label}
|
|
||||||
</DropdownMenuCheckboxItem>
|
|
||||||
))}
|
|
||||||
</DropdownMenuGroup>
|
|
||||||
</DropdownMenuContent>
|
|
||||||
</DropdownMenu>
|
|
||||||
{/* Modals */}
|
|
||||||
{openLimitModal && (
|
|
||||||
<ProjectLimitModal
|
|
||||||
open={openLimitModal}
|
|
||||||
setOpen={setOpenLimitModal}
|
|
||||||
buttons={LimitModalButtons()}
|
|
||||||
projectLimit={organizationProjectsLimit}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{openCreateProjectModal && (
|
|
||||||
<CreateProjectModal
|
|
||||||
open={openCreateProjectModal}
|
|
||||||
setOpen={setOpenCreateProjectModal}
|
|
||||||
organizationId={currentOrganizationId}
|
|
||||||
isAccessControlAllowed={isAccessControlAllowed}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</BreadcrumbItem>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { createContext, useContext, useMemo } from "react";
|
|
||||||
import { TEnvironment } from "@formbricks/types/environment";
|
|
||||||
import { TOrganization } from "@formbricks/types/organizations";
|
|
||||||
import { TProject } from "@formbricks/types/project";
|
|
||||||
|
|
||||||
export interface EnvironmentContextType {
|
|
||||||
environment: TEnvironment;
|
|
||||||
project: TProject;
|
|
||||||
organization: TOrganization;
|
|
||||||
organizationId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const EnvironmentContext = createContext<EnvironmentContextType | null>(null);
|
|
||||||
|
|
||||||
export const useEnvironment = () => {
|
|
||||||
const context = useContext(EnvironmentContext);
|
|
||||||
if (!context) {
|
|
||||||
throw new Error("useEnvironment must be used within an EnvironmentProvider");
|
|
||||||
}
|
|
||||||
return context;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useProject = () => {
|
|
||||||
const context = useContext(EnvironmentContext);
|
|
||||||
if (!context) {
|
|
||||||
return { project: null };
|
|
||||||
}
|
|
||||||
return { project: context.project };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useOrganization = () => {
|
|
||||||
const context = useContext(EnvironmentContext);
|
|
||||||
if (!context) {
|
|
||||||
return { organization: null };
|
|
||||||
}
|
|
||||||
return { organization: context.organization };
|
|
||||||
};
|
|
||||||
|
|
||||||
// Client wrapper component to be used in server components
|
|
||||||
interface EnvironmentContextWrapperProps {
|
|
||||||
environment: TEnvironment;
|
|
||||||
project: TProject;
|
|
||||||
organization: TOrganization;
|
|
||||||
children: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const EnvironmentContextWrapper = ({
|
|
||||||
environment,
|
|
||||||
project,
|
|
||||||
organization,
|
|
||||||
children,
|
|
||||||
}: EnvironmentContextWrapperProps) => {
|
|
||||||
const environmentContextValue = useMemo(
|
|
||||||
() => ({
|
|
||||||
environment,
|
|
||||||
project,
|
|
||||||
organization,
|
|
||||||
organizationId: project.organizationId,
|
|
||||||
}),
|
|
||||||
[environment, project, organization]
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<EnvironmentContext.Provider value={environmentContextValue}>{children}</EnvironmentContext.Provider>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
import { getServerSession } from "next-auth";
|
|
||||||
import { redirect } from "next/navigation";
|
|
||||||
import { EnvironmentLayout } from "@/app/(app)/environments/[environmentId]/components/EnvironmentLayout";
|
|
||||||
import { EnvironmentContextWrapper } from "@/app/(app)/environments/[environmentId]/context/environment-context";
|
|
||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
|
||||||
import { getEnvironmentLayoutData } from "@/modules/environments/lib/utils";
|
|
||||||
import EnvironmentStorageHandler from "./components/EnvironmentStorageHandler";
|
|
||||||
|
|
||||||
const EnvLayout = async (props: {
|
|
||||||
params: Promise<{ environmentId: string }>;
|
|
||||||
children: React.ReactNode;
|
|
||||||
}) => {
|
|
||||||
const params = await props.params;
|
|
||||||
const { children } = props;
|
|
||||||
|
|
||||||
// Check session first (required for userId)
|
|
||||||
const session = await getServerSession(authOptions);
|
|
||||||
if (!session?.user) {
|
|
||||||
return redirect(`/auth/login`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Single consolidated data fetch (replaces ~12 individual fetches)
|
|
||||||
const layoutData = await getEnvironmentLayoutData(params.environmentId, session.user.id);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<EnvironmentStorageHandler environmentId={params.environmentId} />
|
|
||||||
<EnvironmentContextWrapper
|
|
||||||
environment={layoutData.environment}
|
|
||||||
project={layoutData.project}
|
|
||||||
organization={layoutData.organization}>
|
|
||||||
<EnvironmentLayout layoutData={layoutData}>{children}</EnvironmentLayout>
|
|
||||||
</EnvironmentContextWrapper>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default EnvLayout;
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
import { redirect } from "next/navigation";
|
|
||||||
import { IS_FORMBRICKS_CLOUD } from "@/lib/constants";
|
|
||||||
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
|
||||||
import { getAccessFlags } from "@/lib/membership/utils";
|
|
||||||
import { getEnvironmentAuth } from "@/modules/environments/lib/utils";
|
|
||||||
|
|
||||||
const EnvironmentPage = async (props) => {
|
|
||||||
const params = await props.params;
|
|
||||||
const { session, organization } = await getEnvironmentAuth(params.environmentId);
|
|
||||||
|
|
||||||
const currentUserMembership = await getMembershipByUserIdOrganizationId(session?.user.id, organization.id);
|
|
||||||
const { isBilling } = getAccessFlags(currentUserMembership?.role);
|
|
||||||
|
|
||||||
if (isBilling) {
|
|
||||||
if (IS_FORMBRICKS_CLOUD) {
|
|
||||||
return redirect(`/environments/${params.environmentId}/settings/billing`);
|
|
||||||
} else {
|
|
||||||
return redirect(`/environments/${params.environmentId}/settings/enterprise`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirect(`/environments/${params.environmentId}/surveys`);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default EnvironmentPage;
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import { getServerSession } from "next-auth";
|
|
||||||
import { getOrganizationByEnvironmentId } from "@/lib/organization/service";
|
|
||||||
import { getProjectByEnvironmentId } from "@/lib/project/service";
|
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
|
||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
|
||||||
|
|
||||||
const AccountSettingsLayout = async (props) => {
|
|
||||||
const params = await props.params;
|
|
||||||
|
|
||||||
const { children } = props;
|
|
||||||
|
|
||||||
const t = await getTranslate();
|
|
||||||
const [organization, project, session] = await Promise.all([
|
|
||||||
getOrganizationByEnvironmentId(params.environmentId),
|
|
||||||
getProjectByEnvironmentId(params.environmentId),
|
|
||||||
getServerSession(authOptions),
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (!organization) {
|
|
||||||
throw new Error(t("common.organization_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!project) {
|
|
||||||
throw new Error(t("common.workspace_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!session) {
|
|
||||||
throw new Error(t("common.session_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return <>{children}</>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default AccountSettingsLayout;
|
|
||||||
-37
@@ -1,37 +0,0 @@
|
|||||||
"use server";
|
|
||||||
|
|
||||||
import { z } from "zod";
|
|
||||||
import { ZUserNotificationSettings } from "@formbricks/types/user";
|
|
||||||
import { getUser, updateUser } from "@/lib/user/service";
|
|
||||||
import { authenticatedActionClient } from "@/lib/utils/action-client";
|
|
||||||
import { AuthenticatedActionClientCtx } from "@/lib/utils/action-client/types/context";
|
|
||||||
import { withAuditLogging } from "@/modules/ee/audit-logs/lib/handler";
|
|
||||||
|
|
||||||
const ZUpdateNotificationSettingsAction = z.object({
|
|
||||||
notificationSettings: ZUserNotificationSettings,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const updateNotificationSettingsAction = authenticatedActionClient
|
|
||||||
.inputSchema(ZUpdateNotificationSettingsAction)
|
|
||||||
.action(
|
|
||||||
withAuditLogging(
|
|
||||||
"updated",
|
|
||||||
"user",
|
|
||||||
async ({
|
|
||||||
ctx,
|
|
||||||
parsedInput,
|
|
||||||
}: {
|
|
||||||
ctx: AuthenticatedActionClientCtx;
|
|
||||||
parsedInput: Record<string, any>;
|
|
||||||
}) => {
|
|
||||||
const oldObject = await getUser(ctx.user.id);
|
|
||||||
const result = await updateUser(ctx.user.id, {
|
|
||||||
notificationSettings: parsedInput.notificationSettings,
|
|
||||||
});
|
|
||||||
ctx.auditLoggingCtx.userId = ctx.user.id;
|
|
||||||
ctx.auditLoggingCtx.oldObject = oldObject;
|
|
||||||
ctx.auditLoggingCtx.newObject = result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
-90
@@ -1,90 +0,0 @@
|
|||||||
"use server";
|
|
||||||
|
|
||||||
import { z } from "zod";
|
|
||||||
import { ZId } from "@formbricks/types/common";
|
|
||||||
import { OperationNotAllowedError } from "@formbricks/types/errors";
|
|
||||||
import { ZOrganizationUpdateInput } from "@formbricks/types/organizations";
|
|
||||||
import { deleteOrganization, getOrganization, updateOrganization } from "@/lib/organization/service";
|
|
||||||
import { authenticatedActionClient } from "@/lib/utils/action-client";
|
|
||||||
import { checkAuthorizationUpdated } from "@/lib/utils/action-client/action-client-middleware";
|
|
||||||
import { AuthenticatedActionClientCtx } from "@/lib/utils/action-client/types/context";
|
|
||||||
import { withAuditLogging } from "@/modules/ee/audit-logs/lib/handler";
|
|
||||||
import { getIsMultiOrgEnabled } from "@/modules/ee/license-check/lib/utils";
|
|
||||||
|
|
||||||
const ZUpdateOrganizationNameAction = z.object({
|
|
||||||
organizationId: ZId,
|
|
||||||
data: ZOrganizationUpdateInput.pick({ name: true }),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const updateOrganizationNameAction = authenticatedActionClient
|
|
||||||
.inputSchema(ZUpdateOrganizationNameAction)
|
|
||||||
.action(
|
|
||||||
withAuditLogging(
|
|
||||||
"updated",
|
|
||||||
"organization",
|
|
||||||
async ({
|
|
||||||
ctx,
|
|
||||||
parsedInput,
|
|
||||||
}: {
|
|
||||||
ctx: AuthenticatedActionClientCtx;
|
|
||||||
parsedInput: Record<string, any>;
|
|
||||||
}) => {
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user.id,
|
|
||||||
organizationId: parsedInput.organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
schema: ZOrganizationUpdateInput.pick({ name: true }),
|
|
||||||
data: parsedInput.data,
|
|
||||||
roles: ["owner"],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
ctx.auditLoggingCtx.organizationId = parsedInput.organizationId;
|
|
||||||
const oldObject = await getOrganization(parsedInput.organizationId);
|
|
||||||
const result = await updateOrganization(parsedInput.organizationId, parsedInput.data);
|
|
||||||
ctx.auditLoggingCtx.oldObject = oldObject;
|
|
||||||
ctx.auditLoggingCtx.newObject = result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const ZDeleteOrganizationAction = z.object({
|
|
||||||
organizationId: ZId,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const deleteOrganizationAction = authenticatedActionClient
|
|
||||||
.inputSchema(ZDeleteOrganizationAction)
|
|
||||||
.action(
|
|
||||||
withAuditLogging(
|
|
||||||
"deleted",
|
|
||||||
"organization",
|
|
||||||
async ({
|
|
||||||
ctx,
|
|
||||||
parsedInput,
|
|
||||||
}: {
|
|
||||||
ctx: AuthenticatedActionClientCtx;
|
|
||||||
parsedInput: Record<string, any>;
|
|
||||||
}) => {
|
|
||||||
const isMultiOrgEnabled = await getIsMultiOrgEnabled();
|
|
||||||
if (!isMultiOrgEnabled) throw new OperationNotAllowedError("Organization deletion disabled");
|
|
||||||
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user.id,
|
|
||||||
organizationId: parsedInput.organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner"],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
ctx.auditLoggingCtx.organizationId = parsedInput.organizationId;
|
|
||||||
const oldObject = await getOrganization(parsedInput.organizationId);
|
|
||||||
ctx.auditLoggingCtx.oldObject = oldObject;
|
|
||||||
return await deleteOrganization(parsedInput.organizationId);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import { getServerSession } from "next-auth";
|
|
||||||
import { getOrganizationByEnvironmentId } from "@/lib/organization/service";
|
|
||||||
import { getProjectByEnvironmentId } from "@/lib/project/service";
|
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
|
||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
|
||||||
|
|
||||||
const Layout = async (props) => {
|
|
||||||
const params = await props.params;
|
|
||||||
|
|
||||||
const { children } = props;
|
|
||||||
|
|
||||||
const t = await getTranslate();
|
|
||||||
const [organization, project, session] = await Promise.all([
|
|
||||||
getOrganizationByEnvironmentId(params.environmentId),
|
|
||||||
getProjectByEnvironmentId(params.environmentId),
|
|
||||||
getServerSession(authOptions),
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (!organization) {
|
|
||||||
throw new Error(t("common.organization_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!project) {
|
|
||||||
throw new Error(t("common.workspace_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!session) {
|
|
||||||
throw new Error(t("common.session_not_found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return <>{children}</>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Layout;
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import { redirect } from "next/navigation";
|
|
||||||
|
|
||||||
const Page = async (props) => {
|
|
||||||
const params = await props.params;
|
|
||||||
return redirect(`/environments/${params.environmentId}/settings/profile`);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Page;
|
|
||||||
@@ -1,174 +0,0 @@
|
|||||||
"use server";
|
|
||||||
|
|
||||||
import { z } from "zod";
|
|
||||||
import { ZId } from "@formbricks/types/common";
|
|
||||||
import { OperationNotAllowedError, ResourceNotFoundError } from "@formbricks/types/errors";
|
|
||||||
import { ZResponseFilterCriteria } from "@formbricks/types/responses";
|
|
||||||
import { TSurvey, ZSurvey } from "@formbricks/types/surveys/types";
|
|
||||||
import { getOrganization } from "@/lib/organization/service";
|
|
||||||
import { getResponseDownloadFile, getResponseFilteringValues } from "@/lib/response/service";
|
|
||||||
import { getSurvey, updateSurvey } from "@/lib/survey/service";
|
|
||||||
import { getTagsByEnvironmentId } from "@/lib/tag/service";
|
|
||||||
import { authenticatedActionClient } from "@/lib/utils/action-client";
|
|
||||||
import { checkAuthorizationUpdated } from "@/lib/utils/action-client/action-client-middleware";
|
|
||||||
import { AuthenticatedActionClientCtx } from "@/lib/utils/action-client/types/context";
|
|
||||||
import { getOrganizationIdFromSurveyId, getProjectIdFromSurveyId } from "@/lib/utils/helper";
|
|
||||||
import { withAuditLogging } from "@/modules/ee/audit-logs/lib/handler";
|
|
||||||
import { getIsQuotasEnabled } from "@/modules/ee/license-check/lib/utils";
|
|
||||||
import { checkMultiLanguagePermission } from "@/modules/ee/multi-language-surveys/lib/actions";
|
|
||||||
import { getQuotas } from "@/modules/ee/quotas/lib/quotas";
|
|
||||||
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
|
|
||||||
import { checkSpamProtectionPermission } from "@/modules/survey/lib/permission";
|
|
||||||
import { getOrganizationBilling } from "@/modules/survey/lib/survey";
|
|
||||||
|
|
||||||
const ZGetResponsesDownloadUrlAction = z.object({
|
|
||||||
surveyId: ZId,
|
|
||||||
format: z.union([z.literal("csv"), z.literal("xlsx")]),
|
|
||||||
filterCriteria: ZResponseFilterCriteria,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const getResponsesDownloadUrlAction = authenticatedActionClient
|
|
||||||
.inputSchema(ZGetResponsesDownloadUrlAction)
|
|
||||||
.action(async ({ ctx, parsedInput }) => {
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user.id,
|
|
||||||
organizationId: await getOrganizationIdFromSurveyId(parsedInput.surveyId),
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner", "manager"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "projectTeam",
|
|
||||||
minPermission: "read",
|
|
||||||
projectId: await getProjectIdFromSurveyId(parsedInput.surveyId),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
return await getResponseDownloadFile(
|
|
||||||
parsedInput.surveyId,
|
|
||||||
parsedInput.format,
|
|
||||||
parsedInput.filterCriteria
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
const ZGetSurveyFilterDataAction = z.object({
|
|
||||||
surveyId: ZId,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const getSurveyFilterDataAction = authenticatedActionClient
|
|
||||||
.inputSchema(ZGetSurveyFilterDataAction)
|
|
||||||
.action(async ({ ctx, parsedInput }) => {
|
|
||||||
const survey = await getSurvey(parsedInput.surveyId);
|
|
||||||
|
|
||||||
if (!survey) {
|
|
||||||
throw new ResourceNotFoundError("Survey", parsedInput.surveyId);
|
|
||||||
}
|
|
||||||
|
|
||||||
const organizationId = await getOrganizationIdFromSurveyId(parsedInput.surveyId);
|
|
||||||
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user.id,
|
|
||||||
organizationId: organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner", "manager"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "projectTeam",
|
|
||||||
minPermission: "read",
|
|
||||||
projectId: await getProjectIdFromSurveyId(parsedInput.surveyId),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
const organizationBilling = await getOrganizationBilling(organizationId);
|
|
||||||
if (!organizationBilling) {
|
|
||||||
throw new ResourceNotFoundError("Organization", organizationId);
|
|
||||||
}
|
|
||||||
|
|
||||||
const isQuotasAllowed = await getIsQuotasEnabled(organizationBilling.plan);
|
|
||||||
|
|
||||||
const [tags, { contactAttributes: attributes, meta, hiddenFields }, quotas = []] = await Promise.all([
|
|
||||||
getTagsByEnvironmentId(survey.environmentId),
|
|
||||||
getResponseFilteringValues(parsedInput.surveyId),
|
|
||||||
isQuotasAllowed ? getQuotas(parsedInput.surveyId) : [],
|
|
||||||
]);
|
|
||||||
|
|
||||||
return { environmentTags: tags, attributes, meta, hiddenFields, quotas };
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if survey follow-ups are enabled for the given organization.
|
|
||||||
*
|
|
||||||
* @param {string} organizationId The ID of the organization to check.
|
|
||||||
* @returns {Promise<void>} A promise that resolves if the permission is granted.
|
|
||||||
* @throws {ResourceNotFoundError} If the organization is not found.
|
|
||||||
* @throws {OperationNotAllowedError} If survey follow-ups are not enabled for the organization.
|
|
||||||
*/
|
|
||||||
const checkSurveyFollowUpsPermission = async (organizationId: string): Promise<void> => {
|
|
||||||
const organization = await getOrganization(organizationId);
|
|
||||||
|
|
||||||
if (!organization) {
|
|
||||||
throw new ResourceNotFoundError("Organization not found", organizationId);
|
|
||||||
}
|
|
||||||
|
|
||||||
const isSurveyFollowUpsEnabled = await getSurveyFollowUpsPermission(organization.billing.plan);
|
|
||||||
if (!isSurveyFollowUpsEnabled) {
|
|
||||||
throw new OperationNotAllowedError("Survey follow ups are not enabled for this organization");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updateSurveyAction = authenticatedActionClient.inputSchema(ZSurvey).action(
|
|
||||||
withAuditLogging(
|
|
||||||
"updated",
|
|
||||||
"survey",
|
|
||||||
async ({ ctx, parsedInput }: { ctx: AuthenticatedActionClientCtx; parsedInput: TSurvey }) => {
|
|
||||||
const organizationId = await getOrganizationIdFromSurveyId(parsedInput.id);
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user?.id ?? "",
|
|
||||||
organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner", "manager"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "projectTeam",
|
|
||||||
projectId: await getProjectIdFromSurveyId(parsedInput.id),
|
|
||||||
minPermission: "readWrite",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
const { followUps } = parsedInput;
|
|
||||||
|
|
||||||
const oldSurvey = await getSurvey(parsedInput.id);
|
|
||||||
|
|
||||||
if (parsedInput.recaptcha?.enabled) {
|
|
||||||
await checkSpamProtectionPermission(organizationId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (followUps?.length) {
|
|
||||||
await checkSurveyFollowUpsPermission(organizationId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsedInput.languages?.length) {
|
|
||||||
await checkMultiLanguagePermission(organizationId);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Context for audit log
|
|
||||||
ctx.auditLoggingCtx.surveyId = parsedInput.id;
|
|
||||||
ctx.auditLoggingCtx.organizationId = organizationId;
|
|
||||||
ctx.auditLoggingCtx.oldObject = oldSurvey;
|
|
||||||
|
|
||||||
const newSurvey = await updateSurvey(parsedInput);
|
|
||||||
|
|
||||||
ctx.auditLoggingCtx.newObject = newSurvey;
|
|
||||||
|
|
||||||
return newSurvey;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import { redirect } from "next/navigation";
|
|
||||||
|
|
||||||
const Page = async (props) => {
|
|
||||||
const params = await props.params;
|
|
||||||
return redirect(`/environments/${params.environmentId}/surveys/${params.surveyId}/summary`);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Page;
|
|
||||||
-208
@@ -1,208 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { CheckCircle2, Sparkles } from "lucide-react";
|
|
||||||
import { useState } from "react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { Button } from "@/modules/ui/components/button";
|
|
||||||
|
|
||||||
const FORMBRICKS_HOST = "https://app.formbricks.com";
|
|
||||||
const SURVEY_ID = "cr9r4b2r73x6hlmn5aa2ha44";
|
|
||||||
const ENVIRONMENT_ID = "cmk41i8bi92bdad01svi74dec";
|
|
||||||
|
|
||||||
interface WorkflowsPageProps {
|
|
||||||
userEmail: string;
|
|
||||||
organizationName: string;
|
|
||||||
billingPlan: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
type Step = "prompt" | "followup" | "thankyou";
|
|
||||||
|
|
||||||
export const WorkflowsPage = ({ userEmail, organizationName, billingPlan }: WorkflowsPageProps) => {
|
|
||||||
const { t } = useTranslation();
|
|
||||||
const [step, setStep] = useState<Step>("prompt");
|
|
||||||
const [promptValue, setPromptValue] = useState("");
|
|
||||||
const [detailsValue, setDetailsValue] = useState("");
|
|
||||||
const [responseId, setResponseId] = useState<string | null>(null);
|
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
||||||
|
|
||||||
const handleGenerateWorkflow = async () => {
|
|
||||||
if (promptValue.trim().length < 100 || isSubmitting) return;
|
|
||||||
setIsSubmitting(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${FORMBRICKS_HOST}/api/v2/client/${ENVIRONMENT_ID}/responses`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({
|
|
||||||
surveyId: SURVEY_ID,
|
|
||||||
finished: false,
|
|
||||||
data: {
|
|
||||||
workflow: promptValue.trim(),
|
|
||||||
useremail: userEmail,
|
|
||||||
orgname: organizationName,
|
|
||||||
billingplan: billingPlan,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
const json = await res.json();
|
|
||||||
setResponseId(json.data?.id ?? null);
|
|
||||||
}
|
|
||||||
|
|
||||||
setStep("followup");
|
|
||||||
} catch {
|
|
||||||
setStep("followup");
|
|
||||||
} finally {
|
|
||||||
setIsSubmitting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmitFeedback = async () => {
|
|
||||||
if (isSubmitting) return;
|
|
||||||
setIsSubmitting(true);
|
|
||||||
|
|
||||||
if (responseId) {
|
|
||||||
try {
|
|
||||||
await fetch(`${FORMBRICKS_HOST}/api/v1/client/${ENVIRONMENT_ID}/responses/${responseId}`, {
|
|
||||||
method: "PUT",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({
|
|
||||||
finished: true,
|
|
||||||
data: {
|
|
||||||
details: detailsValue.trim(),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// silently fail
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsSubmitting(false);
|
|
||||||
setStep("thankyou");
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSkipFeedback = async () => {
|
|
||||||
if (!responseId) {
|
|
||||||
setStep("thankyou");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await fetch(`${FORMBRICKS_HOST}/api/v1/client/${ENVIRONMENT_ID}/responses/${responseId}`, {
|
|
||||||
method: "PUT",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({
|
|
||||||
finished: true,
|
|
||||||
data: {},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// silently fail
|
|
||||||
}
|
|
||||||
|
|
||||||
setStep("thankyou");
|
|
||||||
};
|
|
||||||
|
|
||||||
if (step === "prompt") {
|
|
||||||
return (
|
|
||||||
<div className="flex h-full flex-col items-center px-4 pt-[15vh]">
|
|
||||||
<div className="w-full max-w-2xl space-y-8">
|
|
||||||
<div className="space-y-3 text-center">
|
|
||||||
<div className="from-brand-light to-brand-dark mx-auto mb-4 flex h-12 w-12 items-center justify-center rounded-xl bg-gradient-to-br shadow-md">
|
|
||||||
<Sparkles className="h-6 w-6 text-white" />
|
|
||||||
</div>
|
|
||||||
<h1 className="text-4xl font-bold tracking-tight text-slate-800">{t("workflows.heading")}</h1>
|
|
||||||
<p className="text-lg text-slate-500">{t("workflows.subheading")}</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="relative">
|
|
||||||
<textarea
|
|
||||||
value={promptValue}
|
|
||||||
onChange={(e) => setPromptValue(e.target.value)}
|
|
||||||
placeholder={t("workflows.placeholder")}
|
|
||||||
rows={5}
|
|
||||||
className="focus:border-brand-dark focus:ring-brand-light/20 w-full resize-none rounded-xl border border-slate-200 bg-white px-5 py-4 text-base text-slate-800 shadow-sm transition-all placeholder:text-slate-400 focus:outline-none focus:ring-2"
|
|
||||||
onKeyDown={(e) => {
|
|
||||||
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
|
||||||
handleGenerateWorkflow();
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<div className="mt-3 flex items-center justify-between">
|
|
||||||
<span
|
|
||||||
className={`text-xs ${promptValue.trim().length >= 100 ? "text-slate-400" : "text-amber-500"}`}>
|
|
||||||
{promptValue.trim().length} / 100
|
|
||||||
</span>
|
|
||||||
<Button
|
|
||||||
onClick={handleGenerateWorkflow}
|
|
||||||
disabled={promptValue.trim().length < 100 || isSubmitting}
|
|
||||||
loading={isSubmitting}
|
|
||||||
size="lg">
|
|
||||||
<Sparkles className="h-4 w-4" />
|
|
||||||
{t("workflows.generate_button")}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (step === "followup") {
|
|
||||||
return (
|
|
||||||
<div className="flex h-full flex-col items-center px-4 pt-[15vh]">
|
|
||||||
<div className="w-full max-w-2xl space-y-8">
|
|
||||||
<div className="space-y-3 text-center">
|
|
||||||
<div className="mx-auto mb-4 flex h-12 w-12 items-center justify-center rounded-xl bg-slate-100">
|
|
||||||
<Sparkles className="text-brand-dark h-6 w-6" />
|
|
||||||
</div>
|
|
||||||
<h1 className="text-3xl font-bold tracking-tight text-slate-800">
|
|
||||||
{t("workflows.coming_soon_title")}
|
|
||||||
</h1>
|
|
||||||
<p className="mx-auto max-w-md text-base text-slate-500">
|
|
||||||
{t("workflows.coming_soon_description")}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="rounded-xl border border-slate-200 bg-white p-6 shadow-sm">
|
|
||||||
<label className="text-md mb-2 block font-medium text-slate-700">
|
|
||||||
{t("workflows.follow_up_label")}
|
|
||||||
</label>
|
|
||||||
<textarea
|
|
||||||
value={detailsValue}
|
|
||||||
onChange={(e) => setDetailsValue(e.target.value)}
|
|
||||||
placeholder={t("workflows.follow_up_placeholder")}
|
|
||||||
rows={4}
|
|
||||||
className="focus:border-brand-dark focus:ring-brand-light/20 w-full resize-none rounded-lg border border-slate-200 bg-slate-50 px-4 py-3 text-sm text-slate-800 transition-all placeholder:text-slate-400 focus:bg-white focus:outline-none focus:ring-2"
|
|
||||||
/>
|
|
||||||
<div className="mt-4 flex items-center justify-end gap-3">
|
|
||||||
<Button variant="ghost" onClick={handleSkipFeedback} className="text-slate-500">
|
|
||||||
{t("common.skip")}
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
onClick={handleSubmitFeedback}
|
|
||||||
disabled={!detailsValue.trim() || isSubmitting}
|
|
||||||
loading={isSubmitting}>
|
|
||||||
{t("workflows.submit_button")}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex h-full flex-col items-center px-4 pt-[15vh]">
|
|
||||||
<div className="w-full max-w-md space-y-6 text-center">
|
|
||||||
<div className="mx-auto flex h-16 w-16 items-center justify-center rounded-full bg-green-50">
|
|
||||||
<CheckCircle2 className="h-8 w-8 text-green-500" />
|
|
||||||
</div>
|
|
||||||
<h1 className="text-2xl font-bold text-slate-800">{t("workflows.thank_you_title")}</h1>
|
|
||||||
<p className="text-base text-slate-500">{t("workflows.thank_you_description")}</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
import { Metadata } from "next";
|
|
||||||
import { notFound, redirect } from "next/navigation";
|
|
||||||
import { IS_FORMBRICKS_CLOUD } from "@/lib/constants";
|
|
||||||
import { getUser } from "@/lib/user/service";
|
|
||||||
import { getEnvironmentAuth } from "@/modules/environments/lib/utils";
|
|
||||||
import { WorkflowsPage } from "./components/workflows-page";
|
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
|
||||||
title: "Workflows",
|
|
||||||
};
|
|
||||||
|
|
||||||
const Page = async (props: { params: Promise<{ environmentId: string }> }) => {
|
|
||||||
const params = await props.params;
|
|
||||||
|
|
||||||
if (!IS_FORMBRICKS_CLOUD) {
|
|
||||||
return notFound();
|
|
||||||
}
|
|
||||||
|
|
||||||
const { session, organization, isBilling } = await getEnvironmentAuth(params.environmentId);
|
|
||||||
|
|
||||||
if (isBilling) {
|
|
||||||
return redirect(`/environments/${params.environmentId}/settings/billing`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await getUser(session.user.id);
|
|
||||||
if (!user) {
|
|
||||||
return redirect("/auth/login");
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<WorkflowsPage
|
|
||||||
userEmail={user.email}
|
|
||||||
organizationName={organization.name}
|
|
||||||
billingPlan={organization.billing.plan}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Page;
|
|
||||||
-3
@@ -1,3 +0,0 @@
|
|||||||
import { AppConnectionLoading } from "@/modules/projects/settings/(setup)/app-connection/loading";
|
|
||||||
|
|
||||||
export default AppConnectionLoading;
|
|
||||||
-3
@@ -1,3 +0,0 @@
|
|||||||
import { AppConnectionPage } from "@/modules/projects/settings/(setup)/app-connection/page";
|
|
||||||
|
|
||||||
export default AppConnectionPage;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { GeneralSettingsLoading } from "@/modules/projects/settings/general/loading";
|
|
||||||
|
|
||||||
export default GeneralSettingsLoading;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { GeneralSettingsPage } from "@/modules/projects/settings/general/page";
|
|
||||||
|
|
||||||
export default GeneralSettingsPage;
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
"use server";
|
|
||||||
|
|
||||||
import { z } from "zod";
|
|
||||||
import { ZId } from "@formbricks/types/common";
|
|
||||||
import { ZIntegrationInput } from "@formbricks/types/integration";
|
|
||||||
import { createOrUpdateIntegration, deleteIntegration } from "@/lib/integration/service";
|
|
||||||
import { getPostHogClient } from "@/lib/posthog-server";
|
|
||||||
import { authenticatedActionClient } from "@/lib/utils/action-client";
|
|
||||||
import { checkAuthorizationUpdated } from "@/lib/utils/action-client/action-client-middleware";
|
|
||||||
import { AuthenticatedActionClientCtx } from "@/lib/utils/action-client/types/context";
|
|
||||||
import {
|
|
||||||
getOrganizationIdFromEnvironmentId,
|
|
||||||
getOrganizationIdFromIntegrationId,
|
|
||||||
getProjectIdFromEnvironmentId,
|
|
||||||
getProjectIdFromIntegrationId,
|
|
||||||
} from "@/lib/utils/helper";
|
|
||||||
import { withAuditLogging } from "@/modules/ee/audit-logs/lib/handler";
|
|
||||||
|
|
||||||
const ZCreateOrUpdateIntegrationAction = z.object({
|
|
||||||
environmentId: ZId,
|
|
||||||
integrationData: ZIntegrationInput,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const createOrUpdateIntegrationAction = authenticatedActionClient
|
|
||||||
.inputSchema(ZCreateOrUpdateIntegrationAction)
|
|
||||||
.action(
|
|
||||||
withAuditLogging(
|
|
||||||
"createdUpdated",
|
|
||||||
"integration",
|
|
||||||
async ({
|
|
||||||
ctx,
|
|
||||||
parsedInput,
|
|
||||||
}: {
|
|
||||||
ctx: AuthenticatedActionClientCtx;
|
|
||||||
parsedInput: Record<string, any>;
|
|
||||||
}) => {
|
|
||||||
const organizationId = await getOrganizationIdFromEnvironmentId(parsedInput.environmentId);
|
|
||||||
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user.id,
|
|
||||||
organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner", "manager"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "projectTeam",
|
|
||||||
minPermission: "readWrite",
|
|
||||||
projectId: await getProjectIdFromEnvironmentId(parsedInput.environmentId),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.auditLoggingCtx.organizationId = organizationId;
|
|
||||||
const result = await createOrUpdateIntegration(
|
|
||||||
parsedInput.environmentId,
|
|
||||||
parsedInput.integrationData
|
|
||||||
);
|
|
||||||
ctx.auditLoggingCtx.integrationId = result.id;
|
|
||||||
ctx.auditLoggingCtx.newObject = result;
|
|
||||||
|
|
||||||
const posthog = getPostHogClient();
|
|
||||||
posthog.capture({
|
|
||||||
distinctId: ctx.user.email,
|
|
||||||
event: "integration_connected",
|
|
||||||
properties: {
|
|
||||||
integrationType: parsedInput.integrationData.type,
|
|
||||||
environmentId: parsedInput.environmentId,
|
|
||||||
organizationId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const ZDeleteIntegrationAction = z.object({
|
|
||||||
integrationId: ZId,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const deleteIntegrationAction = authenticatedActionClient.inputSchema(ZDeleteIntegrationAction).action(
|
|
||||||
withAuditLogging(
|
|
||||||
"deleted",
|
|
||||||
"integration",
|
|
||||||
async ({ ctx, parsedInput }: { ctx: AuthenticatedActionClientCtx; parsedInput: Record<string, any> }) => {
|
|
||||||
const organizationId = await getOrganizationIdFromIntegrationId(parsedInput.integrationId);
|
|
||||||
|
|
||||||
await checkAuthorizationUpdated({
|
|
||||||
userId: ctx.user.id,
|
|
||||||
organizationId,
|
|
||||||
access: [
|
|
||||||
{
|
|
||||||
type: "organization",
|
|
||||||
roles: ["owner", "manager"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "projectTeam",
|
|
||||||
projectId: await getProjectIdFromIntegrationId(parsedInput.integrationId),
|
|
||||||
minPermission: "readWrite",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.auditLoggingCtx.organizationId = organizationId;
|
|
||||||
ctx.auditLoggingCtx.integrationId = parsedInput.integrationId;
|
|
||||||
const result = await deleteIntegration(parsedInput.integrationId);
|
|
||||||
ctx.auditLoggingCtx.oldObject = result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
import { redirect } from "next/navigation";
|
|
||||||
import { TIntegrationSlack } from "@formbricks/types/integration/slack";
|
|
||||||
import { getSurveys } from "@/app/(app)/environments/[environmentId]/workspace/integrations/lib/surveys";
|
|
||||||
import { SlackWrapper } from "@/app/(app)/environments/[environmentId]/workspace/integrations/slack/components/SlackWrapper";
|
|
||||||
import { SLACK_CLIENT_ID, SLACK_CLIENT_SECRET, WEBAPP_URL } from "@/lib/constants";
|
|
||||||
import { getIntegrationByType } from "@/lib/integration/service";
|
|
||||||
import { findMatchingLocale } from "@/lib/utils/locale";
|
|
||||||
import { getTranslate } from "@/lingodotdev/server";
|
|
||||||
import { getEnvironmentAuth } from "@/modules/environments/lib/utils";
|
|
||||||
import { GoBackButton } from "@/modules/ui/components/go-back-button";
|
|
||||||
import { PageContentWrapper } from "@/modules/ui/components/page-content-wrapper";
|
|
||||||
import { PageHeader } from "@/modules/ui/components/page-header";
|
|
||||||
|
|
||||||
const Page = async (props) => {
|
|
||||||
const params = await props.params;
|
|
||||||
const isEnabled = !!(SLACK_CLIENT_ID && SLACK_CLIENT_SECRET);
|
|
||||||
|
|
||||||
const t = await getTranslate();
|
|
||||||
|
|
||||||
const { isReadOnly, environment } = await getEnvironmentAuth(params.environmentId);
|
|
||||||
|
|
||||||
const [surveys, slackIntegration] = await Promise.all([
|
|
||||||
getSurveys(params.environmentId),
|
|
||||||
getIntegrationByType(params.environmentId, "slack"),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const locale = await findMatchingLocale();
|
|
||||||
|
|
||||||
if (isReadOnly) {
|
|
||||||
return redirect("./");
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<PageContentWrapper>
|
|
||||||
<GoBackButton url={`${WEBAPP_URL}/environments/${params.environmentId}/workspace/integrations`} />
|
|
||||||
<PageHeader pageTitle={t("environments.integrations.slack.slack_integration")} />
|
|
||||||
<div className="h-[75vh] w-full">
|
|
||||||
<SlackWrapper
|
|
||||||
isEnabled={isEnabled}
|
|
||||||
environment={environment}
|
|
||||||
surveys={surveys}
|
|
||||||
slackIntegration={slackIntegration as TIntegrationSlack}
|
|
||||||
webAppUrl={WEBAPP_URL}
|
|
||||||
locale={locale}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</PageContentWrapper>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Page;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { LanguagesLoading } from "@/modules/ee/languages/loading";
|
|
||||||
|
|
||||||
export default LanguagesLoading;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { LanguagesPage } from "@/modules/ee/languages/page";
|
|
||||||
|
|
||||||
export default LanguagesPage;
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
import { ProjectSettingsLayout, metadata } from "@/modules/projects/settings/layout";
|
|
||||||
|
|
||||||
export { metadata };
|
|
||||||
export default ProjectSettingsLayout;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { ProjectLookSettingsLoading } from "@/modules/projects/settings/look/loading";
|
|
||||||
|
|
||||||
export default ProjectLookSettingsLoading;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { ProjectLookSettingsPage } from "@/modules/projects/settings/look/page";
|
|
||||||
|
|
||||||
export default ProjectLookSettingsPage;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { ProjectSettingsPage } from "@/modules/projects/settings/page";
|
|
||||||
|
|
||||||
export default ProjectSettingsPage;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { TagsLoading } from "@/modules/projects/settings/tags/loading";
|
|
||||||
|
|
||||||
export default TagsLoading;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { TagsPage } from "@/modules/projects/settings/tags/page";
|
|
||||||
|
|
||||||
export default TagsPage;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { ProjectTeams } from "@/modules/ee/teams/project-teams/page";
|
|
||||||
|
|
||||||
export default ProjectTeams;
|
|
||||||
@@ -1,13 +1,21 @@
|
|||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { ChatwootWidget } from "@/app/chatwoot/ChatwootWidget";
|
import { ChatwootWidget } from "@/app/chatwoot/ChatwootWidget";
|
||||||
import { CHATWOOT_BASE_URL, CHATWOOT_WEBSITE_TOKEN, IS_CHATWOOT_CONFIGURED } from "@/lib/constants";
|
import { PostHogIdentify } from "@/app/posthog/PostHogIdentify";
|
||||||
|
import {
|
||||||
|
CHATWOOT_BASE_URL,
|
||||||
|
CHATWOOT_WEBSITE_TOKEN,
|
||||||
|
IS_CHATWOOT_CONFIGURED,
|
||||||
|
POSTHOG_KEY,
|
||||||
|
SESSION_MAX_AGE,
|
||||||
|
} from "@/lib/constants";
|
||||||
import { getUser } from "@/lib/user/service";
|
import { getUser } from "@/lib/user/service";
|
||||||
|
import { NextAuthProvider } from "@/modules/auth/components/next-auth-provider";
|
||||||
import { authOptions } from "@/modules/auth/lib/authOptions";
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
||||||
import { ClientLogout } from "@/modules/ui/components/client-logout";
|
import { ClientLogout } from "@/modules/ui/components/client-logout";
|
||||||
import { NoMobileOverlay } from "@/modules/ui/components/no-mobile-overlay";
|
import { NoMobileOverlay } from "@/modules/ui/components/no-mobile-overlay";
|
||||||
import { ToasterClient } from "@/modules/ui/components/toaster-client";
|
import { ToasterClient } from "@/modules/ui/components/toaster-client";
|
||||||
|
|
||||||
const AppLayout = async ({ children }) => {
|
const AppLayout = async ({ children }: { children: React.ReactNode }) => {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
const user = session?.user?.id ? await getUser(session.user.id) : null;
|
const user = session?.user?.id ? await getUser(session.user.id) : null;
|
||||||
|
|
||||||
@@ -17,8 +25,11 @@ const AppLayout = async ({ children }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<NextAuthProvider sessionMaxAge={SESSION_MAX_AGE}>
|
||||||
<NoMobileOverlay />
|
<NoMobileOverlay />
|
||||||
|
{POSTHOG_KEY && user && (
|
||||||
|
<PostHogIdentify posthogKey={POSTHOG_KEY} userId={user.id} email={user.email} name={user.name} />
|
||||||
|
)}
|
||||||
{IS_CHATWOOT_CONFIGURED && (
|
{IS_CHATWOOT_CONFIGURED && (
|
||||||
<ChatwootWidget
|
<ChatwootWidget
|
||||||
userEmail={user?.email}
|
userEmail={user?.email}
|
||||||
@@ -30,7 +41,7 @@ const AppLayout = async ({ children }) => {
|
|||||||
)}
|
)}
|
||||||
<ToasterClient />
|
<ToasterClient />
|
||||||
{children}
|
{children}
|
||||||
</>
|
</NextAuthProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { ChartsListPage } from "@/modules/ee/analysis/charts/components/charts-list-page";
|
||||||
|
|
||||||
|
const ChartsPage = async (props: Readonly<{ params: Promise<{ workspaceId: string }> }>) => {
|
||||||
|
const { workspaceId } = await props.params;
|
||||||
|
return <ChartsListPage workspaceId={workspaceId} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ChartsPage;
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
import { DashboardDetailPage } from "@/modules/ee/analysis/dashboards/pages/dashboard-detail-page";
|
||||||
|
|
||||||
|
const Page = (props: { params: Promise<{ workspaceId: string; dashboardId: string }> }) => {
|
||||||
|
return <DashboardDetailPage params={props.params} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Page;
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { DashboardsListPage } from "@/modules/ee/analysis/dashboards/pages/dashboards-list-page";
|
||||||
|
|
||||||
|
const DashboardsPage = async (props: Readonly<{ params: Promise<{ workspaceId: string }> }>) => {
|
||||||
|
const { workspaceId } = await props.params;
|
||||||
|
return <DashboardsListPage workspaceId={workspaceId} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DashboardsPage;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import { AnalysisListLoading } from "@/modules/ee/analysis/loading";
|
||||||
|
|
||||||
|
export default AnalysisListLoading;
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
import { AppConnectionLoading } from "@/modules/workspaces/settings/(setup)/app-connection/loading";
|
||||||
|
|
||||||
|
export default AppConnectionLoading;
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
import { AppConnectionPage } from "@/modules/workspaces/settings/(setup)/app-connection/page";
|
||||||
|
|
||||||
|
export default AppConnectionPage;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import { GeneralSettingsLoading } from "@/modules/workspaces/settings/general/loading";
|
||||||
|
|
||||||
|
export default GeneralSettingsLoading;
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user