Skip to content

Commit

Permalink
feat(console): posthog publish and delete app
Browse files Browse the repository at this point in the history
  • Loading branch information
poolsar42 committed Jul 11, 2023
1 parent d2c97bb commit 5052442
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
5 changes: 5 additions & 0 deletions apps/console/app/components/DeleteAppModal/DeleteAppModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ const DeleteModalAppForm = ({
/>
</section>
<input type="hidden" name="clientId" value={appDetails.clientId} />
<input
type="hidden"
name="published"
value={appDetails.published ? '1' : '0'}
/>

<div className="flex justify-end items-center space-x-3">
<Button
Expand Down
39 changes: 28 additions & 11 deletions apps/console/app/routes/apps/$clientId/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { requireJWT } from '~/utilities/session.server'
import { useEffect, useRef, useState } from 'react'
import { z } from 'zod'
import { RollType } from '~/types'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import {
getAuthzHeaderConditionallyFromToken,
parseJwt,
} from '@proofzero/utils'
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'

import { DeleteAppModal } from '~/components/DeleteAppModal/DeleteAppModal'
Expand All @@ -44,6 +47,7 @@ import { BadRequestError } from '@proofzero/errors'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { usePostHog } from 'posthog-js/react'
import { type AccountURN } from '@proofzero/urns/account'
import { posthogCall } from '@proofzero/utils/posthog'

/**
* @file app/routes/dashboard/index.tsx
Expand Down Expand Up @@ -146,6 +150,9 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
let rotatedSecret, updates

const jwt = await requireJWT(request)
const parsedJwt = parseJwt(jwt as string)
const accountURN = parsedJwt?.sub as AccountURN

const starbaseClient = createStarbaseClient(Starbase, {
...getAuthzHeaderConditionallyFromToken(jwt),
...generateTraceContextHeaders(context.traceSpan),
Expand All @@ -158,6 +165,7 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
const formData = await request.formData()
const op = formData.get('op')
const published = formData.get('published') === '1'
const previously_published = formData.get('previously_published') === '1'
const errors: errorsAuthProps = {}

// As part of the rolling operation
Expand Down Expand Up @@ -220,6 +228,17 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
}
console.debug('ERRORS', errors)

if (published === false && previously_published === true) {
await posthogCall({
eventName: 'app_unpublished',
apiKey: POSTHOG_API_KEY,
distinctId: accountURN,
properties: {
clientId: params.clientId,
},
})
}

return json({
rotatedSecret,
updatedApp: { published, app: { ...updates } },
Expand All @@ -236,20 +255,14 @@ export default function AppDetailIndexPage() {
const submit = useSubmit()
const actionData = useActionData()
const outletContextData = useOutletContext<{
accountURN: AccountURN
notificationHandler: notificationHandlerType
appDetails: appDetailsProps
rotationResult: any
paymaster: PaymasterType
appContactAddress?: AddressURN
}>()
const {
appContactAddress,
paymaster,
notificationHandler,
appDetails,
accountURN,
} = outletContextData
const { appContactAddress, paymaster, notificationHandler, appDetails } =
outletContextData
const { scopeMeta }: { scopeMeta: ScopeMeta } = useLoaderData()
const posthog = usePostHog()

Expand Down Expand Up @@ -292,8 +305,7 @@ export default function AppDetailIndexPage() {
if (actionData?.updatedApp) Object.assign(appDetails, actionData.updatedapp)
if (actionData?.published) {
posthog?.capture('app_published', {
distinct_id: appDetails.clientId,
account_urn: accountURN,
client_id: appDetails.clientId,
})
}
}, [actionData])
Expand Down Expand Up @@ -327,6 +339,11 @@ export default function AppDetailIndexPage() {
>
<fieldset disabled={isImgUploading}>
<input type="hidden" name="op" value="update_app" />
<input
type="hidden"
name="previously_published"
value={appDetails.published ? '1' : '0'}
/>

<section className="flex flex-col space-y-5">
<div className="flex flex-row justify-between space-x-5 max-sm:pl-6">
Expand Down
19 changes: 18 additions & 1 deletion apps/console/app/routes/apps/delete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,48 @@ import { redirect } from '@remix-run/cloudflare'
import type { ActionFunction } from '@remix-run/cloudflare'
import createStarbaseClient from '@proofzero/platform-clients/starbase'
import { requireJWT } from '~/utilities/session.server'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import {
getAuthzHeaderConditionallyFromToken,
parseJwt,
} from '@proofzero/utils'
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'
import {
JsonError,
getErrorCause,
getRollupReqFunctionErrorWrapper,
} from '@proofzero/utils/errors'
import { BadRequestError, InternalServerError } from '@proofzero/errors'
import { posthogCall } from '@proofzero/utils/posthog'
import { type AccountURN } from '@proofzero/urns/account'

export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context }) => {
const formData = await request.formData()
const clientId = formData.get('clientId')?.toString()
const published = formData.get('published')?.toString() === '1'

if (!clientId)
throw new BadRequestError({ message: 'Client ID is required' })

const jwt = await requireJWT(request)
const parsedJwt = parseJwt(jwt as string)
const accountURN = parsedJwt.sub as AccountURN

const starbaseClient = createStarbaseClient(Starbase, {
...getAuthzHeaderConditionallyFromToken(jwt),
...generateTraceContextHeaders(context.traceSpan),
})
try {
await starbaseClient.deleteApp.mutate({ clientId })
await posthogCall({
apiKey: POSTHOG_API_KEY,
eventName: 'app_deleted',
distinctId: accountURN,
properties: {
clientId,
published,
},
})
return redirect('/')
} catch (error) {
const cause = getErrorCause(error)
Expand Down

0 comments on commit 5052442

Please sign in to comment.