Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Update fetching to use Graphql #8

Merged
merged 5 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,9 @@ const getData = async () => {
The preview bar component fetches all segments from the Prepr API. So, you need to give it access to do this as follows:

1. In your Prepr environment, go to the **Settings → Access tokens** page to view all the access tokens.
2. Click the **Add access token** button to create a new access token, give it a name and choose the segments scope like in the image below.
2. Click the *GraphQL Preview* access token to open it and tick the **Enable edit mode** checkbox and click the **Save** button.

![Segments access token](https://assets-site.prepr.io//2mcoy87hhmfz-segments-access-token.png)

3. Click the **Save** button and copy the generated *Access token*.

4. Add a new variable `PREPR_SEGMENTS_ACCESS_TOKEN` to the `.env` file and paste the value you just copied.

```bash
PREPR_SEGMENTS_ACCESS_TOKEN=<YOUR-SEGMENTS-ACCESS-TOKEN>
```
![Preview access token](https://assets-site.prepr.io/229kaekn7m96//preview-access-token-enable-edit-mode.png)

To implement the *Adaptive Preview Bar* component, navigate to your root layout file, this is usually `layout.tsx`.

Expand All @@ -132,7 +124,7 @@ import '@preprio/prepr-nextjs/dist/components.css'

export default async function RootLayout({children}: {children: React.ReactNode}) {
// Get the props for the PreviewBar component, pass the access token as an argument
const previewBarProps = await getPreviewBarProps(process.env.PREPR_SEGMENTS_ACCESS_TOKEN)
const previewBarProps = await getPreviewBarProps(process.env.PREPR_GRAPHQL_URL)

return (
<html>
Expand Down
3 changes: 2 additions & 1 deletion dist/components.d.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { P as PreprSegment } from './types-DmITW6Tn.mjs';

declare function PreprPreviewBar(props: {
activeSegment?: string | null;
activeVariant?: string | null;
data?: any;
data?: PreprSegment[];
}): React.JSX.Element;

export { PreprPreviewBar };
3 changes: 2 additions & 1 deletion dist/components.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { P as PreprSegment } from './types-DmITW6Tn.js';

declare function PreprPreviewBar(props: {
activeSegment?: string | null;
activeVariant?: string | null;
data?: any;
data?: PreprSegment[];
}): React.JSX.Element;

export { PreprPreviewBar };
79 changes: 41 additions & 38 deletions dist/components.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/components.js.map

Large diffs are not rendered by default.

79 changes: 41 additions & 38 deletions dist/components.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,29 @@ function InfoPopover({ title, text }) {
import { clsx } from "clsx";
function PreprPreviewBar(props) {
const { activeSegment, activeVariant, data } = props;
const [segmentList, setSegmentList] = useState(data == null ? void 0 : data.items);
const [segmentList, setSegmentList] = useState(data);
const [isToggled, setIsToggled] = useState(false);
const searchParams = useSearchParams();
console.log("SEGMENTS:", segmentList);
if (searchParams.get("prepr_hide_bar") === "true") {
return null;
}
if (typeof window !== "undefined" && (window == null ? void 0 : window.parent) !== window.self) {
return null;
}
if (segmentList && segmentList[0] && segmentList[0].reference_id !== "null") {
if (segmentList && segmentList[0] && segmentList[0]._id !== "null") {
setSegmentList([
{
id: "null",
reference_id: "null",
body: "All other users"
_id: "null",
name: "All other users"
},
...segmentList
]);
}
const emptyVariant = "A";
const emptySegment = {
body: "Choose segment"
name: "Choose segment",
_id: "null"
};
useEffect(() => {
if (!window) {
Expand All @@ -140,7 +141,7 @@ function PreprPreviewBar(props) {
});
const [selectedSegment, setSelectedSegment] = useState(
segmentList && segmentList.filter(
(segmentData) => segmentData === activeSegment
(segmentData) => segmentData.name === activeSegment
)[0] || emptySegment
);
const [selectedVariant, setSelectedVariant] = useState(
Expand All @@ -156,7 +157,7 @@ function PreprPreviewBar(props) {
}
if (key === "prepr_preview_segment" && value) {
setSelectedSegment(value);
params.set(key, value.reference_id);
params.set(key, value._id);
}
for (const [key2, value2] of params.entries()) {
if (value2 === "null" || value2 === null || value2 === void 0) {
Expand Down Expand Up @@ -200,7 +201,7 @@ function PreprPreviewBar(props) {
)), /* @__PURE__ */ React4.createElement(
Listbox,
{
value: selectedSegment.slug,
value: selectedSegment._id,
onChange: (value) => handleSearchParams(
"prepr_preview_segment",
value
Expand All @@ -222,7 +223,7 @@ function PreprPreviewBar(props) {
},
className: "prp-w-full prp-overflow-hidden prp-mr-auto"
},
segmentList.length > 0 ? selectedSegment.body : "No segments"
segmentList.length > 0 ? selectedSegment.name : "No segments"
),
/* @__PURE__ */ React4.createElement("div", { className: "prp-text-gray-400" }, /* @__PURE__ */ React4.createElement(FaCaretDown, { className: "prp-w-3" }))
),
Expand All @@ -232,38 +233,40 @@ function PreprPreviewBar(props) {
anchor: "top start",
className: "prp-z-[9999] prp-rounded-md prp-bg-white prp-h-1/3 prp-mt-2 prp-shadow-xl"
},
segmentList == null ? void 0 : segmentList.map((segment) => /* @__PURE__ */ React4.createElement(
ListboxOption,
{
key: segment.id,
value: segment,
className: clsx(
"prp-flex prp-items-center prp-p-2 prp-regular-text prp-z-[100] hover:prp-cursor-pointer prp-w-full prp-pr-4",
segment.reference_id === selectedSegment.reference_id ? "prp-bg-indigo-50 prp-text-indigo-700" : "hover:prp-bg-gray-100 prp-bg-white prp-text-gray-900"
)
},
/* @__PURE__ */ React4.createElement(
FaCheck,
segmentList == null ? void 0 : segmentList.map(
(segment) => /* @__PURE__ */ React4.createElement(
ListboxOption,
{
key: segment._id,
value: segment,
className: clsx(
"prp-size-3 prp-shrink-0 prp-mr-1",
segment.reference_id === selectedSegment.reference_id ? "prp-visible" : "prp-invisible"
"prp-flex prp-items-center prp-p-2 prp-regular-text prp-z-[100] hover:prp-cursor-pointer prp-w-full prp-pr-4",
segment._id === selectedSegment._id ? "prp-bg-indigo-50 prp-text-indigo-700" : "hover:prp-bg-gray-100 prp-bg-white prp-text-gray-900"
)
}
),
/* @__PURE__ */ React4.createElement(
"div",
{
style: {
textWrap: "nowrap",
textOverflow: "ellipsis",
textAlign: "left"
},
className: "prp-w-full prp-overflow-hidden prp-mr-auto"
},
segment.body
/* @__PURE__ */ React4.createElement(
FaCheck,
{
className: clsx(
"prp-size-3 prp-shrink-0 prp-mr-1",
segment._id === selectedSegment._id ? "prp-visible" : "prp-invisible"
)
}
),
/* @__PURE__ */ React4.createElement(
"div",
{
style: {
textWrap: "nowrap",
textOverflow: "ellipsis",
textAlign: "left"
},
className: "prp-w-full prp-overflow-hidden prp-mr-auto"
},
segment.name
)
)
))
)
)
)), /* @__PURE__ */ React4.createElement("div", { className: "prp-flex prp-flex-initial prp-flex-col md:prp-flex-row prp-gap-2 md:prp-gap-4" }, /* @__PURE__ */ React4.createElement("div", { className: "prp-regular-text prp-text-white prp-items-center prp-gap-2 prp-hidden lg:prp-flex" }, /* @__PURE__ */ React4.createElement("span", { className: "prp-pb-0.5 prp-text-xs md:prp-text-base prp-block md:prp-hidden xl:prp-block" }, "Show A/B variant"), /* @__PURE__ */ React4.createElement(
InfoPopover,
Expand Down Expand Up @@ -303,7 +306,7 @@ function PreprPreviewBar(props) {
ResetButton,
{
handleClick: handleReset,
enabled: selectedSegment.reference_id || selectedVariant !== "A"
enabled: selectedSegment._id !== "null" || selectedVariant !== "A"
}
))))
), /* @__PURE__ */ React4.createElement(
Expand Down
2 changes: 1 addition & 1 deletion dist/components.mjs.map

Large diffs are not rendered by default.

26 changes: 5 additions & 21 deletions dist/index.d.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextResponse } from 'next/server';
import { P as PreprSegment } from './types-DmITW6Tn.mjs';

/**
*
Expand All @@ -24,29 +25,12 @@ declare function getActiveVariant(): Promise<string>;
declare function getPreprHeaders(): Promise<{
[key: string]: string;
}>;
type PreprSegment = {
id: string;
created_on: string;
changed_on: string;
synced_on: string;
label: string;
reference_id: string;
body: string;
query: string;
count: number;
};
type PreprSegmentsResponse = {
total: number;
skip: number;
limit: number;
items: PreprSegment[];
};
/**
* Fetches the segments from the Prepr API
* @param token Prepr access token with scope 'segments'
* @returns Object with total, skip, limit and items
* @returns Array of PreprSegmentResponse
*/
declare function getPreprEnvironmentSegments(token: string): Promise<PreprSegmentsResponse>;
declare function getPreprEnvironmentSegments(token: string): Promise<PreprSegment[]>;
/**
* Fetches all the necessary previewbar props
* @param token Prepr access token with scope 'segments'
Expand All @@ -55,7 +39,7 @@ declare function getPreprEnvironmentSegments(token: string): Promise<PreprSegmen
declare function getPreviewBarProps(token: string): Promise<{
activeSegment: string | null;
activeVariant: string | null;
data: PreprSegmentsResponse;
data: PreprSegment[];
}>;

export { PreprMiddleware, type PreprSegment, type PreprSegmentsResponse, getActiveSegment, getActiveVariant, getPreprEnvironmentSegments, getPreprHeaders, getPreprUUID, getPreviewBarProps };
export { PreprMiddleware, getActiveSegment, getActiveVariant, getPreprEnvironmentSegments, getPreprHeaders, getPreprUUID, getPreviewBarProps };
Loading