Skip to content

Commit

Permalink
Merge pull request stakwork#751 from stakwork/fix/chat_bounty
Browse files Browse the repository at this point in the history
fix: fix prettier issue
  • Loading branch information
tobi-bams authored Dec 11, 2024
2 parents b83ce48 + eefbad8 commit 510e29c
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 273 deletions.
180 changes: 90 additions & 90 deletions src/services/index.ts
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();
176 changes: 88 additions & 88 deletions src/store/bountyStore.ts
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();
Loading

0 comments on commit 510e29c

Please sign in to comment.