-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add open in playground context menu item (#205)
- Loading branch information
Showing
7 changed files
with
216 additions
and
137 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { InteractionsRegistry } from "dfx/gateway" | ||
import { Discord, Ix, IxHelpers } from "dfx" | ||
import { Effect, Encoding, Layer, Option } from "effect" | ||
import { DiscordLive } from "./Discord.js" | ||
|
||
export const PlaygroundLive = Effect.gen(function* () { | ||
const registry = yield* InteractionsRegistry | ||
|
||
const linkFromCode = (code: string) => | ||
Effect.sync(() => { | ||
const encoded = Encoding.encodeBase64Url(code) | ||
return `https://effect.website/play/?code=${encoded}` | ||
}) | ||
|
||
const menu = Ix.global( | ||
{ | ||
type: Discord.ApplicationCommandType.MESSAGE, | ||
name: "Open in playground", | ||
}, | ||
Effect.gen(function* () { | ||
const interaction = yield* Ix.Interaction | ||
const command = yield* Ix.ApplicationCommand | ||
const resolved = yield* IxHelpers.resolved(interaction) | ||
const message = resolved.messages?.[command.target_id!] | ||
const content = yield* Effect.fromNullable(message?.content) | ||
const code = yield* extractCode(content) | ||
const url = yield* linkFromCode(code) | ||
|
||
const response = `Here is your [playground link](${url}).` | ||
if (response.length > 1950) { | ||
return Ix.response({ | ||
type: Discord.InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE, | ||
data: { | ||
flags: Discord.MessageFlag.EPHEMERAL, | ||
content: `The code snippet is too long to be displayed in a single message.`, | ||
}, | ||
}) | ||
} | ||
return Ix.response({ | ||
type: Discord.InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE, | ||
data: { | ||
flags: Discord.MessageFlag.EPHEMERAL, | ||
content: `Here is your [playground link](${url}).`, | ||
}, | ||
}) | ||
}).pipe( | ||
Effect.catchTag("NoSuchElementException", () => | ||
Effect.succeed( | ||
Ix.response({ | ||
type: Discord.InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE, | ||
data: { | ||
flags: Discord.MessageFlag.EPHEMERAL, | ||
content: "No code snippets were found in the message.", | ||
}, | ||
}), | ||
), | ||
), | ||
), | ||
) | ||
|
||
const ix = Ix.builder.add(menu).catchAllCause(Effect.logError) | ||
yield* registry.register(ix) | ||
}).pipe(Layer.effectDiscard, Layer.provide(DiscordLive)) | ||
|
||
const extractCode = (content: string): Option.Option<string> => { | ||
const codeBlock = content.matchAll(/```.*$([\s\S]*?)```/gm) | ||
const items = [...codeBlock] | ||
return items.length > 0 | ||
? Option.some(items.map(([, code]) => code.trim()).join("\n\n\n")) | ||
: Option.none() | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
I have a doubt about effectful interfaces. I have a lib which exposes a bunch of functions to call remote functions in different ways. One, for example, asks for a lambda client (aws sdk) to make invocations. | ||
|
||
I have the need to consume such functions from a repo which doesn't have effect. Is there an existing combinator or such which trasposes the requirements to normal function arguments, maybe using a map or a record or something? | ||
|
||
``` | ||
class Test {} | ||
const test = new Test() | ||
``` |