- {filteredThreads.map((thread: Thread, key) => (
+
+ {threads.data.map((thread: Thread) => (
void
- changeTab: (tab: null | number) => void
-}
-
-const BrowseContext = React.createContext(
- undefined
-)
-
-export function useBrowse() {
- const context = React.useContext(BrowseContext)
- if (!context) {
- throw new Error('useBrowseContext must be used within a BrowseProvider')
- }
- return context
-}
-
-interface BrowseProviderProps {
- children: React.ReactNode
-}
-
-export function BrowseProvider({ children }: BrowseProviderProps) {
- const [keyword, setKeyword] = React.useState('')
- const [tab, setTab] = React.useState(null)
-
- const changeTab = (tab: null | number) => {
- setTab(tab)
- }
-
- const changeKeyword = (keyword: string) => {
- setKeyword(keyword)
- }
-
- return (
-
- {children}
-
- )
-}
diff --git a/apps/masterbots.ai/lib/number.ts b/apps/masterbots.ai/lib/number.ts
new file mode 100644
index 00000000..981b6142
--- /dev/null
+++ b/apps/masterbots.ai/lib/number.ts
@@ -0,0 +1,5 @@
+// Function to generate a random number as a string
+export const generateRandomNumber = (length: number): string => {
+ const randomNumber = Math.floor(Math.random() * Math.pow(10, length))
+ return randomNumber.toString().padStart(length, '0')
+}
\ No newline at end of file
diff --git a/apps/masterbots.ai/lib/url.ts b/apps/masterbots.ai/lib/url.ts
new file mode 100644
index 00000000..96fd0202
--- /dev/null
+++ b/apps/masterbots.ai/lib/url.ts
@@ -0,0 +1,33 @@
+import { z, ZodSchema } from 'zod'
+
+// Zod schema for validating slug strings
+export const SlugSchema: ZodSchema = z.string()
+ .min(1)
+ .regex(/^[a-z0-9]+[a-z0-9+_-]*[a-z0-9]+$/, "Invalid slug format.")
+
+// Function to convert a username into a slug
+export const toSlug = (username: string, separator: string): string => {
+ return username
+ .toLowerCase()
+ .replace(/&/g, '_')
+ .replace(/ & /g, '_')
+ .replace(/[^a-z0-9_]/g, separator)
+}
+
+// Function to simulate converting a slug back to a username
+export const fromSlug = (slug: string, separator: string): string => {
+ return slug
+ .replace(new RegExp(`[${separator}]+`, 'g'), ' ')
+ .replace(/_/g, '&')
+}
+
+//Encodes a string for use in a URL, replacing spaces with the '+' character.
+export const encodeQuery = (input: string): string => {
+ return encodeURIComponent(input).replace(/%20/g, '+')
+}
+
+//Decodes a URL-encoded string, converting '+' back into spaces.
+
+export const decodeQuery = (input: string): string => {
+ return decodeURIComponent(input.replace(/\+/g, ' '))
+}
diff --git a/apps/masterbots.ai/lib/username.ts b/apps/masterbots.ai/lib/username.ts
new file mode 100644
index 00000000..b01470ff
--- /dev/null
+++ b/apps/masterbots.ai/lib/username.ts
@@ -0,0 +1,24 @@
+import { z, ZodSchema } from 'zod'
+import { generateRandomNumber } from './number'
+
+// Zod schema for validating usernames
+export const UsernameSchema: ZodSchema = z.string()
+ .min(11, { message: "Username must be at least 11 characters long." })
+ .max(20, { message: "Username must not exceed 20 characters." })
+ .regex(/^[a-z0-9_]*$/, { message: "Username must contain only lowercase letters, numbers, and underscores." })
+
+// Function to generate a username from an OAuth profile name
+export const generateUsername = (name: string): string => {
+ if (!name) return `user_${generateRandomNumber(6)}`
+
+ let username = name.toLowerCase().replace(/[^a-z0-9]/g, '_')
+
+ if (username.length < 10) {
+ username += `_${generateRandomNumber(7)}`
+ } else if (username.length > 20) {
+ username = username.substring(0, 20)
+ }
+
+ return UsernameSchema.parse(username)
+}
+
diff --git a/apps/masterbots.ai/lib/utils.ts b/apps/masterbots.ai/lib/utils.ts
index ccacf839..2c55ed30 100644
--- a/apps/masterbots.ai/lib/utils.ts
+++ b/apps/masterbots.ai/lib/utils.ts
@@ -11,28 +11,6 @@ export const nanoid = customAlphabet(
7
) // 7-character random string
-export async function fetcher(
- input: RequestInfo,
- init?: RequestInit
-): Promise {
- const res = await fetch(input, init)
-
- if (!res.ok) {
- const json = await res.json()
- if (json.error) {
- const error = new Error(json.error) as Error & {
- status: number
- }
- error.status = res.status
- throw error
- } else {
- throw new Error('An unexpected error occurred')
- }
- }
-
- return res.json()
-}
-
export function formatDate(input: string | number | Date): string {
const date = new Date(input)
return date.toLocaleDateString('en-US', {
diff --git a/apps/masterbots.ai/services/hasura/hasura.service.ts b/apps/masterbots.ai/services/hasura/hasura.service.ts
index aac03b46..98331d4e 100644
--- a/apps/masterbots.ai/services/hasura/hasura.service.ts
+++ b/apps/masterbots.ai/services/hasura/hasura.service.ts
@@ -350,13 +350,14 @@ export async function getChatbot({
export async function getBrowseThreads({
categoryId,
- keyword,
+ query,
chatbotName,
userId,
limit,
offset,
slug
}: GetBrowseThreadsParams) {
+
const client = getHasuraClient({})
const { thread } = await client.query({
@@ -377,18 +378,18 @@ export async function getBrowseThreads({
...everything,
__args: {
orderBy: [{ createdAt: 'ASC' }],
- ...(keyword
+ ...(query
? {
where: {
_or: [
{
content: {
- _iregex: keyword
+ _iregex: query
}
},
{
content: {
- _eq: keyword
+ _eq: query
}
}
]
diff --git a/apps/masterbots.ai/services/hasura/hasura.service.type.ts b/apps/masterbots.ai/services/hasura/hasura.service.type.ts
index e6eccff9..da7eb610 100644
--- a/apps/masterbots.ai/services/hasura/hasura.service.type.ts
+++ b/apps/masterbots.ai/services/hasura/hasura.service.type.ts
@@ -13,7 +13,7 @@ export interface GetThreadsParams extends HasuraServiceParams {
chatbotName?: string
userId: string
categoryId?: number | null
- keyword?: string
+ query?: string
limit?: number
offset?: number
}
@@ -52,7 +52,7 @@ export interface GetChatbotParams extends HasuraServiceParams {
export interface GetBrowseThreadsParams {
categoryId?: number | null
- keyword?: string
+ query?: string
userId?: string
chatbotName?: string
slug?: string | null
diff --git a/apps/masterbots.ai/lib/types.ts b/apps/masterbots.ai/types/chat.ts
similarity index 100%
rename from apps/masterbots.ai/lib/types.ts
rename to apps/masterbots.ai/types/chat.ts
diff --git a/bun.lockb b/bun.lockb
index 4fe53473..351be7a1 100755
Binary files a/bun.lockb and b/bun.lockb differ