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

10/21 Tweaks #115

Merged
merged 5 commits into from
Oct 21, 2024
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
3 changes: 2 additions & 1 deletion apps/api/app/api/users/[id]/positions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ export async function GET(
const searchParams = new URLSearchParams(url.search)
const urlParams = Object.fromEntries(searchParams)

const { id, pageSize } = schema.GET.parameters.parse({ ...(params || {}), ...urlParams })
const { id, pageSize, status } = schema.GET.parameters.parse({ ...(params || {}), ...urlParams })

const user = await getUserById({ id })

const { positions } = await getPositions(
{
accountId: user.primaryAccountId,
status,
},
{ field: 'updatedAt', direction: 'desc' },
{ take: pageSize ?? 25, skip: 0 }
Expand Down
5 changes: 4 additions & 1 deletion apps/api/app/api/users/[id]/positions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { MarketOptionPositionSchema, UserSchema } from '@play-money/database'

export default createSchema({
GET: {
parameters: UserSchema.pick({ id: true }).extend({ pageSize: z.coerce.number().optional() }),
parameters: UserSchema.pick({ id: true }).extend({
pageSize: z.coerce.number().optional(),
status: z.enum(['active', 'closed', 'all']).optional(),
}),
responses: {
200: z.object({
positions: z.array(MarketOptionPositionSchema),
Expand Down
2 changes: 1 addition & 1 deletion packages/comments/lib/createComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function createComment({
} else if (entityType === 'LIST') {
const list = await getList({ id: entityId })

if (authorId !== list.ownerId) {
if (![authorId, comment.parent?.authorId, ...userIdsMentioned].includes(list.ownerId)) {
createNotification({
type: 'LIST_COMMENT',
actorId: authorId,
Expand Down
18 changes: 16 additions & 2 deletions packages/finance/lib/getPositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ExtendedMarketOptionPosition } from '../types'

interface PositionsFilterOptions {
accountId?: string
status?: 'active' | 'closed' | 'all'
}

interface SortOptions {
Expand All @@ -20,11 +21,24 @@ export async function getPositions(
sort: SortOptions = { field: 'createdAt', direction: 'desc' },
pagination: PaginationOptions = { skip: 0, take: 10 }
): Promise<{ positions: Array<ExtendedMarketOptionPosition>; total: number }> {
const statusFilters =
filters.status === 'active'
? {
quantity: { gt: 0.0001 },
}
: filters.status === 'closed'
? {
quantity: { lte: 0.0001 },
}
: filters.status === 'all'
? {}
: {}

const [positions, total] = await Promise.all([
db.marketOptionPosition.findMany({
where: {
accountId: filters.accountId,
value: { gt: 0 },
...statusFilters,
},
include: {
market: true,
Expand All @@ -39,7 +53,7 @@ export async function getPositions(
db.marketOptionPosition.count({
where: {
accountId: filters.accountId,
value: { gt: 0 },
...statusFilters,
},
}),
])
Expand Down
1 change: 1 addition & 0 deletions packages/markets/lib/getMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function getMarkets(
lt: new Date(),
},
resolvedAt: null,
canceledAt: null,
}
: filters.status === 'resolved'
? {
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/src/hooks/usePersistForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const getPersistedData = <T>({
defaultValue: T
localStorageKey: string
}): T => {
if (typeof window === 'undefined') {
return defaultValue
}

const data = localStorage.getItem(localStorageKey)

if (data) {
Expand All @@ -37,5 +41,9 @@ export const getPersistedData = <T>({
}

export function clearPresistedData({ localStorageKey }: { localStorageKey: string }) {
if (typeof window === 'undefined') {
return
}

localStorage.removeItem(localStorageKey)
}
14 changes: 12 additions & 2 deletions packages/users/components/UserPositionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ export const columns: Array<ColumnDef<ExtendedMarketPosition>> = [
},
cell: ({ row }) => {
const option = row.original.option
const quantity = new Decimal(row.original.quantity).toDecimalPlaces(4)

return (
<Link href={`/questions/${row.original.market.id}/${row.original.market.slug}`} className="line-clamp-2">
<CurrencyDisplay value={Number(row.original.value)} /> {_.truncate(option.name, { length: 40 })}
{quantity.gt(0) ? (
<>
<CurrencyDisplay value={Number(row.original.value)} /> {_.truncate(option.name, { length: 40 })}
</>
) : (
<>Closed</>
)}
</Link>
)
},
Expand All @@ -49,8 +56,11 @@ export const columns: Array<ColumnDef<ExtendedMarketPosition>> = [
const cost = new Decimal(row.original.cost).toDecimalPlaces(4)
const change = value.sub(cost).div(cost).times(100).round().toNumber()
const changeLabel = `(${change > 0 ? '+' : ''}${change}%)`
const quantity = new Decimal(row.original.quantity).toDecimalPlaces(4)

return change ? <span className={change > 0 ? 'text-lime-500' : 'text-red-400'}>{changeLabel}</span> : null
return change && quantity.gt(0) ? (
<span className={change > 0 ? 'text-lime-500' : 'text-red-400'}>{changeLabel}</span>
) : null
},
},
{
Expand Down
6 changes: 5 additions & 1 deletion packages/users/components/UserProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ async function UserPositionsTab({
sortDirection?: string
}
}) {
const { marketPositions, totalPages } = await getMarketPositions({ ownerId: userId, ...filters })
const { marketPositions, totalPages } = await getMarketPositions({
ownerId: userId,
...filters,
status: filters?.status ?? 'active',
})

return (
<div className="mt-3 md:mt-6">
Expand Down