forked from stakwork/sphinx-tribes-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request stakwork#751 from stakwork/fix/chat_bounty
fix: fix prettier issue
- Loading branch information
Showing
3 changed files
with
273 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,90 @@ | ||
import { TribesURL } from '../config'; | ||
import { Chat, ChatMessage, ContextTag } from '../store/interface'; | ||
import { uiStore } from '../store/ui'; | ||
|
||
export class ChatService { | ||
async getChat(chat_id: string): Promise<Chat | undefined> { | ||
try { | ||
if (!uiStore.meInfo) return undefined; | ||
const info = uiStore.meInfo; | ||
|
||
const response = await fetch(`${TribesURL}/chat/${chat_id}`, { | ||
method: 'GET', | ||
mode: 'cors', | ||
headers: { | ||
'x-jwt': info.tribe_jwt, | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} catch (e) { | ||
console.error('Error getting chat:', e); | ||
return undefined; | ||
} | ||
} | ||
|
||
async getChatHistory(chat_id: string): Promise<ChatMessage[] | undefined> { | ||
try { | ||
if (!uiStore.meInfo) return undefined; | ||
const info = uiStore.meInfo; | ||
|
||
const response = await fetch(`${TribesURL}/chat/${chat_id}/messages`, { | ||
method: 'GET', | ||
mode: 'cors', | ||
headers: { | ||
'x-jwt': info.tribe_jwt, | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} catch (e) { | ||
console.error('Error loading chat history:', e); | ||
return undefined; | ||
} | ||
} | ||
|
||
async sendMessage( | ||
chat_id: string, | ||
message: string, | ||
contextTags?: ContextTag[] | ||
): Promise<ChatMessage | undefined> { | ||
try { | ||
if (!uiStore.meInfo) return undefined; | ||
const info = uiStore.meInfo; | ||
|
||
const response = await fetch(`${TribesURL}/chat/${chat_id}/messages`, { | ||
method: 'POST', | ||
mode: 'cors', | ||
headers: { | ||
'x-jwt': info.tribe_jwt, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
message, | ||
context_tags: contextTags | ||
}) | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} catch (e) { | ||
console.error('Error sending message:', e); | ||
return undefined; | ||
} | ||
} | ||
} | ||
|
||
export const chatService = new ChatService(); | ||
import { TribesURL } from '../config'; | ||
import { Chat, ChatMessage, ContextTag } from '../store/interface'; | ||
import { uiStore } from '../store/ui'; | ||
|
||
export class ChatService { | ||
async getChat(chat_id: string): Promise<Chat | undefined> { | ||
try { | ||
if (!uiStore.meInfo) return undefined; | ||
const info = uiStore.meInfo; | ||
|
||
const response = await fetch(`${TribesURL}/chat/${chat_id}`, { | ||
method: 'GET', | ||
mode: 'cors', | ||
headers: { | ||
'x-jwt': info.tribe_jwt, | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} catch (e) { | ||
console.error('Error getting chat:', e); | ||
return undefined; | ||
} | ||
} | ||
|
||
async getChatHistory(chat_id: string): Promise<ChatMessage[] | undefined> { | ||
try { | ||
if (!uiStore.meInfo) return undefined; | ||
const info = uiStore.meInfo; | ||
|
||
const response = await fetch(`${TribesURL}/chat/${chat_id}/messages`, { | ||
method: 'GET', | ||
mode: 'cors', | ||
headers: { | ||
'x-jwt': info.tribe_jwt, | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} catch (e) { | ||
console.error('Error loading chat history:', e); | ||
return undefined; | ||
} | ||
} | ||
|
||
async sendMessage( | ||
chat_id: string, | ||
message: string, | ||
contextTags?: ContextTag[] | ||
): Promise<ChatMessage | undefined> { | ||
try { | ||
if (!uiStore.meInfo) return undefined; | ||
const info = uiStore.meInfo; | ||
|
||
const response = await fetch(`${TribesURL}/chat/${chat_id}/messages`, { | ||
method: 'POST', | ||
mode: 'cors', | ||
headers: { | ||
'x-jwt': info.tribe_jwt, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
message, | ||
context_tags: contextTags | ||
}) | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} catch (e) { | ||
console.error('Error sending message:', e); | ||
return undefined; | ||
} | ||
} | ||
} | ||
|
||
export const chatService = new ChatService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,88 @@ | ||
import { makeAutoObservable } from 'mobx'; | ||
|
||
interface FeaturedBounty { | ||
bountyId: string; | ||
sequence: number; | ||
} | ||
|
||
class BountyStore { | ||
featuredBounties: FeaturedBounty[] = []; | ||
|
||
constructor() { | ||
makeAutoObservable(this); | ||
this.loadFromStorage(); | ||
} | ||
|
||
private loadFromStorage(): void { | ||
try { | ||
const saved = localStorage.getItem('featuredBounties'); | ||
if (saved) { | ||
this.featuredBounties = JSON.parse(saved); | ||
} | ||
} catch (error) { | ||
console.error('Error loading from storage:', error); | ||
} | ||
} | ||
|
||
private saveToStorage(): void { | ||
try { | ||
localStorage.setItem('featuredBounties', JSON.stringify(this.featuredBounties)); | ||
} catch (error) { | ||
console.error('Error saving to storage:', error); | ||
} | ||
} | ||
|
||
getBountyIdFromURL(url: string): string | null { | ||
const match = url.match(/\/bounty\/(\d+)$/); | ||
return match ? match[1] : null; | ||
} | ||
|
||
addFeaturedBounty(bountyId: string): void { | ||
const nextSequence = this.featuredBounties.length + 1; | ||
const exists = this.featuredBounties.some((b: FeaturedBounty) => b.bountyId === bountyId); | ||
|
||
if (!exists) { | ||
this.featuredBounties.push({ | ||
bountyId, | ||
sequence: nextSequence | ||
}); | ||
this.saveToStorage(); | ||
} | ||
} | ||
|
||
removeFeaturedBounty(bountyId: string): void { | ||
this.featuredBounties = this.featuredBounties.filter( | ||
(b: FeaturedBounty) => b.bountyId !== bountyId | ||
); | ||
this.featuredBounties.forEach((bounty: FeaturedBounty, index: number) => { | ||
bounty.sequence = index + 1; | ||
}); | ||
this.saveToStorage(); | ||
} | ||
|
||
getFeaturedBounties(): FeaturedBounty[] { | ||
return this.featuredBounties | ||
.slice() | ||
.sort((a: FeaturedBounty, b: FeaturedBounty) => a.sequence - b.sequence); | ||
} | ||
|
||
updateSequence(bountyId: string, newSequence: number): void { | ||
const bounty = this.featuredBounties.find((b: FeaturedBounty) => b.bountyId === bountyId); | ||
if (bounty) { | ||
bounty.sequence = newSequence; | ||
this.saveToStorage(); | ||
} | ||
} | ||
|
||
hasBounty(bountyId: string): boolean { | ||
const exists = this.featuredBounties.some((b: FeaturedBounty) => b.bountyId === bountyId); | ||
return exists; | ||
} | ||
|
||
clearAllBounties(): void { | ||
this.featuredBounties = []; | ||
this.saveToStorage(); | ||
} | ||
} | ||
|
||
export const bountyStore = new BountyStore(); | ||
import { makeAutoObservable } from 'mobx'; | ||
|
||
interface FeaturedBounty { | ||
bountyId: string; | ||
sequence: number; | ||
} | ||
|
||
class BountyStore { | ||
featuredBounties: FeaturedBounty[] = []; | ||
|
||
constructor() { | ||
makeAutoObservable(this); | ||
this.loadFromStorage(); | ||
} | ||
|
||
private loadFromStorage(): void { | ||
try { | ||
const saved = localStorage.getItem('featuredBounties'); | ||
if (saved) { | ||
this.featuredBounties = JSON.parse(saved); | ||
} | ||
} catch (error) { | ||
console.error('Error loading from storage:', error); | ||
} | ||
} | ||
|
||
private saveToStorage(): void { | ||
try { | ||
localStorage.setItem('featuredBounties', JSON.stringify(this.featuredBounties)); | ||
} catch (error) { | ||
console.error('Error saving to storage:', error); | ||
} | ||
} | ||
|
||
getBountyIdFromURL(url: string): string | null { | ||
const match = url.match(/\/bounty\/(\d+)$/); | ||
return match ? match[1] : null; | ||
} | ||
|
||
addFeaturedBounty(bountyId: string): void { | ||
const nextSequence = this.featuredBounties.length + 1; | ||
const exists = this.featuredBounties.some((b: FeaturedBounty) => b.bountyId === bountyId); | ||
|
||
if (!exists) { | ||
this.featuredBounties.push({ | ||
bountyId, | ||
sequence: nextSequence | ||
}); | ||
this.saveToStorage(); | ||
} | ||
} | ||
|
||
removeFeaturedBounty(bountyId: string): void { | ||
this.featuredBounties = this.featuredBounties.filter( | ||
(b: FeaturedBounty) => b.bountyId !== bountyId | ||
); | ||
this.featuredBounties.forEach((bounty: FeaturedBounty, index: number) => { | ||
bounty.sequence = index + 1; | ||
}); | ||
this.saveToStorage(); | ||
} | ||
|
||
getFeaturedBounties(): FeaturedBounty[] { | ||
return this.featuredBounties | ||
.slice() | ||
.sort((a: FeaturedBounty, b: FeaturedBounty) => a.sequence - b.sequence); | ||
} | ||
|
||
updateSequence(bountyId: string, newSequence: number): void { | ||
const bounty = this.featuredBounties.find((b: FeaturedBounty) => b.bountyId === bountyId); | ||
if (bounty) { | ||
bounty.sequence = newSequence; | ||
this.saveToStorage(); | ||
} | ||
} | ||
|
||
hasBounty(bountyId: string): boolean { | ||
const exists = this.featuredBounties.some((b: FeaturedBounty) => b.bountyId === bountyId); | ||
return exists; | ||
} | ||
|
||
clearAllBounties(): void { | ||
this.featuredBounties = []; | ||
this.saveToStorage(); | ||
} | ||
} | ||
|
||
export const bountyStore = new BountyStore(); |
Oops, something went wrong.